From c75ee43ff12139556b94a1b607f090f26cdd967a Mon Sep 17 00:00:00 2001 From: Astralcircle <142503363+Astralcircle@users.noreply.github.com> Date: Thu, 30 Jan 2025 11:49:35 +0300 Subject: [PATCH] ```factorial(n)``` function (#3253) * factorial(n) function * Use local function * Woops * Return NaN if the number is less than 0 * Update function Co-authored-by: thegrb93 --------- Co-authored-by: thegrb93 --- lua/entities/gmod_wire_expression2/core/number.lua | 12 ++++++++++++ lua/wire/client/e2descriptions.lua | 1 + 2 files changed, 13 insertions(+) diff --git a/lua/entities/gmod_wire_expression2/core/number.lua b/lua/entities/gmod_wire_expression2/core/number.lua index eff2e31210..82aee7c394 100644 --- a/lua/entities/gmod_wire_expression2/core/number.lua +++ b/lua/entities/gmod_wire_expression2/core/number.lua @@ -303,6 +303,18 @@ end --[[************************************************************************]]-- +__e2setcost(10) + +[nodiscard] +e2function number factorial(number n) + if n < 0 then return 0 / 0 end + if n > 170 then return math.huge end + + local res = 1 + for i = 2, n do res = res * i end + return res +end + __e2setcost(2) -- approximation [nodiscard] diff --git a/lua/wire/client/e2descriptions.lua b/lua/wire/client/e2descriptions.lua index d0093edf3c..d2a153fde5 100644 --- a/lua/wire/client/e2descriptions.lua +++ b/lua/wire/client/e2descriptions.lua @@ -54,6 +54,7 @@ E2Helper.Descriptions["isinf(n)"] = "Returns 1 if given value is a positive infi E2Helper.Descriptions["isnan(n)"] = "Returns 1 if given value is not a number (NaN); otherwise 0." E2Helper.Descriptions["inf()"] = "Returns a huge constant (infinity)" E2Helper.Descriptions["mod(nn)"] = "Modulo, returns the Remainder after Argument 1 has been divided by Argument 2. Note \"mod(-1, 3) = -1\"" +E2Helper.Descriptions["factorial(n)"] = "Returns the Factorial of the Argument" E2Helper.Descriptions["sqrt(n)"] = "Returns the Square Root of the Argument" E2Helper.Descriptions["cbrt(n)"] = "Returns the Cube Root of the Argument" E2Helper.Descriptions["root(nn)"] = "Returns the Nth Root of the first Argument"