Integers, floats and overflow

Go's numbers are fixed-width: int8 to int64, uint8 to uint64, the platform-sized int and uint, and the floating point float32 and float64. There is no implicit conversion between them, so the width of every value is visible in the source.

The ranges

int is 64 bits on a 64-bit machine and 32 bits on a 32-bit one, which is why a program that stores a number that can exceed two billion uses int64 explicitly. The limits are constants of the math package and cost nothing at run time.

package main import ( "fmt" "math" ) func main() { fmt.Println("int is", 32<<(^uint(0)>>63), "bits on this machine") fmt.Println("int8:", math.MinInt8, "to", math.MaxInt8) fmt.Println("int32:", math.MinInt32, "to", math.MaxInt32) fmt.Println("int64:", math.MinInt64, "to", math.MaxInt64) fmt.Println("uint64 max:", uint64(math.MaxUint64)) fmt.Println("float64 smallest positive:", math.SmallestNonzeroFloat64) }

go run ranges.go

int is 64 bits on this machine
int8: -128 to 127
int32: -2147483648 to 2147483647
int64: -9223372036854775808 to 9223372036854775807
uint64 max: 18446744073709551615
float64 smallest positive: 5e-324

uint64(math.MaxUint64) is needed because MaxUint64 is an untyped constant too large for int; the conversion gives it the type it must have. The constants lesson explains why the compiler treats that as a different situation from a variable that overflows.

Overflow and integer division

A variable that overflows wraps around without a warning, because the arithmetic is the two's complement of the machine and checking every operation would cost what Go does not want to pay. Division of integers truncates toward zero, and the remainder takes the sign of the dividend.

package main import "fmt" func main() { var small int8 = 127 small++ fmt.Println("int8 127 + 1 =", small) var big uint8 = 0 big-- fmt.Println("uint8 0 - 1 =", big) fmt.Println("7 / 2 =", 7/2) fmt.Println("-7 / 2 =", -7/2) fmt.Println("7 % 2 =", 7%2) fmt.Println("-7 % 2 =", -7%2) var f float64 = 1 for i := 0; i < 10; i++ { f *= 10 } fmt.Println("1e10 as float64:", f) a, b := 0.1, 0.2 fmt.Println("a + b =", a+b) fmt.Println("equal to 0.3:", a+b == 0.3) }

go run wrap.go

int8 127 + 1 = -128
uint8 0 - 1 = 255
7 / 2 = 3
-7 / 2 = -3
7 % 2 = 1
-7 % 2 = -1
1e10 as float64: 1e+10
a + b = 0.30000000000000004
equal to 0.3: false

Floating point has the behaviour every language with binary floats has: 0.1 and 0.2 are not exact, and their sum is not exactly 0.3. Money belongs in integer cents or in a decimal type, not in a float64. The math package has math.IsNaN, math.Inf and the constants that make an explicit comparison possible.

Numbers larger than 64 bits

math/big holds integers and rationals of any size, allocated on the heap. It is the type for a factorial, a hash printed as a decimal or a checksum, and it is slower than a machine integer by design.

package main import ( "fmt" "math" "math/big" ) func main() { fact := big.NewInt(1) for i := int64(2); i <= 25; i++ { fact.Mul(fact, big.NewInt(i)) } fmt.Println("25! =", fact) fmt.Println("digits:", len(fact.String())) fmt.Println("MaxInt64:", int64(math.MaxInt64)) fmt.Println("as float64 it rounds:", float64(math.MaxInt64)) }

go run big.go

25! = 15511210043330985984000000
digits: 26
MaxInt64: 9223372036854775807
as float64 it rounds: 9.223372036854776e+18

A big.Int is a pointer, and its methods return the receiver so they can be chained; the value is mutated in place. That is the opposite of the small integers above, which are values copied on assignment.

The one division the compiler accepts

A constant division by zero is a compile error, but a division by a variable that happens to be zero is a run-time panic. The program below stops at the division and never prints its last line.

package main import "fmt" func main() { zero := 0 fmt.Println("before the division") fmt.Println(10 / zero) fmt.Println("this line never runs") }

go build -trimpath -o divzero divzero.go
GOTRACEBACK=none ./divzero 2>&1; echo "status: $?"

before the division
panic: runtime error: integer divide by zero
status: 2

The panic is a runtime error: integer divide by zero, and the process exits with status 2. A program that divides by a value from outside must check it, and the lesson on panic and recover shows where such a failure can still be caught.

Exercises

  1. Print math.MaxInt32 + 1 as a variable of type int32 and as an int64. Which one is wrong, and why does the compiler accept it?
  2. Compute the sum of 0.1 ten times and compare it with 1.0. What does the comparison say?
  3. Use big.NewRat to add one third and one sixth. What does the result print?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. How wide is int on a 64-bit platform?

    Answer

    64 bits

    int is the width of a machine word, 64 bits on amd64 and arm64.

  2. What happens when an int8 variable holding 127 is incremented?

    Answer

    It wraps to -128

    Signed integer overflow wraps in two's complement, and only constants are checked.

  3. What is 7 / 2 in Go?

    Answer

    3

    Integer division truncates toward zero, so the fraction is lost.

  4. What is -7 % 2 in Go?

    Answer

    -1

    The remainder takes the sign of the dividend.

  5. What does this program print? package main import "fmt" func main() { var u uint8 = 255 u++ fmt.Println(u) }

    Answer

    0

    An unsigned 8-bit value wraps from 255 to 0.

  6. What does this program print? package main import "fmt" func main() { a, b := 0.1, 0.2 fmt.Println(a + b) }

    Answer

    0.30000000000000004

    Binary floating point cannot represent 0.1, 0.2 or their sum exactly, and Println prints the shortest form that round-trips. Constant arithmetic is exact, which is why the two values must be variables here.

  7. What does this program print? package main import "fmt" func main() { a := 10 b := 0 fmt.Println(a / b) }

    Answer

    panic: runtime error: integer divide by zero

    The divisor is a variable, so the compiler cannot check it and the run-time panics.

  8. Which type holds money without rounding surprises?

    Answer

    An integer number of cents

    Binary floats cannot represent decimal fractions exactly; integers or a decimal type can.

  9. How is int64 printed with full precision?

    Answer

    fmt.Println(v) prints every digit

    Println prints integers exactly; a float conversion would lose digits above 2^53.

  10. What does math/big buy over int64?

    Answer

    Integers of any size, at the cost of allocation

    big.Int is arbitrary precision and heap allocated, which is what a factorial of 25 needs.

The code on this page was run with go 1.26.10 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev