Skip to content

Instantly share code, notes, and snippets.

@appgurueu
Created January 3, 2023 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save appgurueu/44eae009b4b4d6c48af606647985578b to your computer and use it in GitHub Desktop.
Save appgurueu/44eae009b4b4d6c48af606647985578b to your computer and use it in GitHub Desktop.
Compact but hacky and inefficient float number to string using a binary search on the precision
-- Lookup table for format strings
local formats = {}
for i = 1, 17 do
formats[i] = ("%%.%dg"):format(i)
end
local function _number_to_string(num, min_digits, max_digits)
if min_digits > max_digits then
return
end
local mid = (min_digits + max_digits) // 2
local formatted = formats[mid]:format(num)
if tonumber(formatted) == num then
return _number_to_string(num, min_digits, mid - 1) or formatted
end
return _number_to_string(num, mid + 1, max_digits)
end
local function number_to_string(num)
return assert(_number_to_string(num, 1, #formats))
end
-- Tests
for _ = 1, 1e3 do
local x = math.random(0, 2^53 - 1) * 2^math.random(-1e3, 1e3 - 30)
assert(tonumber(number_to_string(x)) == x)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment