# HG changeset patch # User jbe # Date 1452790893 -3600 # Node ID 387a303918a6b94ae880c87ce6f00038db289693 # Parent 52ebde158c92091b5d376fa50c7bfc957ff7a3cc Handle special corner case for integral floats in json.export(...) diff -r 52ebde158c92 -r 387a303918a6 libraries/json/json.c --- a/libraries/json/json.c Thu Jan 14 17:40:49 2016 +0100 +++ b/libraries/json/json.c Thu Jan 14 18:01:33 2016 +0100 @@ -1119,10 +1119,16 @@ if (isnan(num)) return luaL_error(L, "JSON export not possible for NaN value"); // throw error if number is positive or negative infinity: if (isinf(num)) return luaL_error(L, "JSON export not possible for infinite numbers"); - // determine necessary precision to represent double precision floating point number: - sprintf(numstr, "%.15g", num); // NOTE: e.g. 0.009 should not be 0.008999999999999999 - if (strtod(numstr, NULL) != num) sprintf(numstr, "%.16g", num); - if (strtod(numstr, NULL) != num) sprintf(numstr, "%.17g", num); + // check if float is integral: + if (trunc(num) == num) { + // if yes, use maximum precision: + sprintf(numstr, "%.17g", num); // NOTE: e.g. 12345678901234560 + } else { + // determine necessary precision to represent double precision floating point number: + sprintf(numstr, "%.15g", num); // NOTE: e.g. 0.009 should not be 0.008999999999999999 + if (strtod(numstr, NULL) != num) sprintf(numstr, "%.16g", num); + if (strtod(numstr, NULL) != num) sprintf(numstr, "%.17g", num); + } // add string encoding of the number to the output buffer: luaL_addstring(&buf, numstr); #if LUA_VERSION_NUM >= 503