Skip to content

Instantly share code, notes, and snippets.

@cevaris
Created March 9, 2015 14:09
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cevaris/bc331cbe970b03816c6b to your computer and use it in GitHub Desktop.
Save cevaris/bc331cbe970b03816c6b to your computer and use it in GitHub Desktop.
Golang float64 equality esitimation
var EPSILON float64 = 0.00000001
func floatEquals(a, b float64) bool {
if ((a - b) < EPSILON && (b - a) < EPSILON) {
return true
}
return false
}
@cevaris
Copy link
Author

cevaris commented Mar 9, 2015

@brendensoares
Copy link

If anyone is curious what the meaning of EPSILON is:

In mathematics (particularly calculus), an arbitrarily small positive quantity is commonly denoted ε

from Wikipedia

@aleh-null
Copy link

aleh-null commented Dec 14, 2017

why not

var eps float64 = 0.00000001
func floatEquals(a, b float64) bool {
	if math.Abs(a - b) < eps {
		return true
	}
	return false
}

@bearx3f
Copy link

bearx3f commented Feb 19, 2018

@aleh-null may be this is an answer => Floating-Point Comparison

@matbur
Copy link

matbur commented Mar 9, 2018

Why not

var EPSILON float64 = 0.00000001
func floatEquals(a, b float64) bool {
	return (a - b) < EPSILON && (b - a) < EPSILON
}

@TranBaNgoc
Copy link

var EPSILON float64 = 0.00000001
func floatEquals(a, b float64) bool {
        return math.Abs(a - b) < EPSILON
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment