summaryrefslogtreecommitdiffhomepage
path: root/src/numeric.c
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2014-02-22 18:06:27 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2014-02-22 18:06:27 +0900
commite8daace8f14a1f564f92fd02a5732c9051702ea4 (patch)
tree83471dcf3f1e8b37ff56b7aa743cc6ff867839f1 /src/numeric.c
parentac936fc21103f8cc3c144f66c62b41cc8ee1165a (diff)
parentb0e3b873f5d33a750cc4b04a20a9e6634ece1a47 (diff)
downloadmruby-e8daace8f14a1f564f92fd02a5732c9051702ea4.tar.gz
mruby-e8daace8f14a1f564f92fd02a5732c9051702ea4.zip
Merge pull request #1724 from h2so5/f2s-significand
preserve significands in float-string conversion
Diffstat (limited to 'src/numeric.c')
-rw-r--r--src/numeric.c19
1 files changed, 18 insertions, 1 deletions
diff --git a/src/numeric.c b/src/numeric.c
index c17b9c251..a8c483148 100644
--- a/src/numeric.c
+++ b/src/numeric.c
@@ -143,8 +143,25 @@ mrb_flo_to_str(mrb_state *mrb, mrb_value flo)
}
exp = (n > 1) ? floor(log10(n)) : -ceil(-log10(n));
+
+ /* 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 ((exp < 0 ? -exp : exp) >= FLO_MAX_DIGITS) {
+ if (abs(exp) + length >= FLO_MAX_DIGITS) {
/* exponent representation */
e = TRUE;
n = n / pow(10.0, exp);