mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
rational.c: optimize Rational#abs
* rational.c (rb_rational_abs): optimize Rational#abs with the specialized implementation. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56808 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
b252126d6d
commit
ab9b7890c2
1 changed files with 26 additions and 0 deletions
26
rational.c
26
rational.c
|
@ -1235,6 +1235,30 @@ nurat_negative_p(VALUE self)
|
|||
return f_boolcast(INT_NEGATIVE_P(dat->num));
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* rat.abs -> rat
|
||||
* rat.magnitude -> rat
|
||||
*
|
||||
* Returns the absolute value of +rat+.
|
||||
*
|
||||
* (1/2r).abs #=> 1/2r
|
||||
* (-1/2r).abs #=> 1/2r
|
||||
*
|
||||
* Rational#magnitude is an alias of Rational#abs.
|
||||
*/
|
||||
|
||||
VALUE
|
||||
rb_rational_abs(VALUE self)
|
||||
{
|
||||
get_dat1(self);
|
||||
if (INT_NEGATIVE_P(dat->num)) {
|
||||
VALUE num = rb_int_abs(dat->num);
|
||||
return nurat_s_canonicalize_internal_no_reduce(CLASS_OF(self), num, dat->den);
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
static VALUE
|
||||
nurat_floor(VALUE self)
|
||||
{
|
||||
|
@ -2640,6 +2664,8 @@ Init_Rational(void)
|
|||
#endif
|
||||
rb_define_method(rb_cRational, "positive?", nurat_positive_p, 0);
|
||||
rb_define_method(rb_cRational, "negative?", nurat_negative_p, 0);
|
||||
rb_define_method(rb_cRational, "abs", rb_rational_abs, 0);
|
||||
rb_define_method(rb_cRational, "magnitude", rb_rational_abs, 0);
|
||||
|
||||
rb_define_method(rb_cRational, "floor", nurat_floor_n, -1);
|
||||
rb_define_method(rb_cRational, "ceil", nurat_ceil_n, -1);
|
||||
|
|
Loading…
Reference in a new issue