diff options
Diffstat (limited to 'numeric.c')
-rw-r--r-- | numeric.c | 40 |
1 files changed, 32 insertions, 8 deletions
@@ -63,6 +63,25 @@ #define DBL_EPSILON 2.2204460492503131e-16 #endif +#ifndef HAVE_ROUND +double +round(x) + double x; +{ + double f; + + if (x > 0.0) { + f = floor(x); + x = f + (x - f >= 0.5); + } + else if (x < 0.0) { + f = ceil(x); + x = f - (f - x >= 0.5); + } + return x; +} +#endif + static ID id_coerce, id_to_i, id_eq; VALUE rb_cNumeric; @@ -662,7 +681,10 @@ flodivmod(x, y, divp, modp) mod = x - z * y; } #endif - div = (x - mod) / y; + if (isinf(x) && !isinf(y) && !isnan(y)) + div = x; + else + div = (x - mod) / y; if (y*mod < 0) { mod += y; div -= 1.0; @@ -735,11 +757,14 @@ flo_divmod(x, y) } flodivmod(RFLOAT(x)->value, fy, &div, &mod); if (FIXABLE(div)) { - val = div; - a = LONG2FIX(val); + val = round(div); + a = LONG2FIX(val); + } + else if (isnan(div) || isinf(div)) { + a = rb_float_new(div); } else { - a = rb_dbl2big(div); + a = rb_dbl2big(div); } b = rb_float_new(mod); return rb_assoc_new(a, b); @@ -1291,8 +1316,7 @@ flo_round(num) double f = RFLOAT(num)->value; long val; - if (f > 0.0) f = floor(f+0.5); - if (f < 0.0) f = ceil(f-0.5); + f = round(f); if (!FIXABLE(f)) { return rb_dbl2big(f); @@ -1530,7 +1554,7 @@ rb_num2long(val) char *s; sprintf(buf, "%-.10g", RFLOAT(val)->value); - if (s = strchr(buf, ' ')) *s = '\0'; + if ((s = strchr(buf, ' ')) != 0) *s = '\0'; rb_raise(rb_eRangeError, "float %s out of range of integer", buf); } @@ -1681,7 +1705,7 @@ rb_num2ll(val) char *s; sprintf(buf, "%-.10g", RFLOAT(val)->value); - if (s = strchr(buf, ' ')) *s = '\0'; + if ((s = strchr(buf, ' ')) != 0) *s = '\0'; rb_raise(rb_eRangeError, "float %s out of range of long long", buf); } |