Math - AlsoGhostglowDev/Ghost-s-Utilities GitHub Wiki

lua-addons/math.lua

An addon to Lua's standard math library.

This addon can be declared with the name "math" since it does NOT override the Lua's standard math library.

local math = require 'ghostutil.lua-addons.math'

Fields

math.epsilon = 1e-12
Epsilon, two numbers that are compared with this small of a difference would be considered equal, used in math.equals.
math.max_float = 1.79e308
Maximum value of a floating-point number.
math.max_int = 0x7FFFFFFF
Maximum value of an integer.
math.min_float = 4.94e-324
Minimum value of floating-point number before it reaches 0.
math.min_int = -0x7FFFFFFF
Minimum value of an integer.
math.infinity = math.huge
Lua's definition of infinity.
math.negative_infinity = -math.huge
The negative counterpart of math.infinity.
math.nan = 0/0
Lua's definition of NaN (not a number).

Methods

lerp(a: number, b: number, ratio: number): number

Linear interpolation, a mathematical function that finds a point between two values (a and b) based on a the ratio value; a value between 0 and 1.

Parameters:

  • a: The starting value.
  • b: The end value.
  • ratio: The interpolation factor, ranges from 0 to 1.

Returns: The end-product calculated.

─────────────────────────

fpslerp(a: number, b: number, ratio: number): number

Similar to math.lerp, this function instead relies on the current game FPS.

Parameters:

  • a: The starting value.
  • b: The end value.
  • ratio: The interpolation factor, ranges from 0 to 1.

Returns: The end-product calculated with taking the game's FPS into account.

─────────────────────────

isfinite(n: number): boolean

Checks if the given number is finite. Defined through when the passed value is not infinity and Not a Number (NaN).

Parameters:

  • n: The number to check.

Returns: true if the number is finite.

─────────────────────────

isnan(n: number): boolean

Checks if the given number is Not a Number (NaN). Defined through if the type itself a number but does not equal to itself.
(Shortcut to util.isnan)

Parameters:

  • n: The number to check.

Returns: true if the number is Not a Number.

─────────────────────────

invert(n: number): number

Swaps out the given number's sign.

Parameters:

  • n: The number to invert.

Returns: The inverted number.

─────────────────────────

ispositive(n: number): boolean

Checks if the given number is a positive.

Parameters:

  • n: The number to check.

Returns: If the number is positive.
Information: If the number passed is 0, false is returned.

─────────────────────────

isnegative(n: number): boolean

Checks if the given number is negative.

Parameters:

  • n: The number to check.

Returns: If the number is negative.
Information: If the number passed is 0, false is returned.

─────────────────────────

factorial(n: int): boolean

Returns the factorial of the given number.

Parameters:

  • n: The integer to compute the factorial of.

Returns: The factorial of the given number.

─────────────────────────

bound(n: number, min: number, max: number): number

Bounds the given number in a specific range set by min and max from going less/over the set limit.
(Aliases: math.clamp)

Parameters:

  • n: The number to bound.
  • min: The minimum limit.
  • max: The maximum limit.

Returns: The bounded number.

─────────────────────────

floordecimal(n: number, ?decimals: number): number

Limits the decimals of the given number to the specified value.

Parameters:

  • n: The number.
  • decimals (optional): The decimals the number should have, defaults to 2.

Returns: The number.

─────────────────────────

round(n: number): number

Rounds the given number to the nearest integer.

Parameters:

  • n: The number to be rounded.

Returns: The rounded number.

─────────────────────────

midpoint(a: number, b: number): number

Finds the midpoint between two points.

Parameters:

  • a: The first point.
  • b: The second point.

Returns: The midpoint of the given points.

─────────────────────────

distance(x1: number, y1: number, x2: number, y2: number): number

Finds the distance between two points in a two-dimensional space.

Parameters:

  • (x1, y1): The coordinates of the first point.
  • (x2, y2): The coordinates of the second point.

Returns: The distance between two points.

─────────────────────────

distance3(x1: number, y1: number, z1: number, x2: number, y2: number, z2: number): number

Finds the distance between two points in a three-dimensional space.

Parameters:

  • (x1, y1, z1): The coordinates of the first point.
  • (x2, y2, z2): The coordinates of the second point.

Returns: The distance between two points.

─────────────────────────

dot(ax: number, ay: number, bx: number, by: number): number

Computes the dot product of two 2D vectors.

Parameters:

  • (ax, ay): Components of the first vector.
  • (bx, by): Components of the second vector.

Returns: The dot product of the two vectors.

─────────────────────────

dot3(ax: number, ay: number, az: number, bx: number, by: number, bz: number): number

Computes the dot product of two 3D vectors.

Parameters:

  • (ax, ay, az): Components of the first vector.
  • (bx, by, bz): Components of the second vector.

Returns: The dot product of the two vectors.

─────────────────────────

equals(a: number, b: number, ?diff: number): boolean

Checks if the two values (a and b) are equal to each other by using epsilon (or diff) for a more loose equal check than a literal equal-to operator.

Parameters:

  • a: The first value.
  • b: The second value.
  • diff (optional): The minimum difference between the two numbers until it should be considered equal to each other. Defaults to math.epsilon.

─────────────────────────

area(w: number, h: number): number

The area of the given dimensions.

Parameters:

  • w: The width.
  • h: The height.

Returns: The area based on the given values.

─────────────────────────

volume(w: number, h: number, l: number): number

The volume of the given dimensions.

Parameters:

  • w: The width.
  • h: The height.
  • l: The length.

Returns: The volume based on the given values.

─────────────────────────

inbounds(n: number, min: number, max: number): boolean

Checks if the given number is in the bounds of the specified limit.

Parameters:

  • n: The number to check.
  • min: The minimum bound.
  • max: The maximum bound.

Returns: true if the given number is in bounds.

─────────────────────────

iseven(n: number): boolean

Checks if the passed number is even.

Parameters:

  • n: The number to check.

Returns: true if even.

─────────────────────────

isodd(n: number): boolean

Checks if the passed number is odd

Parameters:

  • n: The number to check.

Returns: true if odd.

─────────────────────────

boundedadd(n: number, a: number, min: number, max: number): number

Performs an arithmetic addition of n to a while keeping it in the given bounds.

Parameters:

  • n: The number.
  • a: The number to add to n.
  • min: The minimum limit.
  • max: The maximum limit.

Returns: The bounded number.

─────────────────────────

samesign(a: number, b: number): number

Checks if the passed values both use the same sign.
(using math.signof)

Parameters:

  • a: The first value.
  • b: The second value to compare.

Returns: true if both values has the same sign.

─────────────────────────

signof(n: number): number

Returns -1 or 1 based on what sign n has.

Parameters:

  • n: The number to check

Returns: -1 if negative, 1 if positive or 0.



GhostUtil 3.0.0Docs 3.0.0, Revision 1

a Lua Library made by GhostglowDev; for Psych Engine
© 2025 GhostglowDevGhost's Utilities
Licensed under the MIT License.

⚠️ **GitHub.com Fallback** ⚠️