Skip to content
Lesson 3 of 14

Step 1 of 5 · Reading · ~4 min

Learn

Getting Started

Numbers and Math

Go's arithmetic operators look like everyone else's, and two of them behave differently than you expect. By the end of this lesson you can predict what / and % give for any pair of integers, including negative ones, and you know when a number has to become a float64 first.

Integer division keeps only the whole part

When both sides of / are integers, the result is an integer. Go throws the fraction away — it does not round:

go

Those last two are the single most common numeric bug for newcomers: a "percentage" that is always zero because both operands were int. The discarded part is never rounded up, no matter how close it was.

The direction of the discard matters once a negative sign appears. Go truncates toward zero, so it chops digits off rather than sliding down the number line:

go

If you have written Python, this is a genuine difference: Python's // floors, so it answers -5 there. Go answers -4.

The remainder operator remembers the sign of the left side

% gives what is left over after that truncating division:

go

The rule worth memorising: % takes the sign of the dividend — the value on the left. A remainder in Go can absolutely be negative, and the identity left == (left/right)*right + left%right holds for every pair.

Together the two operators split a number apart. A clock is the classic use:

go

Same trick on digits: n % 10 is the last digit, n / 10 removes it.

One runtime hazard: dividing by zero is not a compile error, it is a panic that stops the program. Guarding the divisor is your job.

When you actually want the fraction

Convert first. Go never converts numbers for you, and a conversion is always spelled out:

go

Converting the other way truncates too, exactly like integer division: int(9.99) is 9, not 10. Use math.Round(x) when you want the nearest whole number, then convert.

Do not reach for float64 to rescue big integer arithmetic. A float64 holds about 15-16 significant decimal digits, so numbers past roughly 9,000,000,000,000,000 start landing on the wrong value:

go

Integer operators stay exact all the way to 9,223,372,036,854,775,807, the largest int. Floats are for measurements; integers are for counting.

Floats bring their own famous trap, worth knowing now: 0.1 + 0.2 prints 0.30000000000000004, and 0.1+0.2 == 0.3 is false. Compare floats with a tolerance (math.Abs(x-y) < 1e-9), and keep money in whole cents in an int.

Common mistakes

  • Rounding down instead of toward zero. Reaching for math.Floor on a negative result turns -4 into -5. Go's own / is already correct; wrapping it in float helpers changes the answer.
  • Forcing the remainder positive. Adding the divisor back when the leftover looks "wrong" turns -1 into 1. Go's % is allowed to be negative and the tests expect it.
  • Going through float64 for large integers. int(float64(x) / float64(y)) agrees with x / y on small values and quietly disagrees near the top of the int range, where the float cannot represent x exactly.
  • Printing two numbers with one call. fmt.Println(x, y) puts them on one line separated by a space; two separate lines need two calls.

Your exercise

Read the two integers the starter is already reading and report the whole-number part of the first divided by the second, then what is left over, on two lines. The starter prints two placeholder zeros, so it compiles and fails; replace the placeholders. The divisor is never zero, but either value may be negative — the hidden tests care about the sign.

Up nextString BasicsStrings

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…