Commit e8daace8 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

Merge pull request #1724 from h2so5/f2s-significand

preserve significands in float-string conversion
parents ac936fc2 b0e3b873
......@@ -144,7 +144,24 @@ mrb_flo_to_str(mrb_state *mrb, mrb_value flo)
exp = (n > 1) ? floor(log10(n)) : -ceil(-log10(n));
if ((exp < 0 ? -exp : exp) >= FLO_MAX_DIGITS) {
/* preserve significands */
int length = 0;
if (exp < 0) {
int i, beg = -1, end = 0;
double f = n;
double fd = 0;
for (i = 0; i < FLO_MAX_DIGITS; ++i) {
f = (f - fd) * 10.0;
fd = floor(f + FLO_EPSILON);
if (fd != 0) {
if (beg < 0) beg = i;
end = i + 1;
}
}
if (beg >= 0) length = end - beg;
}
if (abs(exp) + length >= FLO_MAX_DIGITS) {
/* exponent representation */
e = TRUE;
n = n / pow(10.0, exp);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment