Fix negative offsets not being supported by container[]
[oweals/minetest.git] / builtin / common / misc_helpers.lua
index e250b0ed131425e702411e33cca856e1e389fe32..25632b4ca21754aa6ff805323c8e76d6e8b6a0ce 100644 (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)