webmcp
changeset 191:e61bf25f3d04
Dynamic determination of decimal floating point precision in json.export(...)
author | jbe |
---|---|
date | Sun Aug 10 20:04:36 2014 +0200 (2014-08-10) |
parents | 2c0db49591be |
children | 33c8f7029cfa |
files | libraries/json/json.c |
line diff
1.1 --- a/libraries/json/json.c Sun Aug 10 19:48:09 2014 +0200 1.2 +++ b/libraries/json/json.c Sun Aug 10 20:04:36 2014 +0200 1.3 @@ -1093,8 +1093,12 @@ 1.4 if (isnan(num)) return luaL_error(L, "JSON export not possible for NaN value"); 1.5 // throw error if number is positive or negative infinity: 1.6 if (isinf(num)) return luaL_error(L, "JSON export not possible for infinite numbers"); 1.7 - // add Lua's string encoding of the number to the output buffer: 1.8 - sprintf(numstr, "%.17g", num); 1.9 + // determine necessary precision to represent double precision floating point number: 1.10 + sprintf(numstr, "%.14g", num); 1.11 + if (strtod(numstr, NULL) != num) sprintf(numstr, "%.15g", num); 1.12 + if (strtod(numstr, NULL) != num) sprintf(numstr, "%.16g", num); 1.13 + if (strtod(numstr, NULL) != num) sprintf(numstr, "%.17g", num); 1.14 + // add string encoding of the number to the output buffer: 1.15 luaL_addstring(&buf, numstr); 1.16 break; 1.17 // value to encode is of type boolean: