From 4bd57e3311ef216016ba06560f2b531147678511 Mon Sep 17 00:00:00 2001 From: matz Date: Fri, 16 Nov 2007 14:41:11 +0000 Subject: * bignum.c (rb_big_odd_p): new method added. a patch from Tadashi Saito . [ruby-dev:32305] * bignum.c (rb_big_even_p): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13950 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- bignum.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'bignum.c') diff --git a/bignum.c b/bignum.c index 138922c417..71e0fb8da7 100644 --- a/bignum.c +++ b/bignum.c @@ -2431,6 +2431,38 @@ rb_big_size(VALUE big) return LONG2FIX(RBIGNUM_LEN(big)*SIZEOF_BDIGITS); } +/* + * call-seq: + * big.odd? -> true or false + * + * Returns true if big is an odd number. + */ + +static VALUE +rb_big_odd_p(VALUE num) +{ + if (BDIGITS(num)[0] & 1) { + return Qtrue; + } + return Qfalse; +} + +/* + * call-seq: + * big.even? -> true or false + * + * Returns true if big is an even number. + */ + +static VALUE +rb_big_even_p(VALUE num) +{ + if (BDIGITS(num)[0] & 1) { + return Qfalse; + } + return Qtrue; +} + /* * Bignum objects hold integers outside the range of * Fixnum. Bignum objects are created @@ -2484,6 +2516,8 @@ Init_Bignum(void) rb_define_method(rb_cBignum, "to_f", rb_big_to_f, 0); rb_define_method(rb_cBignum, "abs", rb_big_abs, 0); rb_define_method(rb_cBignum, "size", rb_big_size, 0); + rb_define_method(rb_cBignum, "odd?", rb_big_odd_p, 0); + rb_define_method(rb_cBignum, "even?", rb_big_even_p, 0); power_cache_init(); } -- cgit v1.2.3