forked from Mirrorlandia_minetest/minetest
Add math.factorial (#8298)
This commit is contained in:
parent
250420e566
commit
a7c5dc50e5
@ -243,6 +243,20 @@ function math.sign(x, tolerance)
|
|||||||
return 0
|
return 0
|
||||||
end
|
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)
|
function get_last_folder(text,count)
|
||||||
local parts = text:split(DIR_DELIM)
|
local parts = text:split(DIR_DELIM)
|
||||||
|
@ -2378,6 +2378,7 @@ Helper functions
|
|||||||
* tolerance: number, default: `0.0`
|
* tolerance: number, default: `0.0`
|
||||||
* If the absolute value of `x` is within the `tolerance` or `x` is NaN,
|
* If the absolute value of `x` is within the `tolerance` or `x` is NaN,
|
||||||
`0` is returned.
|
`0` is returned.
|
||||||
|
* `math.factorial(x)`: returns the factorial of `x`
|
||||||
* `string.split(str, separator, include_empty, max_splits, sep_is_pattern)`
|
* `string.split(str, separator, include_empty, max_splits, sep_is_pattern)`
|
||||||
* `separator`: string, default: `","`
|
* `separator`: string, default: `","`
|
||||||
* `include_empty`: boolean, default: `false`
|
* `include_empty`: boolean, default: `false`
|
||||||
|
Loading…
Reference in New Issue
Block a user