From: Serge Petrenko <sergepetrenko@tarantool.org>
To: vdavydov.dev@gmail.com
Cc: tarantool-patches@freelists.org,
Serge Petrenko <sergepetrenko@tarantool.org>
Subject: [PATCH v3 3/5] decimal: fix string formatting on construction from double
Date: Tue, 2 Jul 2019 20:27:50 +0300 [thread overview]
Message-ID: <cf5d6c60968e213091676f57706542b8270f6467.1562087728.git.sergepetrenko@tarantool.org> (raw)
In-Reply-To: <cover.1562087728.git.sergepetrenko@tarantool.org>
Use printf "%g" option instead of "%f" to trim traling zeros in such
cases:
decimal_from_double(1) -> '1.000000000000000' -> decimal_from_string()
Now it should be
decimal_from_double(1) -> '1' ->decimal_from_string()
Follow-up 6d62c6c19ed418932ead1bba44fcd7cd84c78a19
---
src/lib/core/decimal.c | 10 +++++++++-
test/unit/decimal.c | 3 ++-
2 files changed, 11 insertions(+), 2 deletions(-)
diff --git a/src/lib/core/decimal.c b/src/lib/core/decimal.c
index 91ee3f307..1b4f44362 100644
--- a/src/lib/core/decimal.c
+++ b/src/lib/core/decimal.c
@@ -121,7 +121,15 @@ decimal_from_double(decimal_t *dec, double d)
char buf[DECIMAL_MAX_DIGITS + 3];
if (isinf(d) || isnan(d))
return NULL;
- snprintf(buf, sizeof(buf), "%.*f", DBL_DIG, d);
+ /*
+ * DBL_DIG is 15, it is the guaranteed amount of
+ * correct significant decimal digits in a double
+ * value. There is no point in using higher precision,
+ * since every non-representable number has a long
+ * tail of erroneous digits:
+ * `23.42` -> `23.420000000000001705302565824240446091`
+ */
+ snprintf(buf, sizeof(buf), "%.*g", DBL_DIG, d);
return decimal_from_string(dec, buf);
}
diff --git a/test/unit/decimal.c b/test/unit/decimal.c
index ae806a106..b587e1f14 100644
--- a/test/unit/decimal.c
+++ b/test/unit/decimal.c
@@ -27,7 +27,8 @@
\
is(decimal_div(&t, &u, &v), &t, "decimal("#a") / decimal("#b")");\
is(decimal_from_double(&w, (double)((a)) / (b)), &w, "decimal(("#a") / ("#b"))");\
- is(decimal_round(&t, DBL_DIG), &t, "decimal_round(("#a")/("#b"), %d)", DBL_DIG);\
+ is(decimal_round(&t, DBL_DIG - decimal_precision(&t) + decimal_scale(&t)), &t,\
+ "decimal_round(("#a")/("#b"), %d)", DBL_DIG);\
is(decimal_compare(&t, &w), 0, "decimal("#a") / decimal("#b") == ("#a") / ("#b")");\
})
--
2.20.1 (Apple Git-117)
next prev parent reply other threads:[~2019-07-02 17:27 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-07-02 17:27 [PATCH v3 0/5] decimal: expose decimal module to Lua Serge Petrenko
2019-07-02 17:27 ` [PATCH v3 1/5] decimal: fix ln hang on values between ~ 0.9 and 1.1 Serge Petrenko
2019-07-02 17:27 ` [PATCH v3 2/5] decimal: diallow infinity and NaN entirely Serge Petrenko
2019-07-02 17:27 ` Serge Petrenko [this message]
2019-07-02 17:27 ` [PATCH v3 4/5] lua/utils: add a function to register FFI metatypes Serge Petrenko
2019-07-02 17:27 ` [PATCH v3 5/5] decimal: expose decimal type to lua Serge Petrenko
2019-07-03 6:50 ` [tarantool-patches] " Serge Petrenko
2019-07-08 14:25 ` [PATCH v3 0/5] decimal: expose decimal module to Lua Vladimir Davydov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=cf5d6c60968e213091676f57706542b8270f6467.1562087728.git.sergepetrenko@tarantool.org \
--to=sergepetrenko@tarantool.org \
--cc=tarantool-patches@freelists.org \
--cc=vdavydov.dev@gmail.com \
--subject='Re: [PATCH v3 3/5] decimal: fix string formatting on construction from double' \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox