Merge pull request #6793 from mattn/fix/readfloat-correctly-rounded

This commit is contained in:
Yukihiro "Matz" Matsumoto
2026-04-16 17:09:10 +09:00
committed by GitHub
+3 -2
View File
@@ -168,10 +168,11 @@ mrb_read_float(const char *str, char **endp, double *fp)
res = (double)int_part;
}
else {
// Fast path: combine integer and fractional parts
// Divide by the exact 10^n (exact for n <= 22) rather than multiplying
// by the inexact 10^-n, so the fraction is correctly rounded.
res = (double)int_part;
if (frac_digits > 0) {
res += (double)frac_part * mrb_pow10(-frac_digits);
res += (double)frac_part / mrb_pow10(frac_digits);
}
}