Permalink
Browse files

Avoid the rounding error of 0.49999999999999994 + 0.5 in Float#round

  • Loading branch information...
1 parent 5fa4878 commit 411077059c31b36034c62019d5eca5049f754139 @shugo shugo committed Nov 12, 2009
Showing with 8 additions and 2 deletions.
  1. +8 −2 vm/builtin/float.cpp
View
@@ -223,8 +223,14 @@ namespace rubinius {
Integer* Float::fround(STATE) {
double value = this->val;
- if (value > 0.0) value = floor(value+0.5);
- if (value < 0.0) value = ceil(value-0.5);
+ if (value > 0.0) {
+ value = floor(value);
+ if (this->val - value >= 0.5) value += 1.0;
+ }
+ if (value < 0.0) {
+ value = ceil(value);
+ if (value - this->val >= 0.5) value -= 1.0;
+ }
return Bignum::from_double(state, value);
}

0 comments on commit 4110770

Please sign in to comment.