Page:
Math Library
1
Math Library
Spencer Conlon edited this page 2024-12-28 17:28:20 +00:00
Math Library Wiki
Overview
The math.lua
library provides a collection of math constants, basic math functions, and trigonometric utilities to simplify common mathematical operations in Lua.
Features
Constants
The library includes commonly used mathematical constants:
Constant | Description | Value |
---|---|---|
PI |
Ratio of a circle's circumference to its diameter | 3.14159 |
E |
Base of the natural logarithm | 2.71828 |
TAU |
Full circle in radians (2 * PI) | 6.28318 |
Functions
Basic Math Functions
These functions are available under Math.functions
:
Function | Description | Example Usage |
---|---|---|
square(x) |
Returns the square of x . |
Math.functions.square(5) → 25 |
cube(x) |
Returns the cube of x . |
Math.functions.cube(3) → 27 |
max(a, b) |
Returns the maximum of a and b . |
Math.functions.max(10, 20) → 20 |
min(a, b) |
Returns the minimum of a and b . |
Math.functions.min(10, 20) → 10 |
clamp(value, minValue, maxValue) |
Clamps value between minValue and maxValue . |
Math.functions.clamp(15, 0, 10) → 10 |
Trigonometric Functions
These functions are available under Math.trig
:
Function | Description | Example Usage |
---|---|---|
deg_to_rad(degrees) |
Converts degrees to radians. | Math.trig.deg_to_rad(180) → 3.14159 |
rad_to_deg(radians) |
Converts radians to degrees. | Math.trig.rad_to_deg(Math.constants.PI) → 180 |
Installation
- Place
math.lua
in your project directory. - Import the module using
require
:
local Math = require("math")
Example Usage
Here is an example script using the math.lua
library:
local Math = require("math")
-- Access constants
print("PI:", Math.constants.PI)
print("E:", Math.constants.E)
-- Use basic math functions
local result = Math.functions.square(5)
print("Square of 5:", result)
local clampedValue = Math.functions.clamp(15, 0, 10)
print("Clamped value:", clampedValue)
-- Use trigonometric functions
local radians = Math.trig.deg_to_rad(180)
print("180 degrees in radians:", radians)
local degrees = Math.trig.rad_to_deg(Math.constants.PI)
print("PI radians in degrees:", degrees)
Contributing
- Fork the repository.
- Create a new branch for your feature:
git checkout -b feature-name
. - Commit your changes:
git commit -m "Add feature"
. - Push to the branch:
git push origin feature-name
. - Open a pull request.
License
This library is released under the MIT License. See the LICENSE file for more details.