From 7f5c31cdcac5388b3c48a26112dfb6d2cadb7321 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 23 Jul 2019 12:46:33 -0300 Subject: Fixed bug in 'string.format' with option '%f' As an example, 'print(string.format("%.99f", 1e70))' may have a lot of garbage after the number. The old test to ensure that 'string.format("%.99f", n)' was not too large, 'fabs(n) < 1e100', assumes that the number will fit in the 99 bytes; but the 99 is not the space for the number, it is the added extra zeros. The option worked for smaller numbers because of the extra space added to MAX_ITEM. --- testes/strings.lua | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'testes') diff --git a/testes/strings.lua b/testes/strings.lua index 2540fdef..0e7874bf 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -255,6 +255,12 @@ do -- longest number that can be formatted local s = string.format('%.99f', -(10^i)) assert(string.len(s) >= i + 101) assert(tonumber(s) == -(10^i)) + + -- limit for floats + assert(10^38 < math.huge) + local s = string.format('%.99f', -(10^38)) + assert(string.len(s) >= 38 + 101) + assert(tonumber(s) == -(10^38)) end -- cgit v1.2.3-55-g6feb