Math library for Lua

math = {}

math.pi = 3.14159265358979323846
math.huge = 1.7976931348623157E+308

math.abs = function(x)
  if x >= 0 then
    return x
  else
    return -x
  end
end

math.acos = function(x)
  if x > 1 or x < -1 then
    return nil
  end
  return math.atan2(math.sqrt(1 - x * x), x)
end

math.asin = function(x)
  if x > 1 or x < -1 then
    return nil
  end
  return math.atan2(x, math.sqrt(1 - x * x))
end

math.atan = function(x)
  return math.atan2(x, 1)
end

math.atan2 = function(y, x)
  if x > 0 then
    return math.atan(y / x)
  elseif x < 0 and y >= 0 then
    return math.atan(y / x) + math.pi
  elseif x < 0 and y < 0 then
    return math.atan(y / x) - math.pi
  elseif x == 0 and y > 0 then
    return math.pi / 2
  elseif x == 0 and y < 0 then
    return -math.pi / 2
  else
    return nil
  end
end

math.ceil = function(x)
  local f = math.floor(x)
  if x == f then
    return f
  else
    return f + 1
  end
end

math.cos = function(x)
  return math.sin(x + math.pi / 2)
end

math.cosh = function(x)
  return (math.exp(x) + math.exp(-x)) / 2
end

math.deg = function(x)
  return x * 180 / math.pi
end

math.exp = function(x)
  if x == -math.huge then
    return 0
  end
  local sum = 1
  local term = 1
  for i = 1, 100 do
    term = term * x / i
    sum = sum + term
    if term / sum == 1 then
      break
    end
  end
  return sum
end

math.floor = function(x)
  if x >= 0 then
    return math.modf(x)
  else
    local f = math.modf(x)
    if f == x then
      return f
    else
      return f - 1
    end
  end
end

math.fmod = function(x, y)
  if y == 0 then
    return x
  end
  local f = math.modf(x / y)
  return x - f * y
end

math.frexp = function(x)
  if x == 0 then
    return 0, 0
  end
  local ex = 0
  local f
  if x < 0 then
    x = -x
    f = -1
  else
    f = 1
  end
  while x >= 2 do
    x = x / 2
    ex = ex + 1
  end
  while x < 1 do
    x = x * 2
    ex = ex - 1
  end
  return x, ex
end

math.ldexp = function(x, exp)
  return x * 2 ^ exp
end

math.log = function(x)
  return math.log10(x) / math.log10(math.exp(1))
end

math.log10 = function(x)
  if x <= 0 then
    return nil
  end
  local y = math.log(x) / math.log(10)
  local f = math.floor(y)
  local sum = 1
  local term = 1
  local k = 1
  while k <= 10 do
    term = term * y / k
    sum = sum + term
    if term / sum == 1 then
      break
    end
    k = k + 1
  end
  return sum + f
end

math.max = function(x, y)
  if x > y then
    return x
  else
    return y
  end
end

math.min = function(x, y)
  if x < y then
    return x
  else
    return y
  end
end

math.mod = function(x, y)
  return math.fmod(x, y)
end

math.modf = function(x)
  local f = math.floor(x)
  return f, x - f
end

math.pow = function(x, y)
  return x ^ y
end

math.rad = function(x)
  return x * math.pi / 180
end

math.random = function(m, n)
  if m == nil then
    return math.random()
  elseif n == nil then
    return math.random() * m
  else
    return math.random() * (n - m) + m
  end
end

math.randomseed = function(x)
  return math.random()
end

math.sin = function(x)
  return math.sinh(x) / math.cosh(x)
end

math.sinh = function(x)
  return (math.exp(x) - math.exp(-x)) / 2
end

math.sqrt = function(x)
  if x < 0 then
    return nil
  end
  local y = x
  while true do
    local z = y
    y = (y + x / y) / 2
    if z == y then
      break
    end
  end
  return y
end

math.tan = function(x)
  return math.sinh(x) / math.cosh(x)
end

math.tanh = function(x)
  return math.sinh(2 * x) / (math.cosh(2 * x) + 1)
end

This script creates a math library with all the math functions and constants in Lua.

8 Likes

Could you put it in a module script, and just add return math?

If so, then good job! :slight_smile:

2 Likes

This just provides the same functionality that the math global does? I don’t see the point of this unless you want to modify the behavior a bit.

2 Likes

This is helpful for understanding how the builtin Lua math functions work but otherwise serves no purpose in any actual game.

1 Like