mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-22 23:33:53 +01:00
Improve performance
This commit is contained in:
parent
90c3b0ed90
commit
28eeb4ccf8
34
math.lua
34
math.lua
@ -1,5 +1,6 @@
|
|||||||
-- Localize globals
|
-- Localize globals
|
||||||
local math, minetest, modlib, os, string, table = math, minetest, modlib, os, string, table
|
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
|
||||||
|
|
||||||
-- Set environment
|
-- Set environment
|
||||||
local _ENV = {}
|
local _ENV = {}
|
||||||
@ -14,15 +15,15 @@ positive_nan = negative_nan ^ 1
|
|||||||
|
|
||||||
function round(number, steps)
|
function round(number, steps)
|
||||||
steps = steps or 1
|
steps = steps or 1
|
||||||
return math.floor(number * steps + 0.5) / steps
|
return math_floor(number * steps + 0.5) / steps
|
||||||
end
|
end
|
||||||
|
|
||||||
local c0 = ("0"):byte()
|
local c0 = ("0"):byte()
|
||||||
local cA = ("A"):byte()
|
local cA = ("A"):byte()
|
||||||
|
|
||||||
function default_digit_function(digit)
|
function default_digit_function(digit)
|
||||||
if digit <= 9 then return string.char(c0 + digit) end
|
if digit <= 9 then return string_char(c0 + digit) end
|
||||||
return string.char(cA + digit - 10)
|
return string_char(cA + digit - 10)
|
||||||
end
|
end
|
||||||
|
|
||||||
default_precision = 10
|
default_precision = 10
|
||||||
@ -33,31 +34,32 @@ function tostring(number, base, digit_function, precision)
|
|||||||
precision = precision or default_precision
|
precision = precision or default_precision
|
||||||
local out = {}
|
local out = {}
|
||||||
if number < 0 then
|
if number < 0 then
|
||||||
table.insert(out, "-")
|
table_insert(out, "-")
|
||||||
number = -number
|
number = -number
|
||||||
end
|
end
|
||||||
|
-- Rounding
|
||||||
number = number + base ^ -precision / 2
|
number = number + base ^ -precision / 2
|
||||||
local digit
|
local digit
|
||||||
while number >= base do
|
while number >= base do
|
||||||
digit = math.floor(number % base)
|
digit = math_floor(number % base)
|
||||||
table.insert(out, digit_function(digit))
|
table_insert(out, digit_function(digit))
|
||||||
number = (number - digit) / base
|
number = (number - digit) / base
|
||||||
end
|
end
|
||||||
digit = math.floor(number)
|
digit = math_floor(number)
|
||||||
table.insert(out, digit_function(digit))
|
table_insert(out, digit_function(digit))
|
||||||
modlib.table.reverse(out)
|
modlib_table_reverse(out)
|
||||||
number = number % 1
|
number = number % 1
|
||||||
if number ~= 0 and number >= base ^ -precision then
|
if number ~= 0 and number >= base ^ -precision then
|
||||||
table.insert(out, ".")
|
table_insert(out, ".")
|
||||||
while precision >= 0 and number >= base ^ -precision do
|
while precision >= 0 and number >= base ^ -precision do
|
||||||
number = number * base
|
number = number * base
|
||||||
digit = math.floor(number % base)
|
digit = math_floor(number % base)
|
||||||
table.insert(out, digit_function(digit))
|
table_insert(out, digit_function(digit))
|
||||||
number = number - digit
|
number = number - digit
|
||||||
precision = precision - 1
|
precision = precision - 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return table.concat(out)
|
return table_concat(out)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/fround#polyfill
|
-- See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/fround#polyfill
|
||||||
@ -70,10 +72,10 @@ function fround(number)
|
|||||||
sign = -1
|
sign = -1
|
||||||
number = -number
|
number = -number
|
||||||
end
|
end
|
||||||
local exp = math.floor(math.log(number, 2))
|
local exp = math_floor(math.log(number, 2))
|
||||||
local powexp = 2 ^ math.max(-126, math.min(exp, 127))
|
local powexp = 2 ^ math.max(-126, math.min(exp, 127))
|
||||||
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 * math.huge
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user