Skip to content

`Lua Addons: Math

GhostglowDev edited this page Mar 21, 2024 · 2 revisions
local math = require "ghostutil.lua-addons.math"

See source code

Variables

  • epsilon = 0.0000001
  • max_int = 0x7FFFFFFF
  • min_int = -0x7FFFFFFF
  • max_float = 1.79e+308
  • min_float = 0.0000000001
  • imaginary = i
  • infinity = inf
  • negative_infinity = -inf

Functions

lerp(a:Float, b:Float, ratio:Float) -> Float

Linear Interpolation

  • a: From
  • b: Target
  • ratio: Ratio, duh. (From 0 to 1)

Example:

function onUpdate(elapsed)
    -- Example is currently unavailable
end

boundto(value:Float, max:Float, min:Float) -> Float

Bound a number by a minimum and maximum. Ensures that this number is no smaller than the minimum, and no larger than the maximum

  • value: Value
  • max: Maximum
  • min: Minimum

Example:

debugPrint(math.boundto(10, 7, 1))
-- prints 7

invert(n:Float) -> Float

Inverts the number.

  • n: To invert

Example:

math.invert(-1)
-- returns 1 (Basically like absolute)

math.invert(1)
-- returns -1

ispositive(x:Float) / isnegative(x:Float) -> Float

Checks if x is negative or positive (If x equals 0 then it will return false because 0 is not positive nor negative)

  • x: To check

Example:

math.ispositive(1)
-- returns true

math.isnegative(1)
-- returns false

math.ispositive(0)
-- returns false

fact(n:Int) -> Int

Finds the factorial of n (Only integers are allowed. Floats shows wrong results)

  • n: To be factored

Example:

math.fact(4)
-- returns 24

math.fact(0)
-- returns 1

floordecimal(value:Float, decimals:Int) -> Float

Limit the value's decimals

  • value: Value
  • decimals: Total decimals you want in the new value

Example:

local accuracy = rating*100

accuracy = math.floordecimal(accuracy, 2);
-- if the accuracy is for example, 79.11293283
-- it now shows 79.11

round(value:Float) -> Float

Rounds to the closest number

  • value: Value

Example:

math.round(5.5)
-- returns 6

math.round(5.23)
-- returns 5

getmidpoint(a:Float, b:Float) -> Float

Returns the center / midpoint of a certain distance

It's so self-explainatory that I won't explain it

Example:

local x = 10
local x2 = 0
local x3 = math.getmidpoint(x, x2)

-- x3 returns 5