summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-08-03 07:09:48 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-08-03 07:09:48 +0000
commitc32b77cee1ead76eafb05604d49b315217c49d49 (patch)
treea5944d3699f97c0ec42192d67aa9f9b352188ef8 /numeric.c
parentce8d9b4a0d2c16ff2d634ce6c4b00a9156cc55f3 (diff)
* numeric.c (fix_minus, fix_mul, fix_quo, fix_div, fix_mod,
fix_divmod, fix_pow): ditto. * numeric.c (fix_plus): reduce coercing when a method knows about a operand type. [ruby-dev:26723] * bignum.c (rb_big_div, rb_big_modulo): export to reduce coercing. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8897 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c90
1 files changed, 76 insertions, 14 deletions
diff --git a/numeric.c b/numeric.c
index f602679d1e..ec01d69260 100644
--- a/numeric.c
+++ b/numeric.c
@@ -1985,10 +1985,14 @@ fix_plus(x, y)
}
return r;
}
- if (TYPE(y) == T_FLOAT) {
+ switch (TYPE(y)) {
+ case T_BIGNUM:
+ return rb_big_plus(y, x);
+ case T_FLOAT:
return rb_float_new((double)FIX2LONG(x) + RFLOAT(y)->value);
+ default:
+ return rb_num_coerce_bin(x, y);
}
- return rb_num_coerce_bin(x, y);
}
/*
@@ -2018,10 +2022,15 @@ fix_minus(x, y)
}
return r;
}
- if (TYPE(y) == T_FLOAT) {
+ switch (TYPE(y)) {
+ case T_BIGNUM:
+ x = rb_int2big(FIX2LONG(x));
+ return rb_big_minus(x, y);
+ case T_FLOAT:
return rb_float_new((double)FIX2LONG(x) - RFLOAT(y)->value);
+ default:
+ return rb_num_coerce_bin(x, y);
}
- return rb_num_coerce_bin(x, y);
}
/*
@@ -2053,10 +2062,14 @@ fix_mul(x, y)
}
return r;
}
- if (TYPE(y) == T_FLOAT) {
+ switch (TYPE(y)) {
+ case T_BIGNUM:
+ return rb_big_mul(y, x);
+ case T_FLOAT:
return rb_float_new((double)FIX2LONG(x) * RFLOAT(y)->value);
+ default:
+ return rb_num_coerce_bin(x, y);
}
- return rb_num_coerce_bin(x, y);
}
static void
@@ -2107,7 +2120,14 @@ fix_quo(x, y)
if (FIXNUM_P(y)) {
return rb_float_new((double)FIX2LONG(x) / (double)FIX2LONG(y));
}
- return rb_num_coerce_bin(x, y);
+ switch (TYPE(y)) {
+ case T_BIGNUM:
+ return rb_float_new((double)FIX2LONG(y) / rb_big2dbl(y));
+ case T_FLOAT:
+ return rb_float_new((double)FIX2LONG(x) / RFLOAT(y)->value);
+ default:
+ return rb_num_coerce_bin(x, y);
+ }
}
/*
@@ -2130,7 +2150,15 @@ fix_div(x, y)
fixdivmod(FIX2LONG(x), FIX2LONG(y), &div, 0);
return LONG2NUM(div);
}
- return rb_num_coerce_bin(x, y);
+ switch (TYPE(y)) {
+ case T_BIGNUM:
+ x = rb_int2big(FIX2LONG(x));
+ return rb_big_div(x, y);
+ case T_FLOAT:
+ return rb_Integer(rb_float_new((double)FIX2LONG(x) / RFLOAT(y)->value));
+ default:
+ return rb_num_coerce_bin(x, y);
+ }
}
/*
@@ -2152,7 +2180,20 @@ fix_mod(x, y)
fixdivmod(FIX2LONG(x), FIX2LONG(y), 0, &mod);
return LONG2NUM(mod);
}
- return rb_num_coerce_bin(x, y);
+ switch (TYPE(y)) {
+ case T_BIGNUM:
+ x = rb_int2big(FIX2LONG(x));
+ return rb_big_modulo(x, y);
+ case T_FLOAT:
+ {
+ double mod;
+
+ flodivmod((double)FIX2LONG(x), RFLOAT(y)->value, 0, &mod);
+ return rb_float_new(mod);
+ }
+ default:
+ return rb_num_coerce_bin(x, y);
+ }
}
/*
@@ -2172,7 +2213,23 @@ fix_divmod(x, y)
return rb_assoc_new(LONG2NUM(div), LONG2NUM(mod));
}
- return rb_num_coerce_bin(x, y);
+ switch (TYPE(y)) {
+ case T_BIGNUM:
+ x = rb_int2big(FIX2LONG(x));
+ return rb_big_divmod(x, y);
+ case T_FLOAT:
+ {
+ double div, mod;
+ volatile VALUE a, b;
+
+ flodivmod((double)FIX2LONG(x), RFLOAT(y)->value, &div, &mod);
+ a = rb_float_new(div);
+ b = rb_float_new(mod);
+ return rb_assoc_new(a, b);
+ }
+ default:
+ return rb_num_coerce_bin(x, y);
+ }
}
/*
@@ -2202,11 +2259,16 @@ fix_pow(x, y)
return rb_big_pow(rb_int2big(a), y);
}
return rb_float_new(pow((double)a, (double)b));
- } else if (TYPE(y) == T_FLOAT) {
- long a = FIX2LONG(x);
- return rb_float_new(pow((double)a, RFLOAT(y)->value));
}
- return rb_num_coerce_bin(x, y);
+ switch (TYPE(y)) {
+ case T_BIGNUM:
+ x = rb_int2big(FIX2LONG(x));
+ return rb_big_pow(x, y);
+ case T_FLOAT:
+ return rb_float_new(pow((double)FIX2LONG(x), RFLOAT(y)->value));
+ default:
+ return rb_num_coerce_bin(x, y);
+ }
}
/*