-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create custom Lua library for enhanced math functions with index
- Loading branch information
1 parent
e067201
commit 130c9c7
Showing
1 changed file
with
20 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
-- Create a table named supermath to store our custom functions. | ||
local supermath = {} | ||
|
||
-- Set the metatable of supermath to the standard math module. | ||
setmetatable(supermath, { | ||
__index = math -- This allows supermath to access functions from the math module. | ||
}) | ||
|
||
-- Custom function to round a number x. | ||
function supermath.round(x) | ||
return math.floor(x + 0.5) -- Round the number to the nearest integer. | ||
end | ||
|
||
-- Examples of usage: | ||
print(supermath.floor(2.3)) -- This calls the floor function from the standard math module. | ||
print(supermath.floor(2.7)) -- This also calls the floor function from the standard math module. | ||
|
||
-- Now let's use our custom rounding function. | ||
print(supermath.round(2.3)) -- OUTPUT: Will round to 2. | ||
print(supermath.round(2.7)) -- OUTPUT: Will round to 3. |