Thread: K&R question

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    17

    K&R question

    This is probably the fifth time I am reading this book and everytime around I notice little details that I had not noticed previously. This time, right on the beginning of the book, something caught my attention. On page 12, there's a program that prints the Fahrenheit-Celsius table and the authors use floats to store both temperatures. There is also a while loop where they compare the current value of the fahr variable against the value of the upper limit of the table, which is an int. So we have this:
    Code:
    float fahr;
    int upper = 300;
    
    while (fahr <= upper)
       ...
    I understand that in an operation, operands have to have the same types, so in this case, upper, which is an int, will be converted to float before the comparison is done, right? Now isn't it dangerous to compare floating-point values? Isn't it almost the same as doing something such as this?
    Code:
    float i;
    for (i = 0.0; i <= 10.0; i += 0.1)
       ...
    Thanks.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    It is indeed dangerous to compare floating points values for equality
    Code:
    if( i == 10.0)
    But that is not what your loop is doing. Comparing for a range of values is ok.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's not dangerous. It's just potentially flawed. Also, there is no difference in comparing with a range or comparing to a single specific value. Either way you're doing a floating point comparison, so you're going to have the same potential problem.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    >>Also, there is no difference in comparing with a range or comparing to a single specific value.

    There most certainly is a difference -- program-wise no, but it can (and often does) cause runtime problem due to the inaccuracy of floating point. The value 10.0 may or may not really be represented internally as 10.0 -- for instance it could be 10.00000123. So any attempt to compare a float (or double) with that will most certainly fail.

  5. #5
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    I can't imagine comparing a float to an int would be a big problem. If the floats are stored in your machine as per the IEEE standard for 32-bit or 64-bit floats, they are pretty damn accurate. As long as you aren't comparing two floats that don't differ by some ridiculously small percentage and that have over 20 significant digits, you should be OK.

    The coercion in C really doesn't matter between ints and floats, regardless. People were coding in C for many years where there was no type checking at all. It wasn't until an ANSI C standard that came out inthe late eighties or early nineties that type checking was made an official part of the language, AFAIK. I'm sure comparisons like the one you noticed aren't considered as bothersome to programmers because they are intuitive and have intuitive results.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're missing the point I think. Comparing to a range is exactly the same as comparing to a single value. Either way, you still end up comparing to one single value. That is:
    Code:
    if( x == y )
    This still preforms the same comparison to a single value, that this does:
    Code:
    if( x <= y )
    Either way, x is still compared to y. It still compares x to y to see if it fits the bill. It's simply doing one more check the other way. It compares equality to y, and to see if it's less. I'm not see how making it a range makes it any different, other than now you're doing two comparisons instead of one. In either case, it's still the same method of comparison. That's why they use abs for "better" comparisons.

    As to accuracy, there are multiple FAQs on the topic. "Pretty damn accurate" doesn't make them exact. It makes them "pretty close". Which is the whole point of A.D.'s post. If you need to be exact, don't use floating point numbers.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    >>I can't imagine comparing a float to an int would be a big problem

    It will do an integer comparison -- the float is truncated to an integer, dropping any and all fractional parts. So, if (an int) x == 1 and (a float) y == 1.2, an attempt to find out if y > x will fail

  8. #8
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    That doesn't make sense. Shouldn't coercion always go towards the more complex of the two? I know that it goes int -> float in java...

    Chalk that up to counterintuitive language design.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Ancient Dragon
    It will do an integer comparison -- the float is truncated to an integer, dropping any and all fractional parts. So, if (an int) x == 1 and (a float) y == 1.2, an attempt to find out if y > x will fail
    Isn't that completely backwards? Isn't the int promoted to a float?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Isn't that completely backwards? Isn't the int promoted to a float?
    Microsoft 2003 says that's backwards. That may not follow the standard but I don't see any reason that it wouldn't

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Might we both be talking about the "usual arithmetic conversions"?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about K&R example listing 1-6 Arrays
    By fsx in forum C Programming
    Replies: 1
    Last Post: 04-24-2009, 09:32 AM
  2. Question about K&R program
    By Aerie in forum C Programming
    Replies: 15
    Last Post: 04-24-2005, 07:09 AM
  3. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM