webmcp

changeset 430:33d43f5a319b

json.export(...): Avoid exponential representation for integral floats when they could fit into a 64 bit integer
author jbe
date Thu Jan 14 18:43:11 2016 +0100 (2016-01-14)
parents 387a303918a6
children 4a33becb6f59
files libraries/json/json.c
line diff
     1.1 --- a/libraries/json/json.c	Thu Jan 14 18:01:33 2016 +0100
     1.2 +++ b/libraries/json/json.c	Thu Jan 14 18:43:11 2016 +0100
     1.3 @@ -1121,8 +1121,14 @@
     1.4        if (isinf(num)) return luaL_error(L, "JSON export not possible for infinite numbers");
     1.5        // check if float is integral:
     1.6        if (trunc(num) == num) {
     1.7 -        // if yes, use maximum precision:
     1.8 -        sprintf(numstr, "%.17g", num);  // NOTE: e.g. 12345678901234560
     1.9 +        // avoid exponential notation for INT64's represented as float:
    1.10 +        if (fabs(num) < 1e19) {
    1.11 +          // use decimal notation:
    1.12 +          sprintf(numstr, "%.0f", num);
    1.13 +        } else {
    1.14 +          // use maximum precision:
    1.15 +          sprintf(numstr, "%.17g", num);  // NOTE: e.g. 12345678901234560
    1.16 +        }
    1.17        } else {
    1.18          // determine necessary precision to represent double precision floating point number:
    1.19          sprintf(numstr, "%.15g", num);  // NOTE: e.g. 0.009 should not be 0.008999999999999999

Impressum / About Us