Get Time
Search | Watch Thread  |  My Post History  |  My Watches  |  User Settings
View: Flat (newest first)  | Threaded  | Tree
Previous Thread  |  Next Thread
I have a question | Reply
First i want to say that this tutorial is a really informative one.
I have a question about checking a double to see if it is equal to zero.
Is the following expression valid and reliable?
double d;  // check if d is approximately equals to zero
if(d+1.0==1.0) {
    // equals to zero;
Re: I have a question (response to post by Magicwind) | Reply
I think so, allowing an error of 2^(-53) (which is very small). But you better wait for someone else to confirm.
Re: I have a question (response to post by Magicwind) | Reply
I'm not sure what the point of your code is, or how it's different from if (d == 0.0) (which will work even if d is -0.0). It does correctly check if d is exactly equal to zero, but it has nothing to do with approximately. Perhaps dskloet is right that it would leave you with an "approximately equal" if it's within 2-53, d == 0.0 would only return true if you were within something like 2-1000 (don't remember exactly what it is).

In reality, if you want to do an approximate zero check, you'd do either:
if (d < epsilon && d > -epsilon)

if (abs(d) < epsilon)

where epsilon is some insanely small number :-)