What integer division is
Division between whole numbers does not always come out even. Dividing 15 by 4 gives a quotient of 3 (how many times 4 fits completely) and a remainder of 3 (what is left over). Programs often need only the whole part, for example to count how many full groups fit into a total.
Go has no separate operator for this. There is no //. The ordinary / operator does the job: when both operands are integers, the result is an integer, and the fractional part is thrown away.
fmt.Println(10 / 2) // 5
fmt.Println(15 / 4) // 3
fmt.Println(20 / 3) // 6
The same works with variables:
a := 3
b := 7
fmt.Println(20 / a) // 6
fmt.Println(b / a) // 2
Quotient and remainder together
The operator % gives the remainder, so the pair / and % describes a division completely:
quotient := 26 / 3
remainder := 26 % 3
fmt.Println(quotient) // 8
fmt.Println(remainder) // 2
The two results always fit together: quotient * 3 + remainder gives back the original 26. Here 8 * 3 + 2 is 26.
Truncation, not flooring
For positive numbers, dropping the fraction is the same as rounding down, which is why this lesson is called floor division. For negative numbers, the two differ. Go truncates toward zero:
fmt.Println(-7 / 2) // -3
fmt.Println(-7 % 2) // -1
fmt.Println(7 / -2) // -3
fmt.Println(7 % -2) // 1
Rounding -3.5 down would give -4, but Go gives -3. The remainder follows the sign of the left operand (the dividend), and the rule quotient * divisor + remainder == dividend still holds: (-3) * 2 + (-1) is -7. If your program needs a true floor for negatives, you have to adjust the result yourself.
Integers versus decimals
The behaviour depends on the types, not on the operator. If either operand is a float64, the division is exact:
x := 5
y := 2
fmt.Println(x / y) // 2
fmt.Println(float64(x) / float64(y)) // 2.5
fmt.Println(float64(x / y)) // 2
The last line shows a common trap: the division of two integers happens first and loses the 0.5, and only then is the result converted to a decimal. To get 2.5, convert the operands before dividing. Go never mixes int and float64 on its own, so the conversion is always explicit.
The lesson's exercise
A product costs price dollars and Andy has amount dollars. He can't buy a fraction of a product, so the question is how many whole products he can afford. That is the quotient of amount divided by price.
var amount int = 70
var price int = 15
fmt.Println(amount / price)
The program prints 4. The exact value of 70 / 15 is about 4.67, and truncation leaves the 4 whole products. Four products cost 60 dollars, so 10 are left, which is exactly the remainder 70 % 15.
Quotient and remainder answer two different questions about the same division: how many whole products Andy can buy, and how much money he is left with. Real programs often compute both, and the same pair appears whenever you split a total into full groups and a leftover, such as seconds into minutes and seconds.
Common mistakes and gotchas
- Expecting decimals from two integers.
7 / 2is 3. If you want 3.5, at least one operand must be afloat64. - Converting too late.
float64(x / y)divides first. Writefloat64(x) / float64(y). - Assuming rounding to the nearest.
7 / 4is 1, not 2, although 1.75 is closer to 2. - Negative operands. The result is truncated toward zero, so
-7 / 2is -3 and not -4. - Dividing by zero. With an integer variable as divisor, the program stops at run time with
panic: runtime error: integer divide by zero. A literal zero divisor, as in5 / 0, is rejected by the compiler instead. - Using
//. That starts a comment in Go, so everything after it on the line is ignored.
Key takeaways
- With two integers,
/returns the quotient and drops the fraction. %returns the remainder, and(a / b) * b + a % b == a.- Go truncates toward zero, which equals rounding down only for non-negative results.
- Decimal division needs a
float64operand, converted before the division.