Permalink
Browse files

float do not have enough precision to round if MRB_USE_FLOAT is set

  • Loading branch information...
1 parent 1563cdf commit 0270d11c81c28a5616eebd64d10be5133de4d7a0 @matz matz committed Jul 31, 2012
Showing with 31 additions and 30 deletions.
  1. +14 −13 src/numeric.c
  2. +17 −17 test/t/float.rb
View
@@ -195,7 +195,11 @@ flo_to_s(mrb_state *mrb, mrb_value flt)
} else if(isnan(value))
return mrb_str_new(mrb, "NaN", 3);
+#ifdef MRB_USE_FLOAT
+ n = sprintf(buf, "%.7g", value);
+#else
n = sprintf(buf, "%.14g", value);
+#endif
assert(n >= 0);
return mrb_str_new(mrb, buf, n);
}
@@ -511,21 +515,13 @@ flo_ceil(mrb_state *mrb, mrb_value num)
*/
static mrb_value
-flo_round(mrb_state *mrb, /*int argc, mrb_value *argv,*/ mrb_value num)
+flo_round(mrb_state *mrb, mrb_value num)
{
- mrb_value nd;
- mrb_float number, f;
+ double number, f;
int ndigits = 0, i;
- mrb_value *argv;
- int argc;
-
- mrb_get_args(mrb, "*", &argv, &argc);
- if (argc == 1) {
- nd = argv[0];
- ndigits = mrb_fixnum(nd);
- }
- number = mrb_float(num);
+ mrb_get_args(mrb, "|i", &ndigits);
+ number = mrb_float(num);
f = 1.0;
i = abs(ndigits);
while (--i >= 0)
@@ -535,7 +531,7 @@ flo_round(mrb_state *mrb, /*int argc, mrb_value *argv,*/ mrb_value num)
if (ndigits < 0) number = 0;
}
else {
- mrb_float d;
+ double d;
if (ndigits < 0) number /= f;
else number *= f;
@@ -554,6 +550,11 @@ flo_round(mrb_state *mrb, /*int argc, mrb_value *argv,*/ mrb_value num)
else number /= f;
}
+ {
+ mrb_value ff = mrb_float_value(number);
+
+ printf("%f.round(%d) = %f\n", mrb_float(num), ndigits, mrb_float(ff));
+ }
if (ndigits > 0) return mrb_float_value(number);
return mrb_fixnum_value((mrb_int)number);
}
View
@@ -26,11 +26,11 @@
end
assert('Float#*', '15.2.9.3.3') do
- a = 3.123456789 * 3.123456789
- b = 3.123456789 * 1
+ a = 3.125 * 3.125
+ b = 3.125 * 1
- check_float(a, 9.75598231275019) and
- check_float(b, 3.123456789)
+ check_float(a, 9.765625) and
+ check_float(b, 3.125)
end
assert('Float#/', '15.2.9.3.4') do
@@ -42,19 +42,19 @@
end
assert('Float#%', '15.2.9.3.5') do
- a = 3.123456789 % 3.123456789
- b = 3.123456789 % 1
+ a = 3.125 % 3.125
+ b = 3.125 % 1
check_float(a, 0.0) and
- check_float(b, 0.123456789)
+ check_float(b, 0.125)
end
assert('Float#<=>', '15.2.9.3.6') do
- a = 3.123456789 <=> 3.123456788
- b = 3.123456789 <=> 3.123456789
- c = 3.123456789 <=> 3.123456790
- a2 = 3.123456789 <=> 3
- c2 = 3.123456789 <=> 4
+ a = 3.125 <=> 3.123
+ b = 3.125 <=> 3.125
+ c = 3.125 <=> 3.126
+ a2 = 3.125 <=> 3
+ c2 = 3.125 <=> 4
a == 1 and b == 0 and c == -1 and
a2 == 1 and c2 == -1
@@ -96,16 +96,16 @@
assert('Float#round', '15.2.9.3.12') do
a = 3.123456789.round
b = 3.5.round
- c = 3.499999999.round
+ c = 3.4999.round
d = (-3.123456789).round
e = (-3.5).round
f = 12345.67.round(-1)
- g = 3.123456789.round(0)
- h = 3.123456789.round(1)
- i = 3.123456789.round(4)
+ g = 3.423456789.round(0)
+ h = 3.423456789.round(1)
+ i = 3.423456789.round(3)
a == 3 and b == 4 and c == 3 and d == -3 and e == -4 and
- f == 12350 and g == 3 and h == 3.1 and i == 3.1235
+ f == 12350 and g == 3 and h == 3.4 and i == 3.423
end
assert('Float#to_f', '15.2.9.3.13') do

0 comments on commit 0270d11

Please sign in to comment.