Created
March 9, 2015 14:09
-
-
Save cevaris/bc331cbe970b03816c6b to your computer and use it in GitHub Desktop.
Golang float64 equality esitimation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var EPSILON float64 = 0.00000001 | |
func floatEquals(a, b float64) bool { | |
if ((a - b) < EPSILON && (b - a) < EPSILON) { | |
return true | |
} | |
return false | |
} |
If anyone is curious what the meaning of EPSILON
is:
In mathematics (particularly calculus), an arbitrarily small positive quantity is commonly denoted ε
from Wikipedia
why not
var eps float64 = 0.00000001
func floatEquals(a, b float64) bool {
if math.Abs(a - b) < eps {
return true
}
return false
}
@aleh-null may be this is an answer => Floating-Point Comparison
Why not
var EPSILON float64 = 0.00000001
func floatEquals(a, b float64) bool {
return (a - b) < EPSILON && (b - a) < EPSILON
}
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
Check it out here
http://play.golang.org/p/drqmxn9CPg