Golang float32 type
last modified May 8, 2025
This tutorial explains how to use the float32 built-in type in Go.
We'll cover floating-point basics with practical examples of arithmetic operations.
The float32 type represents single-precision floating-point numbers in Go. It occupies 32 bits (4 bytes) of memory and provides about 6-9 decimal digits of precision.
In Go, float32 is used for calculations where memory efficiency is
important. For higher precision, float64 is typically preferred.
Basic float32 declaration and initialization
The simplest way to use float32 is to declare and initialize
variables. This example shows basic variable declaration and arithmetic.
Note: Float literals are float64 by default in Go.
package main
import "fmt"
func main() {
var f1 float32 = 3.14
f2 := float32(2.718) // Type conversion
sum := f1 + f2
product := f1 * f2
fmt.Printf("Sum: %.3f, Product: %.3f\n", sum, product)
fmt.Printf("Types: f1 %T, f2 %T\n", f1, f2)
}
We declare two float32 variables and perform basic arithmetic. The %.3f
format specifier prints with 3 decimal places. Type conversion is needed for literals.
Float32 precision and limitations
Float32 has limited precision compared to float64. This example demonstrates precision limitations with large and small numbers.
package main
import "fmt"
func main() {
large := float32(123456789.0)
small := float32(0.000000123456789)
fmt.Println("Large number:", large)
fmt.Println("Small number:", small)
// Demonstrate precision loss
a := float32(1.0000001)
b := float32(1.0000002)
fmt.Println("a == b?", a == b) // Might be true due to precision
}
The output shows how float32 loses precision with very large or small numbers. The equality comparison demonstrates potential precision issues in calculations.
Mathematical operations with float32
Float32 supports all standard mathematical operations. This example shows various operations including those from the math package.
package main
import (
"fmt"
"math"
)
func main() {
x := float32(9.0)
y := float32(4.0)
// Basic operations
fmt.Println("x + y =", x+y)
fmt.Println("x - y =", x-y)
fmt.Println("x * y =", x*y)
fmt.Println("x / y =", x/y)
// Math functions require type conversion
fmt.Println("Sqrt(x) =", float32(math.Sqrt(float64(x))))
fmt.Println("Pow(x, y) =", float32(math.Pow(float64(x), float64(y))))
}
Basic arithmetic works directly with float32, but math functions require conversion to float64. This shows the trade-off between precision and memory.
Comparing float32 values
Comparing floating-point numbers requires special care due to precision issues. This example demonstrates proper comparison techniques.
package main
import (
"fmt"
"math"
)
const epsilon = 1e-6 // Small threshold for comparison
func main() {
a := float32(0.1)
b := float32(0.2)
c := float32(0.3)
// Problematic direct comparison
fmt.Println("a + b == c?", a+b == c)
// Better approach using epsilon
sum := a + b
diff := float32(math.Abs(float64(sum - c)))
fmt.Println("Approximately equal?", diff < epsilon)
// Special values
nan := float32(math.NaN())
inf := float32(math.Inf(1))
fmt.Println("Is NaN?", math.IsNaN(float64(nan)))
fmt.Println("Is Inf?", math.IsInf(float64(inf), 1))
}
Direct equality comparison often fails due to rounding errors. Using an epsilon threshold is more reliable. The example also shows handling of special values.
Float32 in arrays and slices
Float32 is commonly used in collections for memory efficiency. This example demonstrates float32 arrays and slices with common operations.
package main
import "fmt"
func main() {
// Array of float32
temps := [5]float32{21.5, 22.1, 23.8, 24.2, 25.0}
// Slice of float32
readings := []float32{98.6, 99.2, 100.1, 101.3}
// Calculate average
var sum float32
for _, temp := range temps {
sum += temp
}
avg := sum / float32(len(temps))
fmt.Printf("Average temp: %.1f°C\n", avg)
// Append to slice
readings = append(readings, 102.0, 103.5)
fmt.Println("Updated readings:", readings)
// Multi-dimensional
matrix := [2][2]float32{{1.2, 3.4}, {5.6, 7.8}}
fmt.Println("Matrix:", matrix)
}
Float32 arrays and slices work like other types. The example shows iteration, appending, and multi-dimensional usage. Float32 is often used in numerical work.
Source
This tutorial covered the float32 type in Go with practical
examples of arithmetic operations, comparisons, and collections usage.
Author
List all Golang tutorials.