Floats formatted with "correct" precision

Conversion float->string ensures that, for any float f,
tonumber(tostring(f)) == f, but still avoiding noise like 1.1
converting to "1.1000000000000001".
This commit is contained in:
Roberto Ierusalimschy
2024-08-02 15:09:30 -03:00
parent 4c6afbcb01
commit 1bf4b80f1a
3 changed files with 153 additions and 21 deletions

View File

@@ -416,8 +416,13 @@
@@ l_floatatt(x) corrects float attribute 'x' to the proper float type
** by prefixing it with one of FLT/DBL/LDBL.
@@ LUA_NUMBER_FRMLEN is the length modifier for writing floats.
@@ LUA_NUMBER_FMT is the format for writing floats.
@@ lua_number2str converts a float to a string.
@@ LUA_NUMBER_FMT is the format for writing floats with the maximum
** number of digits that respects tostring(tonumber(numeral)) == numeral.
** (That would be floor(log10(2^n)), where n is the number of bits in
** the float mantissa.)
@@ LUA_NUMBER_FMT_N is the format for writing floats with the minimum
** number of digits that ensures tonumber(tostring(number)) == number.
** (That would be LUA_NUMBER_FMT+2.)
@@ l_mathop allows the addition of an 'l' or 'f' to all math operations.
@@ l_floor takes the floor of a float.
@@ lua_str2number converts a decimal numeral to a number.
@@ -428,8 +433,6 @@
#define l_floor(x) (l_mathop(floor)(x))
#define lua_number2str(s,sz,n) \
l_sprintf((s), sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)(n))
/*
@@ lua_numbertointeger converts a float number with an integral value
@@ -458,6 +461,7 @@
#define LUA_NUMBER_FRMLEN ""
#define LUA_NUMBER_FMT "%.7g"
#define LUA_NUMBER_FMT_N "%.9g"
#define l_mathop(op) op##f
@@ -474,6 +478,7 @@
#define LUA_NUMBER_FRMLEN "L"
#define LUA_NUMBER_FMT "%.19Lg"
#define LUA_NUMBER_FMT_N "%.21Lg"
#define l_mathop(op) op##l
@@ -488,7 +493,8 @@
#define LUAI_UACNUMBER double
#define LUA_NUMBER_FRMLEN ""
#define LUA_NUMBER_FMT "%.14g"
#define LUA_NUMBER_FMT "%.15g"
#define LUA_NUMBER_FMT_N "%.17g"
#define l_mathop(op) op