mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-22 07:13:45 +01:00
Add special value support to math.tostring
This commit is contained in:
parent
9ced0f6d5c
commit
27e91df7dd
13
math.lua
13
math.lua
@ -2,6 +2,8 @@
|
||||
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
|
||||
|
||||
local inf = math.huge
|
||||
|
||||
-- Set environment
|
||||
local _ENV = {}
|
||||
setfenv(1, _ENV)
|
||||
@ -30,6 +32,15 @@ default_precision = 10
|
||||
|
||||
-- See https://github.com/appgurueu/Luon/blob/master/index.js#L724
|
||||
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
|
||||
precision = precision or default_precision
|
||||
local out = {}
|
||||
@ -77,7 +88,7 @@ function fround(number)
|
||||
local leading = exp < -127 and 0 or 1
|
||||
local mantissa = math_floor((leading - number / powexp) * 0x800000 + 0.5)
|
||||
if mantissa <= -0x800000 then
|
||||
return sign * math.huge
|
||||
return sign * inf
|
||||
end
|
||||
return sign * powexp * (leading - mantissa / 0x800000)
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user