How reliable is comparing Doubles? Should I create...
# announcements
h
How reliable is comparing Doubles? Should I create a safe compare function with some small wiggle room? like
fun Double.equals(b: Double): Boolean = abs(this - b) < 1e-20
?
s
I would expect it to be a bitwise equality. Any error tolerance seems would be quite domain specific imo
k
It's reliable, but of course the operations aren't, eg they're not commutative/associative/... and so when you're unit testing there are usually functions like that that take a precision parameter
s
Comparing Doubles is very reliable. They adhere, in most languages, to this: https://en.wikipedia.org/wiki/Double-precision_floating-point_format
😍 1
h
Thank you so kindly, Anton