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
|
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
|
||||||
|
Loading…
Reference in New Issue
Block a user