Lua: Math and Variables


There are three kinds of variables in Lua:

  1. global variables
  2. local variables
  3. table fields

Example: Basic math with variables


	x = 10
	y = 5
	total = x + y
	print("Total: ", total)
	

Output:

  Total:   15


Example: Math libary and Absolute Value


	x = -10
	print("Absolute value of x: ", math.abs(x))
	

Output:

  Absolute value of x:    10


The math library contains many functions.

  • math.abs
  • math.acos
  • math.asin
  • math.atan
  • math.ceil
  • math.cos
  • math.deg
  • math.exp
  • math.floor
  • math.fmod
  • math.huge
  • math.log
  • math.max
  • math.maxinteger
  • math.min
  • math.mininteger
  • math.modf
  • math.pi
  • math.rad
  • math.random
  • math.randomseed
  • math.sin
  • math.sqrt
  • math.tan
  • math.tointeger
  • math.type
  • math.ult

Example: More Basic Math and Comments/Remarks

The double hyphen (--) is the symbols to create a comment/remark in Lua.


	-- This is a sample Lua program with math
	print("5 plus 4 is: " .. 5+4)
	print("2 times 4.4 is: " .. 2*4.4)
	

Output:

  5 plus 4 is: 9
  2 times 4.4 is: 8.8