diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-08-02 15:09:30 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-08-02 15:09:30 -0300 |
commit | 1bf4b80f1ace8384eb9dd6f7f8b67256b3944a7a (patch) | |
tree | 5162b1f662c4dcb6127ba03866096f70d0c298ab /luaconf.h | |
parent | 4c6afbcb01d1cae72d829af5301df5f592fa2079 (diff) | |
download | lua-1bf4b80f1ace8384eb9dd6f7f8b67256b3944a7a.tar.gz lua-1bf4b80f1ace8384eb9dd6f7f8b67256b3944a7a.tar.bz2 lua-1bf4b80f1ace8384eb9dd6f7f8b67256b3944a7a.zip |
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".
Diffstat (limited to 'luaconf.h')
-rw-r--r-- | luaconf.h | 16 |
1 files changed, 11 insertions, 5 deletions
@@ -416,8 +416,13 @@ | |||
416 | @@ l_floatatt(x) corrects float attribute 'x' to the proper float type | 416 | @@ l_floatatt(x) corrects float attribute 'x' to the proper float type |
417 | ** by prefixing it with one of FLT/DBL/LDBL. | 417 | ** by prefixing it with one of FLT/DBL/LDBL. |
418 | @@ LUA_NUMBER_FRMLEN is the length modifier for writing floats. | 418 | @@ LUA_NUMBER_FRMLEN is the length modifier for writing floats. |
419 | @@ LUA_NUMBER_FMT is the format for writing floats. | 419 | @@ LUA_NUMBER_FMT is the format for writing floats with the maximum |
420 | @@ lua_number2str converts a float to a string. | 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.) | ||
421 | @@ l_mathop allows the addition of an 'l' or 'f' to all math operations. | 426 | @@ l_mathop allows the addition of an 'l' or 'f' to all math operations. |
422 | @@ l_floor takes the floor of a float. | 427 | @@ l_floor takes the floor of a float. |
423 | @@ lua_str2number converts a decimal numeral to a number. | 428 | @@ lua_str2number converts a decimal numeral to a number. |
@@ -428,8 +433,6 @@ | |||
428 | 433 | ||
429 | #define l_floor(x) (l_mathop(floor)(x)) | 434 | #define l_floor(x) (l_mathop(floor)(x)) |
430 | 435 | ||
431 | #define lua_number2str(s,sz,n) \ | ||
432 | l_sprintf((s), sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)(n)) | ||
433 | 436 | ||
434 | /* | 437 | /* |
435 | @@ lua_numbertointeger converts a float number with an integral value | 438 | @@ lua_numbertointeger converts a float number with an integral value |
@@ -458,6 +461,7 @@ | |||
458 | 461 | ||
459 | #define LUA_NUMBER_FRMLEN "" | 462 | #define LUA_NUMBER_FRMLEN "" |
460 | #define LUA_NUMBER_FMT "%.7g" | 463 | #define LUA_NUMBER_FMT "%.7g" |
464 | #define LUA_NUMBER_FMT_N "%.9g" | ||
461 | 465 | ||
462 | #define l_mathop(op) op##f | 466 | #define l_mathop(op) op##f |
463 | 467 | ||
@@ -474,6 +478,7 @@ | |||
474 | 478 | ||
475 | #define LUA_NUMBER_FRMLEN "L" | 479 | #define LUA_NUMBER_FRMLEN "L" |
476 | #define LUA_NUMBER_FMT "%.19Lg" | 480 | #define LUA_NUMBER_FMT "%.19Lg" |
481 | #define LUA_NUMBER_FMT_N "%.21Lg" | ||
477 | 482 | ||
478 | #define l_mathop(op) op##l | 483 | #define l_mathop(op) op##l |
479 | 484 | ||
@@ -488,7 +493,8 @@ | |||
488 | #define LUAI_UACNUMBER double | 493 | #define LUAI_UACNUMBER double |
489 | 494 | ||
490 | #define LUA_NUMBER_FRMLEN "" | 495 | #define LUA_NUMBER_FRMLEN "" |
491 | #define LUA_NUMBER_FMT "%.14g" | 496 | #define LUA_NUMBER_FMT "%.15g" |
497 | #define LUA_NUMBER_FMT_N "%.17g" | ||
492 | 498 | ||
493 | #define l_mathop(op) op | 499 | #define l_mathop(op) op |
494 | 500 | ||