JOIN
 Select a Forum     Round Tables New Member Discussions News Discussions Algorithm Matches Marathon Matches NASA Tournament Lab TopCoder Cookbook High School Matches Sponsor Discussions Development Forums Design Forums Search | Watch Thread  |  My Post History  |  My Watches  |  User Settings View: Flat (newest first)  | Threaded  | Tree Previous Thread  |  Next Thread Forums Tutorial Discussions Representation of Integers and Reals Tutorial I have a question
 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) ```or```if (abs(d) < epsilon) ```where epsilon is some insanely small number :-)
 Forums Tutorial Discussions Representation of Integers and Reals Tutorial I have a question Previous Thread  |  Next Thread