Created
January 3, 2023 21:52
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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