Quotient and remainder
When you divide one whole number by another, the quotient is how many times the divisor fits completely, and the remainder is what is left over. For 15 divided by 4, the quotient is 3 and the remainder is 3, because 4 * 3 + 3 = 15.
Division of Int values
Swift has no separate floor-division operator. The ordinary / does the job when both operands are Int: it returns the quotient and drops the fractional part.
print(10 / 2) // 5
print(15 / 4) // 3
print(20 / 3) // 6
let a = 3
let b = 7
print(20 / a) // 6
print(b / a) // 2
The remainder comes from %:
let quotient = 26 / 3
let remainder = 26 % 3
print(quotient) // 8
print(remainder) // 2
If you need both values at once, 26.quotientAndRemainder(dividingBy: 3) returns the pair (quotient: 8, remainder: 2).
Truncation versus floor
Integer division in Swift truncates: it rounds toward zero. For positive numbers that is the same as rounding down, and the two ideas only separate when a result is negative:
let neg = -7
let two = 2
print(neg / two) // -3
print(neg % two) // -1
The exact value of -7 / 2 is -3.5. Rounding down would give -4, but Swift gives -3, and the remainder takes the sign of the dividend, so that (neg / two) * two + neg % two still equals -7. Programs that need true floor behaviour with negative numbers have to compute it themselves.
The floor() function
The function floor takes a decimal number and returns the largest whole number that is less than or equal to it. It works on floating-point values and returns a Double. It belongs to Foundation, so the file needs an import:
import Foundation
print(floor(3.9)) // 3.0
print(floor(5.2)) // 5.0
print(floor(7.0)) // 7.0
print(floor(-3.9)) // -4.0
print(Int(floor(3.9))) // 3
Without import Foundation, the call fails with cannot find 'floor' in scope. The result is a Double, which is why it prints as 3.0; wrap it in Int(...) for a whole number. Floor of a negative decimal goes down, to -4.0, which is where it differs from integer division.
For this lesson you don't need floor: dividing two Int values already gives the quotient.
Getting a floored quotient yourself
If you do need the floor of a division that might be negative, convert to Double, divide, floor and convert back: Int(floor(Double(neg) / Double(two))) is -4 for -7 and 2. For non-negative values, plain / is all you need.
The exercise
A product costs price dollars and Andy has amount dollars. He can't buy a fraction of a product, so the program must print how many whole products he can afford. The starter code is:
let amount: Int = 70
let price: Int = 15
The number of whole products is the quotient of amount divided by price. Both constants are Int, so the division drops the fraction: 70 divided by 15 is 4.67, and the result is 4.
The solution in Swift
let amount: Int = 70
let price: Int = 15
print(amount / price)
The output is:
4
Common mistakes
- Converting to
Doublefirst.Double(amount) / Double(price)gives 4.666666666666667, which keeps the fraction the task wants removed. - Assigning an
Intdivision to aDouble.let q: Double = a / bwith twoIntvalues doesn't compile:cannot convert value of type 'Int' to specified type 'Double'. - Dividing by zero.
7 / 0onIntvalues stops the program with a runtime error.Doubledivision by zero givesinfinstead. - Using
%on decimals.7.5 % 2doesn't compile in Swift. The message points totruncatingRemainder(dividingBy:).
Key takeaways
- With
Intoperands,/returns the quotient and%returns the remainder. - Integer division rounds toward zero;
floorrounds down and needsimport Foundation. - Dividing
amountbypricecounts how many whole items the money buys.
