« first day (2226 days earlier)      last day (626 days later) » 

3:42 PM
Hey guys, is it ok to compare double like this
` if( a != 0.0 && b != 0.0 ) { /* DO STUFF */ }`
 
depends on the context
 
I mean is it gouranteed to compare to a real 0? what if one is like 0.000000000000000001
 
if it's a computed value, then it's probably not ok. If you literally assigned 0.0 to it, it's alright
 
aha.. .well a and b above are computed somewhere else
hmmm
it has worked fine so far. But I want to be sure it will always work :D
 
you can do something like

epsilon = 0.0000001; //or whatever your tolerance is
if( abs(a) > epsilon && abs(b) > epsilon ) {/* DO STUFF */}
 
3:49 PM
thanks I will try it
you mean std::numeric_limits<double>::epsilon()
right?
 
well you can use the numeric epsilon if that's enough for your use-case. I don't remember much about the numeric stability stuff, but if you do a bunch of operations like divisions or have operations with numbers of very different magnitudes then your error can accumulate way above the machine epsilon
 
4:18 PM
12 messages moved from Lounge<C++>
quick reminder that IEEE754 also supports -0.0
so comparing to zero is already fraught
 
 
4 hours later…
8:48 PM
to be fair x == 0.0 works for both x being negative zero and positive zero
 

« first day (2226 days earlier)      last day (626 days later) »