Sling Academy
Home/Golang/Comparing Floating Point Numbers for Equality in Go

Comparing Floating Point Numbers for Equality in Go

Last updated: November 24, 2024

In many programming tasks, comparing floating point numbers for equality can be more complex than simply using the equality operator. Small differences due to floating point arithmetic can cause direct comparisons to fail even when the numbers are numerically equal. In this article, we will explore how to compare floating point numbers for equality in Go, from basic understanding to more sophisticated techniques.

Understanding Floating Point Precision

Floating point numbers are an approximation to real numbers, and they are subject to rounding errors. This leads to precision issues, which makes direct comparisons for equality unreliable in many cases.

Basic Example

Consider two floating point numbers:

package main

import (
    "fmt"
)

func main() {
    a := 0.1
    b := 0.2
    c := a + b

    fmt.Println(c == 0.3) // false
}

In this Go example, a + b appears to be 0.3, but due to precision issues, the comparison fails and returns false.

Comparing with a Tolerance

A common method to handle floating point comparison is using a tolerance (also known as epsilon). This involves checking if the difference between the two numbers is within a small range.

Intermediate Example

package main

import (
    "fmt"
    "math"
)

func main() {
    a := 0.1
    b := 0.2
    c := a + b
    epsilon := 1e-9 // A small tolerance

    fmt.Println(math.Abs(c-0.3) < epsilon) // true
}

This code will print true because the difference between c and 0.3 is within the specified epsilon tolerance, making them effectively equal in practice.

Advanced Techniques: Relative Difference

Instead of comparing absolute differences, sometimes we use relative differences, which allow the comparison to scale with the magnitude of the numbers involved.

Advanced Example

package main

import (
    "fmt"
    "math"
)

func AlmostEqual(a, b, tol float64) bool {
    diff := math.Abs(a - b)
    largest := math.Max(math.Abs(a), math.Abs(b))

    return diff <= largest*tol
}

func main() {
    a := 12345678.1
    b := 12345678.2
    tolerance := 1e-9

    fmt.Println(AlmostEqual(a, b, tolerance)) // Depending on the context, may be true or false
}

The function AlmostEqual calculates the relative difference by considering the largest magnitude involved. This approach is valuable when comparing numbers with significant magnitude, where absolute differences alone might not correctly reflect the relative discrepancy.

Considerations

When implementing floating point comparisons, choose an appropriate level of precision based on your application's requirements. Also, documentation and comments that explain the chosen tolerance values can help maintain clarity in collaborative or long-term projects.

By understanding and implementing these techniques, you can handle floating point comparisons in Go with greater reliability and accuracy.

Next Article: Working with Random Seeds for Predictable Results in Go

Previous Article: Testing Numeric Algorithms with Edge Cases in Go

Series: Numbers and Math in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant