summaryrefslogtreecommitdiff
path: root/bignum.c
diff options
context:
space:
mode:
authortadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-06-17 12:55:16 +0000
committertadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-06-17 12:55:16 +0000
commitb6849b2502e7f9ea93e5c1dc6a27359a6311f140 (patch)
treeb8de0496c999e2cb6eb3181204dd740cbfb3d515 /bignum.c
parentbbd2b5e9bd62c708a0ea026706528a114e6d5f5a (diff)
* bignum.c (rb_big_fdiv): checks whether the given second argument
can be converted to float properly. * numeric.c (fix_fdiv): calls rb_big_fdiv when the given second argument is a bignum. * rational.c (nurat_fdiv): should calculate Float(x/y), not Float(x)/Float(y). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@23726 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'bignum.c')
-rw-r--r--bignum.c103
1 files changed, 58 insertions, 45 deletions
diff --git a/bignum.c b/bignum.c
index 2f8ab20e32..cba4210946 100644
--- a/bignum.c
+++ b/bignum.c
@@ -2385,6 +2385,52 @@ big_shift(VALUE x, long n)
return x;
}
+static VALUE
+big_fdiv(VALUE x, VALUE y)
+{
+#define DBL_BIGDIG ((DBL_MANT_DIG + BITSPERDIG) / BITSPERDIG)
+ VALUE z;
+ long l, ex, ey;
+ int i;
+
+ bigtrunc(x);
+ l = RBIGNUM_LEN(x) - 1;
+ ex = l * BITSPERDIG;
+ ex += bdigbitsize(BDIGITS(x)[l]);
+ ex -= 2 * DBL_BIGDIG * BITSPERDIG;
+ if (ex) x = big_shift(x, ex);
+
+ switch (TYPE(y)) {
+ case T_FIXNUM:
+ y = rb_int2big(FIX2LONG(y));
+ case T_BIGNUM: {
+ bigtrunc(y);
+ l = RBIGNUM_LEN(y) - 1;
+ ey = l * BITSPERDIG;
+ ey += bdigbitsize(BDIGITS(y)[l]);
+ ey -= DBL_BIGDIG * BITSPERDIG;
+ if (ey) y = big_shift(y, ey);
+ bignum:
+ bigdivrem(x, y, &z, 0);
+ l = ex - ey;
+#if SIZEOF_LONG > SIZEOF_INT
+ {
+ /* Visual C++ can't be here */
+ if (l > INT_MAX) return DBL2NUM(ruby_div0(1.0));
+ if (l < INT_MIN) return DBL2NUM(0.0);
+ }
+#endif
+ return DBL2NUM(ldexp(big2dbl(z), (int)l));
+ }
+ case T_FLOAT:
+ y = dbl2big(ldexp(frexp(RFLOAT_VALUE(y), &i), DBL_MANT_DIG));
+ ey = i - DBL_MANT_DIG;
+ goto bignum;
+ }
+ rb_bug("big_fdiv");
+ /* NOTREACHED */
+}
+
/*
* call-seq:
* big.fdiv(numeric) -> float
@@ -2397,65 +2443,32 @@ big_shift(VALUE x, long n)
*
*/
-static VALUE
+
+VALUE
rb_big_fdiv(VALUE x, VALUE y)
{
- double dx = big2dbl(x);
- double dy;
+ double dx, dy;
- if (isinf(dx)) {
-#define DBL_BIGDIG ((DBL_MANT_DIG + BITSPERDIG) / BITSPERDIG)
- VALUE z;
- long l, ex, ey;
- int i;
-
- bigtrunc(x);
- l = RBIGNUM_LEN(x) - 1;
- ex = l * BITSPERDIG;
- ex += bdigbitsize(BDIGITS(x)[l]);
- ex -= 2 * DBL_BIGDIG * BITSPERDIG;
- if (ex) x = big_shift(x, ex);
-
- switch (TYPE(y)) {
- case T_FIXNUM:
- y = rb_int2big(FIX2LONG(y));
- case T_BIGNUM: {
- bigtrunc(y);
- l = RBIGNUM_LEN(y) - 1;
- ey = l * BITSPERDIG;
- ey += bdigbitsize(BDIGITS(y)[l]);
- ey -= DBL_BIGDIG * BITSPERDIG;
- if (ey) y = big_shift(y, ey);
- bignum:
- bigdivrem(x, y, &z, 0);
- l = ex - ey;
-#if SIZEOF_LONG > SIZEOF_INT
- {
- /* Visual C++ can't be here */
- if (l > INT_MAX) return DBL2NUM(ruby_div0(1.0));
- if (l < INT_MIN) return DBL2NUM(0.0);
- }
-#endif
- return DBL2NUM(ldexp(big2dbl(z), (int)l));
- }
- case T_FLOAT:
- if (isnan(RFLOAT_VALUE(y))) return y;
- y = dbl2big(ldexp(frexp(RFLOAT_VALUE(y), &i), DBL_MANT_DIG));
- ey = i - DBL_MANT_DIG;
- goto bignum;
- }
- }
+ dx = big2dbl(x);
switch (TYPE(y)) {
case T_FIXNUM:
dy = (double)FIX2LONG(y);
+ if (isinf(dx))
+ return big_fdiv(x, y);
break;
case T_BIGNUM:
dy = rb_big2dbl(y);
+ if (isinf(dx) || isinf(dy))
+ return big_fdiv(x, y);
break;
case T_FLOAT:
dy = RFLOAT_VALUE(y);
+ if (isnan(dy))
+ return y;
+ if (isinf(dx))
+ return big_fdiv(x, y);
break;
default: