Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

float32 is a built-in primitive data type in Go that represents a 32-bit single-precision floating-point number, strictly conforming to the IEEE 754 standard. It occupies 4 bytes of memory and is composed of three parts: a 1-bit sign, an 8-bit exponent, and a 23-bit mantissa (fraction).

Technical Specifications

  • Memory Footprint: 32 bits (4 bytes)
  • Precision: Approximately 7 decimal digits
  • Zero Value: 0
  • Maximum Value: math.MaxFloat32 (3.4028235×1038\approx 3.4028235 \times 10^{38})
  • Smallest Non-Zero Value: math.SmallestNonzeroFloat32 (1.401298×1045\approx 1.401298 \times 10^{-45})

Declaration and Type Inference

By default, Go’s untyped floating-point constants and literals are inferred as float64. To instantiate a float32, explicit type declaration or type conversion is mandatory.
package main

// Struct field definition
type Vector struct {
    X float32
    Y float32
}

func main() {
    // Explicit variable declaration
    var pi float32 = 3.141592

    // Short declaration using type conversion
    euler := float32(2.718281)
    
    _, _ = pi, euler
}

Type Conversion

Go’s strong typing system prohibits implicit type coercion. Operations involving float32 and other numeric types (including float64 and integers) require explicit type conversion.
package main

func main() {
    var a float32 = 10.5
    var b float64 = 20.5

    // Invalid: c := a + b (mismatched types float32 and float64)

    // Valid: Convert float32 to float64
    c := float64(a) + b

    // Valid: Convert float32 to int (fractional part is truncated)
    d := int(a) // d equals 10
    
    _, _ = c, d
}

Precision Limitations

Because float32 is limited to 23 bits for the mantissa, it can only accurately represent about 7 decimal digits. Assigning a value that exceeds this precision results in rounding errors at the hardware level.
package main

import "fmt"

func main() {
    // Exceeds 7 digits of precision
    var precise float32 = 12345678.9

    // The actual stored value will be 12345679.000000 due to precision loss
    fmt.Printf("%f\n", precise) 
}

Special IEEE 754 Values

float32 supports standard IEEE 754 non-numeric values, which can be generated using the math package. Because the math package functions return float64, they must be converted to float32.
package main

import "math"

func main() {
    // Positive Infinity (+Inf)
    var posInf float32 = float32(math.Inf(1))

    // Negative Infinity (-Inf)
    var negInf float32 = float32(math.Inf(-1))

    // Not a Number (NaN)
    var nan float32 = float32(math.NaN())
    
    _, _, _ = posInf, negInf, nan
}
Master Go with Deep Grasping Methodology!Learn More