Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coding - Comparison test gone wrong?
#1
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 : 1
Reply
#2
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.
Reply
#3
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?
Reply
#4
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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)