How to compare floating-point values safely in Go

1 Answer

0 votes
package main

import (
	"fmt"
	"math"
)

func main() {

	/*
		==================================================
		Title: Safe Floating-Point Comparison
		==================================================

		Never compare floating-point values directly.
	*/

	const epsilon float64 = 1e-12

	value := 0.1 + 0.2

	if math.Abs(value-0.3) < epsilon {
		fmt.Println("Values are effectively equal.")
	}
}


/*
run:

Values are effectively equal.

*/

 



answered Oct 7 by avibootz
...