Add math.factorial (#8298)

This commit is contained in:
HybridDog 2019-03-05 10:11:21 +01:00 committed by Nils Dagsson Moskopp
parent f1a228902a
commit 744d7a14b4
Signed by: erle
GPG Key ID: A3BC671C35191080
1 changed files with 14 additions and 0 deletions

View File

@ -243,6 +243,20 @@ function math.sign(x, tolerance)
return 0
end
--------------------------------------------------------------------------------
function math.factorial(x)
assert(x % 1 == 0 and x >= 0, "factorial expects a non-negative integer")
if x >= 171 then
-- 171! is greater than the biggest double, no need to calculate
return math.huge
end
local v = 1
for k = 2, x do
v = v * k
end
return v
end
--------------------------------------------------------------------------------
function get_last_folder(text,count)
local parts = text:split(DIR_DELIM)