← Back to Lua Course | Chapter 2: Variables & Types | Lesson 4 of 7

Numbers

Lua 5.3 has two kinds of numbers, whole-number integers and decimal floats, and mixes them for you.

In this page:

  1. Numbers

Numbers

Lua 5.3 has integer and float subtypes of the number type. Integers are 64-bit by default, and a value with a decimal point or exponent is a float. Operations mix them automatically, and math.type tells you which subtype a value has.

Note: Use math.type(x) to check for "integer" or "float", since type(x) says only "number".

Example: Numbers

lua
print(10, 10.0, 1e2)
print(math.type(10), math.type(10.0), math.type("10"))
print(3 == 3.0)
print(math.maxinteger)
print(math.maxinteger + 1 == math.mininteger)

-- Output:
-- 10	10.0	100.0
-- integer	float	nil
-- true
-- 9223372036854775807
-- true
Common Mistakes
  1. Assuming all numbers are floats as in Lua 5.2
  2. Expecting 3 and 3.0 to print the same
  3. Expecting integer overflow to raise an error
Chapter Summary
  • There are integer and float subtypes
  • A decimal point makes a float
  • math.type reveals the subtype
  • Integers wrap around on overflow
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.