Whole and Decimal Numbers
In the first lesson, you saw that 10 / 2 prints 5.0, not 5. Now you'll find out why. Lua has two kinds of numbers:
- Whole numbers, which programmers call integers:
120,0,-3. Emma's coins, eggs and days are whole numbers. - Decimal numbers, which programmers call floats (from floating point, the way computers store them):
2.5,0.75,-1.5. They're written with a decimal point, never with a comma.
Lua always prints a float with a decimal point, even when it's whole: 5.0 is a float, 5 an integer. It's the same amount, but Lua keeps track of which kind each number is. The function math.type tells you:
print(math.type(5))
print(math.type(5.0))
print(math.type(2.5))This prints:
integer
float
float(math is Lua's collection of math functions. You'll use more of them in the next exercise.)
Whether a result is an integer or a float depends on the operator:
+,-and*give an integer when both numbers are integers. When one of them is a float, the result is a float too:3 * 2is6, but3 * 2.0is6.0./(division) always gives a float, even when the division works out:10 / 2is5.0.//(floor division) divides and rounds the result down to a whole number:7 // 2is3. It tells you how many times one number fits into another.%gives the remainder after division:7 % 2is1, because 2 fits into 7 three times, and 1 is left over.^raises a number to a power, and always gives a float:2 ^ 3is8.0.
print(7 / 2)
print(7 // 2)
print(7 % 2)
print(10 / 2)
print(2 ^ 3)This prints:
3.5
3
1
5.0
8.0// and % work well together. The strawberries will be ripe in 17 days: 17 // 7 is 2 whole weeks, and 17 % 7 is 3 days left over. The remainder also tells even numbers from odd ones: an even number divided by 2 leaves 0, an odd one leaves 1.
Typical mistakes:
/where you want a whole number:17 / 7gives2.4285714285714. Use//.- Floor division by zero:
7 // 0stops the program with the errorattempt to divide by zero. (7 / 0doesn't stop it, but givesinf, infinity, which isn't much use either.) - A decimal comma:
local price = 2,5doesn't report an error, butpriceis just2. In Lua, a comma separates two values. The variable gets the first one, and the 5 gets lost.
One more thing: computers store floats in binary, so many decimal numbers aren't exact. 0.1 + 0.2 prints 0.3, but the result is really a tiny bit more. That's one reason why money is best counted in whole numbers, as Emma does with her coins.
Instructions
Run the program. It prints Boxes: 6.3333333333333, but Emma can't sell a third of a box. Change the division so that the program prints only the number of full boxes:
Boxes: 6On the next line, print how many eggs are left over. Calculate it from the variables with %:
Boxes: 6
Eggs left: 2Hugo the trader pays 12 coins for a full box. Below, create a variable boxPrice with the value 12, and print how much Emma gets for one egg: boxPrice divided by boxSize, with /. The division works out, but / always gives a float, so Lua prints 2.0:
Boxes: 6
Eggs left: 2
Coins per egg: 2.0Finally, check the kinds of the two results. With one print, print math.type(eggs // boxSize) and math.type(boxPrice / boxSize), separated by a comma:
Boxes: 6
Eggs left: 2
Coins per egg: 2.0
integer floatStart programming for free
3/10