Mathematical Library

Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Aug 15, 2023
Contribute to Docs

The Mathematical Library holds Lua built-in functions that relate to math. These math functions include those relating to trigonometry (like sin and cos), exponents (exp or ^), logs (logN), rounding (floor, ceil), max, min, and pi.

Math Functions

Lua’s Mathematical Library contains various math functionalities, but here are a few most commonly used functions:

  • math.max(x, ..): Returns the max value from arguments.
  • math.min(x, ..): Returns the min value from arguments.
  • math.pow(x, y): Returns x to the y power (x^y).
  • math.random(a, ...): Returns a random integer between 0 and 1 when no arguments are given, a random integer from 1 to a when one argument is given, or a random integer between a and b when two arguments are given.
  • math.sqrt(x): Returns square root of x (√x).

A full list of math functions can be found on the Lua documentation page.

All contributors

Looking to contribute?

Learn Lua on Codecademy