summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-11-13 16:34:45 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-11-13 16:34:45 +0000
commitfd3ef45a423675e71b22952b59963058039063a6 (patch)
treeca3beaa3907e686eb815f70f06c35bfedf843bd2 /numeric.c
parent25c0cb981af8fda9860ec6e365b9ea7a9e3f498f (diff)
* numeric.c (round): fallback definition.
* numeric.c (flo_divmod, flo_round): use round() always. [ruby-dev:32269] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13914 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c25
1 files changed, 19 insertions, 6 deletions
diff --git a/numeric.c b/numeric.c
index ef199fe66a..9c3ab2a865 100644
--- a/numeric.c
+++ b/numeric.c
@@ -63,6 +63,24 @@
#define DBL_EPSILON 2.2204460492503131e-16
#endif
+#ifndef HAVE_ROUND
+double
+round(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;
@@ -718,11 +736,7 @@ flo_divmod(VALUE x, VALUE y)
}
flodivmod(RFLOAT_VALUE(x), fy, &div, &mod);
if (FIXABLE(div)) {
-#ifdef HVAE_ROUND
val = round(div);
-#else
- val = (div < 0) ? ceil(x - 0.5) : floor(x + 0.5);
-#endif
a = LONG2FIX(val);
}
else if (isnan(div) || isinf(div)) {
@@ -1261,8 +1275,7 @@ flo_round(int argc, VALUE *argv, VALUE num)
if (ndigits < 0) number /= f;
else number *= f;
- if (number > 0.0) number = floor(number+0.5);
- if (number < 0.0) number = ceil(number-0.5);
+ number = round(number);
if (ndigits < 0) number *= f;
else number /= f;