Add special value support to math.tostring

This commit is contained in:
Lars Mueller 2021-08-16 20:07:23 +02:00
parent 9ced0f6d5c
commit 27e91df7dd

@ -2,6 +2,8 @@
local math, math_floor, minetest, modlib_table_reverse, os, string_char, table, table_insert, table_concat local math, math_floor, minetest, modlib_table_reverse, os, string_char, table, table_insert, table_concat
= math, math.floor, minetest, modlib.table.reverse, os, string.char, table, table.insert, table.concat = math, math.floor, minetest, modlib.table.reverse, os, string.char, table, table.insert, table.concat
local inf = math.huge
-- Set environment -- Set environment
local _ENV = {} local _ENV = {}
setfenv(1, _ENV) setfenv(1, _ENV)
@ -30,6 +32,15 @@ default_precision = 10
-- See https://github.com/appgurueu/Luon/blob/master/index.js#L724 -- See https://github.com/appgurueu/Luon/blob/master/index.js#L724
function tostring(number, base, digit_function, precision) function tostring(number, base, digit_function, precision)
if number ~= number then
return "nan"
end
if number == inf then
return "inf"
end
if number == -inf then
return "-inf"
end
digit_function = digit_function or default_digit_function digit_function = digit_function or default_digit_function
precision = precision or default_precision precision = precision or default_precision
local out = {} local out = {}
@ -77,7 +88,7 @@ function fround(number)
local leading = exp < -127 and 0 or 1 local leading = exp < -127 and 0 or 1
local mantissa = math_floor((leading - number / powexp) * 0x800000 + 0.5) local mantissa = math_floor((leading - number / powexp) * 0x800000 + 0.5)
if mantissa <= -0x800000 then if mantissa <= -0x800000 then
return sign * math.huge return sign * inf
end end
return sign * powexp * (leading - mantissa / 0x800000) return sign * powexp * (leading - mantissa / 0x800000)
end end