Floats formatted with "correct" precision · lua/lua@1bf4b80 · GitHub
Skip to content

Commit 1bf4b80

Browse files
committed
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".
1 parent 4c6afbc commit 1bf4b80

3 files changed

Lines changed: 153 additions & 21 deletions

File tree

‎lobject.c‎

Lines changed: 39 additions & 13 deletions

‎luaconf.h‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,13 @@
416416
@@ l_floatatt(x) corrects float attribute 'x' to the proper float type
417417
** by prefixing it with one of FLT/DBL/LDBL.
418418
@@ LUA_NUMBER_FRMLEN is the length modifier for writing floats.
419-
@@ LUA_NUMBER_FMT is the format for writing floats.
420-
@@ lua_number2str converts a float to a string.
419+
@@ LUA_NUMBER_FMT is the format for writing floats with the maximum
420+
** number of digits that respects tostring(tonumber(numeral)) == numeral.
421+
** (That would be floor(log10(2^n)), where n is the number of bits in
422+
** the float mantissa.)
423+
@@ LUA_NUMBER_FMT_N is the format for writing floats with the minimum
424+
** number of digits that ensures tonumber(tostring(number)) == number.
425+
** (That would be LUA_NUMBER_FMT+2.)
421426
@@ l_mathop allows the addition of an 'l' or 'f' to all math operations.
422427
@@ l_floor takes the floor of a float.
423428
@@ lua_str2number converts a decimal numeral to a number.
@@ -428,8 +433,6 @@
428433

429434
#define l_floor(x) (l_mathop(floor)(x))
430435

431-
#define lua_number2str(s,sz,n) \
432-
l_sprintf((s), sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)(n))
433436

434437
/*
435438
@@ lua_numbertointeger converts a float number with an integral value
@@ -458,6 +461,7 @@
458461

459462
#define LUA_NUMBER_FRMLEN ""
460463
#define LUA_NUMBER_FMT "%.7g"
464+
#define LUA_NUMBER_FMT_N "%.9g"
461465

462466
#define l_mathop(op) op##f
463467

@@ -474,6 +478,7 @@
474478

475479
#define LUA_NUMBER_FRMLEN "L"
476480
#define LUA_NUMBER_FMT "%.19Lg"
481+
#define LUA_NUMBER_FMT_N "%.21Lg"
477482

478483
#define l_mathop(op) op##l
479484

@@ -488,7 +493,8 @@
488493
#define LUAI_UACNUMBER double
489494

490495
#define LUA_NUMBER_FRMLEN ""
491-
#define LUA_NUMBER_FMT "%.14g"
496+
#define LUA_NUMBER_FMT "%.15g"
497+
#define LUA_NUMBER_FMT_N "%.17g"
492498

493499
#define l_mathop(op) op
494500

‎testes/math.lua‎

Lines changed: 103 additions & 3 deletions

0 commit comments

Comments
 (0)