diff options
author | mrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-11-11 15:55:30 +0000 |
---|---|---|
committer | mrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-11-11 15:55:30 +0000 |
commit | 06d701a1f0c5981a386458c55cccef9486819cb5 (patch) | |
tree | 307f02a0d50d4ffdb9e82df1c345d072527b2b14 /rational.c | |
parent | 6feb1684f36c7b160eba6b68be43a6cad05e4c40 (diff) | |
download | ruby-06d701a1f0c5981a386458c55cccef9486819cb5.tar.gz |
rational.c: avoid needless object allocation with nurat_to_double
* rational.c (nurat_to_double): introduce to convert rational to double
without object allocation.
* rational.c (rb_rational_plus, nurat_{sub,mul,to_f}): rewrite by using
nurat_to_double.
* bignum.c (rb_big_fdiv_double): introduce to calculate fdiv and return
the result as a double value.
* bignum.c (big_fdiv{,_int,_float}): change the return types for
implementing rb_big_fdiv_double.
* bignum.c (rb_big_fdiv): rewrite by using rb_big_fdiv_double.
* numeric.c (rb_int_fdiv_double): introduce to calculate fdiv and return
the result as a double value.
* numeric.c (fix_fdiv_double): rewrite from fix_fdiv to return the
result as a double value.
* numeric.c (rb_int_fdiv): rewrite by using rb_int_fdiv_double.
* internal.h (rb_{big,int}_fdiv_double): exported.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56719 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'rational.c')
-rw-r--r-- | rational.c | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/rational.c b/rational.c index 2ba501b4a2..44a720aebe 100644 --- a/rational.c +++ b/rational.c @@ -721,7 +721,7 @@ f_addsub(VALUE self, VALUE anum, VALUE aden, VALUE bnum, VALUE bden, int k) return f_rational_new_no_reduce2(CLASS_OF(self), num, den); } -static VALUE nurat_to_f(VALUE self); +static double nurat_to_double(VALUE self); /* * call-seq: * rat + numeric -> numeric @@ -747,7 +747,7 @@ rb_rational_plus(VALUE self, VALUE other) } } else if (RB_TYPE_P(other, T_FLOAT)) { - return DBL2NUM(RFLOAT_VALUE(nurat_to_f(self)) + RFLOAT_VALUE(other)); + return DBL2NUM(nurat_to_double(self) + RFLOAT_VALUE(other)); } else if (RB_TYPE_P(other, T_RATIONAL)) { { @@ -788,7 +788,7 @@ nurat_sub(VALUE self, VALUE other) } } else if (RB_FLOAT_TYPE_P(other)) { - return DBL2NUM(RFLOAT_VALUE(nurat_to_f(self)) - RFLOAT_VALUE(other)); + return DBL2NUM(nurat_to_double(self) - RFLOAT_VALUE(other)); } else if (RB_TYPE_P(other, T_RATIONAL)) { { @@ -868,7 +868,7 @@ nurat_mul(VALUE self, VALUE other) } } else if (RB_FLOAT_TYPE_P(other)) { - return DBL2NUM(RFLOAT_VALUE(nurat_to_f(self)) * RFLOAT_VALUE(other)); + return DBL2NUM(nurat_to_double(self) * RFLOAT_VALUE(other)); } else if (RB_TYPE_P(other, T_RATIONAL)) { { @@ -1433,6 +1433,13 @@ nurat_round_n(int argc, VALUE *argv, VALUE self) return f_round_common(argc, argv, self, round_func); } +static double +nurat_to_double(VALUE self) +{ + get_dat1(self); + return rb_int_fdiv_double(dat->num, dat->den); +} + /* * call-seq: * rat.to_f -> float @@ -1447,8 +1454,7 @@ nurat_round_n(int argc, VALUE *argv, VALUE self) static VALUE nurat_to_f(VALUE self) { - get_dat1(self); - return rb_int_fdiv(dat->num, dat->den); + return DBL2NUM(nurat_to_double(self)); } /* |