summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2012-03-24 07:57:16 +0100
committerYorhel <git@yorhel.nl>2012-03-24 07:57:16 +0100
commit187f286bb7af61276654dbf31c251d4680d48859 (patch)
treea6e00b8948c092a41f541228dfb08909fc5bad61
parent1e692b76a2be55cbb546e92105691c3e3d792d99 (diff)
Increase JSON number format precision
...to 15 significant digits. This does not cover the full range of a double (16 digits would be necessary for that), because using that many digits will also expose that some numbers can't be stored exactly. E.g. 1.2 will become 1.19999999999999996, etc. Which isn't very nice.
-rw-r--r--tanja.c4
-rw-r--r--test/json.c18
2 files changed, 11 insertions, 11 deletions
diff --git a/tanja.c b/tanja.c
index 2ea8acb..6e6cc88 100644
--- a/tanja.c
+++ b/tanja.c
@@ -294,7 +294,7 @@ char *tn_el_str(tn_el el, char *buf) {
return buf;
case TN_VT_NUM:
if(buf)
- snprintf(buf, 25, "%g", el.v.n);
+ snprintf(buf, 25, "%.15g", el.v.n);
return buf;
case TN_VT_STR:;
return el.v.s;
@@ -500,7 +500,7 @@ static void json_fmt_el(tn_el e, lbuf *buf) {
as(b);
break;
case TN_VT_NUM:
- snprintf(b, 25, "%g", e.v.n);
+ snprintf(b, 25, "%.15g", e.v.n);
as(b);
break;
case TN_VT_STR:
diff --git a/test/json.c b/test/json.c
index 01a099e..7759aa8 100644
--- a/test/json.c
+++ b/test/json.c
@@ -91,16 +91,16 @@ static void test_tuples() {
f("[10e--1]", 5);
f("[10e++1]", 5);
f("[1e1.4]", 4);
- t("[10e10]", "[1e+11]"); // these kinda depend on what snprintf() does...
- t("[10e0010]", "[1e+11]");
- t("[10.001e0010]", "[1.0001e+11]");
+ t("[10e10]", "[100000000000]"); // these kinda depend on what snprintf() does...
+ t("[10e0010]", "[100000000000]");
+ t("[10.001e0010]", "[100010000000]");
e("[-9223372036854775808]");
- t("[123456789012345678901234567890]", "[1.23457e+29]"); // TODO: more precision would be nice
- t("[-123456789012345678901234567890]", "[-1.23457e+29]");
- t("[0.123456789012345678901234567890]", "[0.123457]");
- t("[-0.123456789012345678901234567890]", "[-0.123457]");
- t("[123456789012345678901234567890.123456789012345678901234567890]", "[1.23457e+29]");
- t("[-123456789012345678901234567890.123456789012345678901234567890]", "[-1.23457e+29]");
+ t("[123456789012345678901234567890]", "[1.23456789012346e+29]");
+ t("[-123456789012345678901234567890]", "[-1.23456789012346e+29]");
+ t("[0.123456789012345678901234567890]", "[0.123456789012346]");
+ t("[-0.123456789012345678901234567890]", "[-0.123456789012346]");
+ t("[123456789012345678901234567890.123456789012345678901234567890]", "[1.23456789012346e+29]");
+ t("[-123456789012345678901234567890.123456789012345678901234567890]", "[-1.23456789012346e+29]");
t("[1e-9223372036854775808]", "[0]");
f("[1e-92233720368547758080]", 23); // doesn't matter much what this does, as long as it doesn't crash/leak
f("[1e+123456]", 10); // same