![]() |
|
Coding - Comparison test gone wrong? - Printable Version +- Southperry.net (https://www.southperry.net) +-- Forum: Social (https://www.southperry.net/forumdisplay.php?fid=14) +--- Forum: The Speakeasy (https://www.southperry.net/forumdisplay.php?fid=54) +--- Thread: Coding - Comparison test gone wrong? (/showthread.php?tid=49190) |
Coding - Comparison test gone wrong? - Kalovale - 2011-11-29 I'm sure I just made a silly mistake somewhere, I just don't know where. Observe the highlighted piece of code:
screenshot
excerpted for your convenience
sample output of the program
It doesn't do much, take an initial x value, do stuff with it, decrement by deltaX until it hits limit, abort. The problem is, x > limit returns true when x = limit (as in this example, x = limit = 0.7) Code: fprintf("x = %.1f, limit = %.1f, x > limit : %d\n", (x), limit, (x>limit));Code: x = 0.7, limit = 0.7, x > limit : 1Coding - Comparison test gone wrong? - Nikkey - 2011-11-29 x is higher than limit. When you're printing out x and limit, you only print out the first decimal behind the period. Try to print it all out, and you'll see that x > limit. Coding - Comparison test gone wrong? - Kalovale - 2011-11-29 I did print out up to the 15th decimal place. Also note how on the Before-After check, x was printed without any formatting, so perhaps the fprintf function did the truncating itself. But anyway, I got it working with while (x - limit > 10^(-15)) so I'm assuming it's just some nuances with binary representation of numbers. The question now is how do I know beforehand that something this simple (decrement step by 0.1) is going to run into this problem? Should I always do the (different > epsilon) check instead of (difference = 0) check? Coding - Comparison test gone wrong? - sicnarf - 2011-11-29 Kalovale Wrote:The question now is how do I know beforehand that something this simple (decrement step by 0.1) is going to run into this problem? Should I always do the (different > epsilon) check instead of (difference = 0) check? You'll pretty much want to use >= or <= for floating-point compares in place of ==. Integer is fine to use ==, though. |