aboutsummaryrefslogtreecommitdiffstats
path: root/math.c
diff options
context:
space:
mode:
authorgogotanaka <gogotanaka@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-03-03 05:59:28 +0000
committergogotanaka <gogotanaka@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-03-03 05:59:28 +0000
commit972713cee1267c022483a8c6d6ccf713b6c3889d (patch)
tree4fc81f3a596afa2f475ca252781cb50b42b202ba /math.c
parent5eea2af4811ead7f9935a6670c7e672379f4cf92 (diff)
downloadruby-972713cee1267c022483a8c6d6ccf713b6c3889d.tar.gz
* math.c (num2dbl_with_to_f): direct casting from Rational to double.
[Feature #10909] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49818 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'math.c')
-rw-r--r--math.c15
1 files changed, 13 insertions, 2 deletions
diff --git a/math.c b/math.c
index 5d4c2bbea5..fc03b5b0d4 100644
--- a/math.c
+++ b/math.c
@@ -32,13 +32,20 @@ basic_to_f_p(VALUE klass)
return rb_method_basic_definition_p(klass, id_to_f);
}
+#define fix2dbl_without_to_f(x) (double)FIX2LONG(x)
+#define big2dbl_without_to_f(x) rb_big2dbl(x)
+#define int2dbl_without_to_f(x) (FIXNUM_P(x) ? fix2dbl_without_to_f(x) : big2dbl_without_to_f(x))
+#define rat2dbl_without_to_f(x) \
+ (int2dbl_without_to_f(rb_rational_num(x)) / \
+ int2dbl_without_to_f(rb_rational_den(x)))
+
static inline double
num2dbl_with_to_f(VALUE num)
{
if (SPECIAL_CONST_P(num)) {
if (FIXNUM_P(num)) {
if (basic_to_f_p(rb_cFixnum))
- return (double)FIX2LONG(num);
+ return fix2dbl_without_to_f(num);
}
else if (FLONUM_P(num)) {
return RFLOAT_VALUE(num);
@@ -50,7 +57,11 @@ num2dbl_with_to_f(VALUE num)
return RFLOAT_VALUE(num);
case T_BIGNUM:
if (basic_to_f_p(rb_cBignum))
- return rb_big2dbl(num);
+ return big2dbl_without_to_f(num);
+ break;
+ case T_RATIONAL:
+ if (basic_to_f_p(rb_cRational))
+ return rat2dbl_without_to_f(num);
break;
}
}