Go Floats
Floating-point numbers ("floats") store values with a fractional part, such as prices, measurements and percentages. Go has two float types:
| Type | Size | Precision | Approximate range |
|---|---|---|---|
float32 |
32 bits | about 7 decimal digits | ±3.4 × 10³⁸ |
float64 |
64 bits | about 15–16 decimal digits | ±1.8 × 10³⁰⁸ |
float64 is the default for float literals and what the math package uses. Prefer it unless you are storing huge arrays of numbers where memory matters.
package main
import "fmt"
func main() {
price := 19.99 // float64
var weight float32 = 2.5 // explicitly float32
distance := 3.8e5 // scientific notation: 380000
tiny := 1e-3
fmt.Println(price, weight, distance, tiny)
fmt.Printf("%T %T\n", price, weight)
}19.99 2.5 380000 0.001
float64 float32
Float arithmetic
Unlike integers, float division keeps the fractional part. If one side of an operation is an integer variable, you must convert it first.
package main
import "fmt"
func main() {
total := 10.0
people := 4
share := total / float64(people)
fmt.Println("Each pays:", share)
fmt.Println(7.0 / 2) // untyped constants: 3.5
fmt.Printf("%.3f\n", 1.0/3) // round when printing
}Each pays: 2.5
3.5
0.333
Floats are approximations
Floats are stored in binary, and most decimal fractions (like 0.1) cannot be represented exactly. Small errors appear:
package main
import (
"fmt"
"math"
)
func main() {
a := 0.1
b := 0.2
sum := a + b
fmt.Println(sum)
fmt.Println(sum == 0.3)
// Compare with a small tolerance instead
fmt.Println(math.Abs(sum-0.3) < 1e-9)
}0.30000000000000004
false
true
Tip: Never use floats for money in real applications. Store amounts as integer cents (
int64) and format them when printing.
Useful functions in the math package
| Function | Result |
|---|---|
math.Round(x) |
nearest integer (halves away from zero) |
math.Floor(x) / math.Ceil(x) |
round down / up |
math.Trunc(x) |
drop the fraction |
math.Sqrt(x), math.Pow(x, y) |
square root, power |
math.Abs(x) |
absolute value |
math.Max(x, y), math.Min(x, y) |
larger / smaller |
math.Pi, math.Inf(1), math.NaN() |
constants and special values |
package main
import (
"fmt"
"math"
)
func main() {
x := 7.45
fmt.Println(math.Round(x), math.Floor(x), math.Ceil(x), math.Trunc(-x))
fmt.Println(math.Sqrt(144), math.Pow(2, 10))
fmt.Printf("%.4f\n", math.Pi)
fmt.Println(int(math.Round(2.5)))
}7 7 8 -7
12 1024
3.1416
3
Special values: Inf and NaN
Dividing a float variable by zero does not panic — it produces +Inf, -Inf or NaN ("not a number"). NaN is not even equal to itself; check for it with math.IsNaN.
package main
import (
"fmt"
"math"
)
func main() {
zero := 0.0
fmt.Println(1/zero, -1/zero, zero/zero)
n := math.NaN()
fmt.Println(n == n, math.IsNaN(n))
}+Inf -Inf NaN
false true
float32 loses precision
float32 keeps only about seven significant digits, which shows up quickly:
package main
import "fmt"
func main() {
var f32 float32 = 16777216.0
var f64 float64 = 16777216.0
fmt.Println(f32+1, f64+1)
}1.6777216e+07 1.6777217e+07
Test yourself
What type does Go give to x := 2.5?
float32float64decimaldouble
Why does 0.1 + 0.2 == 0.3 evaluate to false with float64 variables?
- A bug in Go
- Floats are binary approximations, so the sum is 0.30000000000000004
==cannot compare floats- 0.1 is rounded to 0
What does math.Floor(-2.5) return?
- -2
- -3
- -2.5
- 2