nice 1 happy_reaper
much appreciated
nice 1 happy_reaper
much appreciated
always true((a < 0.00001 || a > -0.000001) && (b < 0.00001 || b > -0.000001))
Should be
Code:((a < 0.00001 && a > -0.000001) && (b < 0.00001 && b > -0.000001))
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
hehe...right
The funny thing is that I remember correcting someone on that a couple weeks back...
Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction
this...
...seems a lot more intuitive to me.Code:if ( !a && !b )
But that would also fall prey to the same floating point problems as the OP was having.
Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction
no it wouldn'tOriginally Posted by Happy_Reaper
Explain, please.no it wouldn't
Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction
> if ( !a && !b )
Considering this is just
if ( !(a!=0.0) && !(b!=0.0) )
you still haven't solved the problem of comparing floats for exact equality or inequality, as described in the first link posted by andor.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
the original poster wanted to compare variables to absolute zero, not zero within a tolerance (at least that's how i interpreted his code), so i don't understand why you guys are providing solutions that compensate for fuzz... explain, please.Originally Posted by Salem
Namely, he's trying to compare floats to absolute zero. That's not the same as comparing ints with 0.
Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction
yes, i know. however, zero of any magnitude is still zero, and unary negation should still fit the bill in this case (!0, !0.0, !0.0000, etc should all still return 1).Originally Posted by Happy_Reaper
Floating point numbers aren't exact. Assigning "1.23" might result in "1.229999999" being stored in reality. When you're comparing floating point numbers, you have to take this into account.
Take this loop, for example.
It might run infinitely; 1.0 can't be represented exactly on some computers at least.Code:float x; for(x = 0.0; x != 1.0; x += 0.1) {}
This is a good read: http://en.wikipedia.org/wiki/Floating_point
Admittedly, zero is probably different. I imagine that floating point numbers can usually represent zero.
Last edited by dwks; 11-22-2006 at 05:10 PM.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
You're missing the point.
Saying x == 0.0 is just as broken as saying x != 0.0
If x is even off by some very small fraction then the logic of the test is completely broken.
If you want repeatability with floating point comparisions, then you have to implement some kind of approximately equal to test.
Sure it will always work if you just assign a constant, but if you're doing something involving calculations like
for ( x = 0.0 ; x < 1.0 ; x += 0.1 )
don't expect x to be reliably 1.0 when the loop finishes.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
no, i'm not missing the point; i'm afraid both of you are. you can't input 0 into x and have it come out 0.0000001. the inaccuracy of floating point numbers doesn't apply to zero, and the computer doesn't have any problem storing zero, regardless of the magnitude.
and if x is guaranteed to be zero, then negating it is guaranteed to be 1 and the code snippet i offered will work.
You missed my edit.
Where did you see that?Originally Posted by dwks
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.