https://kotlinlang.org logo
Title
h

Hullaballoonatic

05/02/2019, 4:40 AM
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

spand

05/02/2019, 7:23 AM
I would expect it to be a bitwise equality. Any error tolerance seems would be quite domain specific imo
k

karelpeeters

05/02/2019, 7:38 AM
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

streetsofboston

05/02/2019, 11:20 AM
Comparing Doubles is very reliable. They adhere, in most languages, to this: https://en.wikipedia.org/wiki/Double-precision_floating-point_format
😍 1
h

Hullaballoonatic

05/02/2019, 7:19 PM
Thank you so kindly, Anton