Skip to content

Commit

Permalink
factorial(n) function (#3253)
Browse files Browse the repository at this point in the history
* factorial(n) function

* Use local function

* Woops

* Return NaN if the number is less than 0

* Update function

Co-authored-by: thegrb93 <[email protected]>

---------

Co-authored-by: thegrb93 <[email protected]>
  • Loading branch information
Astralcircle and thegrb93 authored Jan 30, 2025
1 parent 158ec27 commit c75ee43
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lua/entities/gmod_wire_expression2/core/number.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions lua/wire/client/e2descriptions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit c75ee43

Please sign in to comment.