ZetCode

Golang float64 type

last modified May 8, 2025

This tutorial explains how to use the float64 built-in type in Go. We'll cover floating-point basics with practical examples of numerical computing.

The float64 type represents 64-bit floating-point numbers in Go. It provides about 15 decimal digits of precision and is the default type for floating-point literals. Float64 is suitable for most numerical computations.

In Go, float64 implements IEEE 754 standard for floating-point arithmetic. It can represent values from approximately 1.7E-308 to 1.7E+308. Special values like NaN and Inf are also supported.

Basic float64 declaration and initialization

The simplest way to use float64 is by declaring variables with explicit type. This example shows basic declaration and initialization of float64 variables.

basic_float.go
package main

import "fmt"

func main() {

    var pi float64 = 3.141592653589793
    var e = 2.718281828459045 // Type inferred as float64
    
    fmt.Printf("pi: %v, type: %T\n", pi, pi)
    fmt.Printf("e: %v, type: %T\n", e, e)
    
    // Scientific notation
    avogadro := 6.02214076e23
    fmt.Println("Avogadro's number:", avogadro)
}

The example shows three ways to create float64 variables. The third uses scientific notation. All floating-point literals without suffix are float64.

Float64 arithmetic operations

Float64 supports all basic arithmetic operations. This example demonstrates addition, subtraction, multiplication, division, and modulus operations.

float_arithmetic.go
package main

import "fmt"

func main() {

    a := 12.5
    b := 3.2
    
    fmt.Println("Addition:", a + b)
    fmt.Println("Subtraction:", a - b)
    fmt.Println("Multiplication:", a * b)
    fmt.Println("Division:", a / b)
    
    // Modulus using math.Mod
    fmt.Println("Modulus:", math.Mod(a, b))
    
    // Compound assignment
    a += b
    fmt.Println("After +=:", a)
}

The example shows basic arithmetic with float64. Note that modulus requires the math.Mod function. Compound assignment operators work as expected.

Comparing float64 values

Comparing floating-point numbers requires special care due to precision issues. This example demonstrates safe comparison techniques for float64 values.

float_comparison.go
package main

import (
    "fmt"
    "math"
)

func main() {

    a := 0.1 + 0.2
    b := 0.3
    
    // Naive comparison (may fail)
    fmt.Println("Naive comparison:", a == b)
    
    // Safe comparison with tolerance
    tolerance := 1e-10
    diff := math.Abs(a - b)
    fmt.Println("Safe comparison:", diff < tolerance)
    
    // Special values comparison
    nan := math.NaN()
    inf := math.Inf(1)
    fmt.Println("NaN == NaN:", nan == nan) // Always false
    fmt.Println("Is Inf:", math.IsInf(inf, 1))
}

The example shows why direct equality comparison often fails with float64. It demonstrates proper comparison using a tolerance value and handling of special values like NaN and Inf.

Math functions with float64

Go's math package provides many functions that operate on float64. This example shows common mathematical operations available in the standard library.

math_functions.go
package main

import (
    "fmt"
    "math"
)

func main() {

    x := 2.0
    y := 3.0
    
    fmt.Println("Square root:", math.Sqrt(x))
    fmt.Println("Power:", math.Pow(x, y))
    fmt.Println("Exponential:", math.Exp(x))
    fmt.Println("Natural log:", math.Log(x))
    fmt.Println("Sine:", math.Sin(math.Pi/x))
    
    // Rounding functions
    fmt.Println("Ceil:", math.Ceil(3.14))
    fmt.Println("Floor:", math.Floor(3.14))
    fmt.Println("Round:", math.Round(3.14))
}

The math package provides comprehensive support for float64 operations. This includes basic arithmetic, trigonometry, logarithms, and rounding functions. All math functions use float64 parameters and return values.

Float64 precision and formatting

Formatting float64 values requires understanding their precision limitations. This example demonstrates different formatting options for float64 output.

float_formatting.go
package main

import "fmt"

func main() {

    value := 123.45678901234567
    
    // Default formatting
    fmt.Println("Default:", value)
    
    // Precision control
    fmt.Printf("2 decimal places: %.2f\n", value)
    fmt.Printf("Scientific notation: %e\n", value)
    fmt.Printf("Large exponent: %g\n", value*1e30)
    
    // String formatting options
    fmt.Printf("Width 10, precision 4: %10.4f\n", value)
    fmt.Printf("Left aligned: %-10.2f|\n", value)
}

The example shows various ways to format float64 values. The printf verbs %f, %e, and %g offer different formatting styles. Precision and width can be controlled for better output presentation.

Source

Go language specification

This tutorial covered the float64 type in Go with practical examples of numerical computation 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.