1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* numeric.c (int_odd_p): a new method to check even or odd.

[RCR#337]

* numeric.c (int_even_p): ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10982 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2006-09-21 06:09:26 +00:00
parent 4e37427ee5
commit 255702be8c
2 changed files with 75 additions and 0 deletions

View file

@ -1,3 +1,10 @@
Thu Sep 21 15:06:24 2006 Yukihiro Matsumoto <matz@ruby-lang.org>
* numeric.c (int_odd_p): a new method to check even or odd.
[RCR#337]
* numeric.c (int_even_p): ditto.
Thu Sep 21 13:55:07 2006 Yukihiro Matsumoto <matz@ruby-lang.org>
* ext/etc/etc.c (etc_getpwuid): uid integer should be wraped in

View file

@ -1698,6 +1698,38 @@ int_int_p(VALUE num)
return Qtrue;
}
/*
* call-seq:
* int.odd? -> true or false
*
* Returns <code>true</code> if <i>int</i> is an odd number.
*/
static VALUE
int_odd_p(VALUE num)
{
if (rb_funcall(num, '%', 1, INT2FIX(2)) != INT2FIX(0)) {
return Qtrue;
}
return Qfalse;
}
/*
* call-seq:
* int.even? -> true or false
*
* Returns <code>true</code> if <i>int</i> is an even number.
*/
static VALUE
int_even_p(VALUE num)
{
if (rb_funcall(num, '%', 1, INT2FIX(2)) == INT2FIX(0)) {
return Qtrue;
}
return Qfalse;
}
/*
* call-seq:
* int.next => integer
@ -2799,6 +2831,38 @@ fix_zero_p(VALUE num)
return Qfalse;
}
/*
* call-seq:
* fix.odd? -> true or false
*
* Returns <code>true</code> if <i>fix</i> is an odd number.
*/
static VALUE
fix_odd_p(VALUE num)
{
if (num & 2) {
return Qtrue;
}
return Qfalse;
}
/*
* call-seq:
* fix.even? -> true or false
*
* Returns <code>true</code> if <i>fix</i> is an even number.
*/
static VALUE
fix_even_p(VALUE num)
{
if (num & 2) {
return Qfalse;
}
return Qtrue;
}
void
Init_Numeric(void)
{
@ -2853,6 +2917,8 @@ Init_Numeric(void)
rb_undef_method(CLASS_OF(rb_cInteger), "new");
rb_define_method(rb_cInteger, "integer?", int_int_p, 0);
rb_define_method(rb_cInteger, "odd?", int_odd_p, 0);
rb_define_method(rb_cInteger, "even?", int_even_p, 0);
rb_define_method(rb_cInteger, "upto", int_upto, 1);
rb_define_method(rb_cInteger, "downto", int_downto, 1);
rb_define_method(rb_cInteger, "times", int_dotimes, 0);
@ -2910,6 +2976,8 @@ Init_Numeric(void)
rb_define_method(rb_cFixnum, "to_f", fix_to_f, 0);
rb_define_method(rb_cFixnum, "size", fix_size, 0);
rb_define_method(rb_cFixnum, "zero?", fix_zero_p, 0);
rb_define_method(rb_cInteger, "odd?", fix_odd_p, 0);
rb_define_method(rb_cInteger, "even?", fix_even_p, 0);
rb_cFloat = rb_define_class("Float", rb_cNumeric);