Skip to content

Instantly share code, notes, and snippets.

@cevaris
Created March 9, 2015 14:09
Show Gist options
  • Select an option

  • Save cevaris/bc331cbe970b03816c6b to your computer and use it in GitHub Desktop.

Select an option

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

cevaris commented Mar 9, 2015

Copy link
Copy Markdown
Author

@brendensuarez

Copy link
Copy Markdown

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

aleh-null commented Dec 14, 2017

Copy link
Copy Markdown

why not

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

@bearx3f

bearx3f commented Feb 19, 2018

Copy link
Copy Markdown

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

@matbur

matbur commented Mar 9, 2018

Copy link
Copy Markdown

Why not

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

@TranBaNgoc

Copy link
Copy Markdown
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