ZetCode

Golang FloatType

last modified May 8, 2025

This tutorial explains how to use the FloatType built-in type in Go. We'll cover floating-point basics with practical examples of working with float numbers.

The FloatType in Go represents floating-point numbers. Go has two floating-point types: float32 and float64. These types follow the IEEE 754 standard for floating-point arithmetic.

Floating-point numbers are used for representing real numbers with fractional parts. They are essential for scientific computing, graphics, and financial applications where precision matters.

Basic float declaration and initialization

The simplest way to use floats in Go is to declare and initialize variables. This example shows basic float operations.
Note: Float literals default to float64 unless explicitly typed.

basic_float.go
package main

import "fmt"

func main() {

    var f1 float32 = 3.14
    f2 := 2.71828 // float64 by default
    
    fmt.Printf("f1 (float32): %v, type: %T\n", f1, f1)
    fmt.Printf("f2 (float64): %v, type: %T\n", f2, f2)
    
    sum := f1 + float32(f2) // Type conversion needed
    fmt.Println("Sum:", sum)
}

The example shows float32 and float64 declarations. Note the type conversion needed when mixing different float types in operations.

Float precision and rounding

Floating-point numbers have limited precision. This example demonstrates precision issues and rounding operations with floats.

float_precision.go
package main

import (
    "fmt"
    "math"
)

func main() {

    a := 1.0 / 3.0
    fmt.Println("1/3 as float64:", a) // Shows precision limit
    
    b := float32(1.0 / 3.0)
    fmt.Println("1/3 as float32:", b) // Less precise
    
    rounded := math.Round(a*100) / 100
    fmt.Println("Rounded to 2 decimal places:", rounded)
    
    // Comparing floats safely
    epsilon := 1e-9
    if math.Abs(0.1+0.2-0.3) < epsilon {
        fmt.Println("0.1 + 0.2 equals 0.3 within tolerance")
    }
}

The example shows how float32 has less precision than float64. It demonstrates rounding and safe comparison techniques for floating-point numbers.

Mathematical operations with floats

Go's math package provides many functions for float operations. This example shows common mathematical operations with floating-point numbers.

math_operations.go
package main

import (
    "fmt"
    "math"
)

func main() {

    x := 16.0
    y := 3.0
    
    fmt.Println("Square root:", math.Sqrt(x))
    fmt.Println("Power:", math.Pow(x, y))
    fmt.Println("Sine:", math.Sin(math.Pi/y))
    fmt.Println("Logarithm:", math.Log(x))
    fmt.Println("Ceiling:", math.Ceil(x/y))
    fmt.Println("Floor:", math.Floor(x/y))
}

The example demonstrates various mathematical functions from the math package. These functions work with float64 values by default.

Special float values

Floating-point numbers can represent special values like infinity and NaN (Not a Number). This example shows how to work with these special cases.

special_values.go
package main

import (
    "fmt"
    "math"
)

func main() {

    posInf := math.Inf(1)
    negInf := math.Inf(-1)
    nan := math.NaN()
    
    fmt.Println("Positive infinity:", posInf)
    fmt.Println("Negative infinity:", negInf)
    fmt.Println("NaN:", nan)
    
    // Checking for special values
    fmt.Println("Is infinity:", math.IsInf(posInf, 1))
    fmt.Println("Is NaN:", math.IsNaN(nan))
    
    // Operations with special values
    fmt.Println("Inf + 5:", posInf+5)
    fmt.Println("Inf * 0:", posInf*0) // Results in NaN
}

The example demonstrates creation and detection of special float values. It shows how these values behave in mathematical operations.

Formatting floats

Formatting floating-point numbers for display requires careful consideration. This example shows different formatting options in Go.

formatting.go
package main

import "fmt"

func main() {

    pi := 3.141592653589793
    
    // Default formatting
    fmt.Println("Default:", pi)
    
    // Precision control
    fmt.Printf("2 decimal places: %.2f\n", pi)
    fmt.Printf("6 decimal places: %.6f\n", pi)
    
    // Scientific notation
    fmt.Printf("Scientific: %e\n", pi)
    fmt.Printf("Scientific (capital E): %E\n", pi)
    
    // Automatic format selection
    fmt.Printf("Auto: %g\n", pi)
    fmt.Printf("Auto (large number): %g\n", pi*1e15)
    
    // Width and padding
    fmt.Printf("|%10.3f|\n", pi) // Right-aligned
    fmt.Printf("|%-10.3f|\n", pi) // Left-aligned
}

The example demonstrates various formatting verbs for floating-point numbers. These include precision control, scientific notation, and alignment options.

Source

Go language specification

This tutorial covered the FloatType in Go with practical examples of floating- point operations, precision handling, and formatting.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Golang tutorials.