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

Improve performance some Numeric methods [Feature #17632] (#4190)

This commit is contained in:
S.H 2021-02-20 04:11:19 +09:00 committed by GitHub
parent 837e77e020
commit efd19badf4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
Notes: git 2021-02-20 04:11:40 +09:00
Merged-By: k0kubun <takashikkbn@gmail.com>
3 changed files with 59 additions and 58 deletions

View file

@ -0,0 +1,13 @@
prelude: |
int = 42
flo = 4.2
benchmark:
real?: |
int.real?
integer?: |
flo.integer?
finite?: |
int.finite?
infinite?: |
int.infinite?
loop_count: 20000000

View file

@ -710,35 +710,6 @@ num_divmod(VALUE x, VALUE y)
return rb_assoc_new(num_div(x, y), num_modulo(x, y));
}
/*
* call-seq:
* num.real? -> true or false
*
* Returns +true+ if +num+ is a real number (i.e. not Complex).
*/
static VALUE
num_real_p(VALUE num)
{
return Qtrue;
}
/*
* call-seq:
* num.integer? -> true or false
*
* Returns +true+ if +num+ is an Integer.
*
* 1.0.integer? #=> false
* 1.integer? #=> true
*/
static VALUE
num_int_p(VALUE num)
{
return Qfalse;
}
/*
* call-seq:
* num.abs -> numeric
@ -824,31 +795,6 @@ num_nonzero_p(VALUE num)
return num;
}
/*
* call-seq:
* num.finite? -> true or false
*
* Returns +true+ if +num+ is a finite number, otherwise returns +false+.
*/
static VALUE
num_finite_p(VALUE num)
{
return Qtrue;
}
/*
* call-seq:
* num.infinite? -> -1, 1, or nil
*
* Returns +nil+, -1, or 1 depending on whether the value is
* finite, <code>-Infinity</code>, or <code>+Infinity</code>.
*/
static VALUE
num_infinite_p(VALUE num)
{
return Qnil;
}
/*
* call-seq:
* num.to_int -> integer
@ -5430,12 +5376,8 @@ Init_Numeric(void)
rb_define_method(rb_cNumeric, "magnitude", num_abs, 0);
rb_define_method(rb_cNumeric, "to_int", num_to_int, 0);
rb_define_method(rb_cNumeric, "real?", num_real_p, 0);
rb_define_method(rb_cNumeric, "integer?", num_int_p, 0);
rb_define_method(rb_cNumeric, "zero?", num_zero_p, 0);
rb_define_method(rb_cNumeric, "nonzero?", num_nonzero_p, 0);
rb_define_method(rb_cNumeric, "finite?", num_finite_p, 0);
rb_define_method(rb_cNumeric, "infinite?", num_infinite_p, 0);
rb_define_method(rb_cNumeric, "floor", num_floor, -1);
rb_define_method(rb_cNumeric, "ceil", num_ceil, -1);

View file

@ -1,3 +1,49 @@
class Numeric
#
# call-seq:
# num.real? -> true or false
#
# Returns +true+ if +num+ is a real number (i.e. not Complex).
#
def real?
return true
end
#
# call-seq:
# num.integer? -> true or false
#
# Returns +true+ if +num+ is an Integer.
#
# 1.0.integer? #=> false
# 1.integer? #=> true
#
def integer?
return false
end
#
# call-seq:
# num.finite? -> true or false
#
# Returns +true+ if +num+ is a finite number, otherwise returns +false+.
#
def finite?
return true
end
#
# call-seq:
# num.infinite? -> -1, 1, or nil
#
# Returns +nil+, -1, or 1 depending on whether the value is
# finite, <code>-Infinity</code>, or <code>+Infinity</code>.
#
def infinite?
return nil
end
end
class Integer
# call-seq:
# -int -> integer