mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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
This commit is contained in:
parent
25c0cb981a
commit
fd3ef45a42
2 changed files with 26 additions and 6 deletions
|
@ -1,3 +1,10 @@
|
|||
Wed Nov 14 01:34:42 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* numeric.c (round): fallback definition.
|
||||
|
||||
* numeric.c (flo_divmod, flo_round): use round() always.
|
||||
[ruby-dev:32269]
|
||||
|
||||
Wed Nov 14 00:33:49 2007 Koichi Sasada <ko1@atdot.net>
|
||||
|
||||
* include/ruby/ruby.h: introduce 2 macros:
|
||||
|
|
25
numeric.c
25
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;
|
||||
|
||||
|
|
Loading…
Reference in a new issue