Thread: False Boolean Value?

  1. #1
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59

    False Boolean Value?

    When the following code is executed, the loop does not run when f == 2.0
    Why wouldn't it?

    Code:
    int main()
    {
            printf("\n\n\n"); //spacer
    
            float f = .1;
            while (f <= 2.0)
            {
                    printf("&#37;.1f*Pi:  sin() = %f  cos() = %f  tan() = %f\n",
                                                            f,
                                                            sin(PI*f),
                                                            cos(PI*f),
                                                            tan(PI*f));
                    f += .1;
                    printf("\nf now equals %f",f);
            }
            printf("\n\n\n"); //spacer
    
    
            return 0;
    }
    last line of output from program:
    Code:
    1.9*Pi:  sin() = -0.309016  cos() = 0.951057  tan() = -0.324919
    
    f now equals 2.000000
    I also tried to see if this statement returned a true value:
    Code:
    (2.0 == 2.00)
    ..and it did, so I have no idea why my loop only runs until f == 1.9

    I know I could eliminate the problem by changing my loop to:
    Code:
    while (f < 2.1)
    But i'm curious as to why it isn't working as is.

    Thank you for any help.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Sigh...

    The value 0.1 cannot be exactly represented in a binary format. There is no way to repeatedly add the value 0.1 to itself to achieve the value 2.0. Read any basic introduction to floating point arithmetic.

  3. #3
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59
    Thanks for your help.

    I'm aware that certain values cannot be expressed in binary, but when I did a printf for float f, it printed:

    2.000000

    I was under the impression that floats stored 6 significant digits, so I thought that this was the entire value of f.

    *shrug*

    Quote Originally Posted by brewbuck View Post
    Read any basic introduction to floating point arithmetic.
    I'll take your advice.
    Last edited by kwikness; 10-08-2007 at 08:15 PM.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Yes by default, the float stores 6 digits after the decimal point. And you know it could be customized to 2 digits by &#37;.2f or something. That won't really make any different on comparing. Comparing floating point number is not going to give you the right answer, because of precision.

    Google "Comparing Floating point number". You will understand the concept of comparing decimal variables.

    ssharish
    Last edited by ssharish2005; 10-08-2007 at 08:28 PM.

  5. #5
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59
    www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

    Found the section on epsilon comparisons especially helpful. Thanks.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by kwikness View Post
    I was under the impression that floats stored 6 significant digits, so I thought that this was the entire value of f.
    No, that's just what got printed. The actual value may have been something like 2.00000000001.

    A float doesn't store a certain number of digits. The decimal point can "float" back and forth, thus the name. Since it is intrinsically a binary, not decimal, object, it is impossible to characterize the number of decimal digits it can represent -- unless you want to refer to a fractional number of digits.

    For instance if you had 16 bits available to represent a fractional value, that's equivalent to 16 * log(2) / log(10) = 4.816... decimal digits.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    I was wrong then if it the float doesnt have const no of decimal places after the point.

    ssharish

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    For instance if you had 16 bits available to represent a fractional value, that's equivalent to 16 * log(2) / log(10) = 4.816... decimal digits.
    Note also that if you do something like this:
    Code:
    int main() 
    {
       float f = 1.4f;
       if (f == 1.4) {
          printf("A\n");
       }
       if (f == 1.4f) {
          printf("B\n");
       }
       return 0;
    }
    You may find that it only prints "B", becasue the first comparison extends f to a temporary double value before comparing with 1.4 - and since 1.4 is one of many values that can't be described EXACTLY as a float, it's failing to compare the exact value of the two variables.

    The second compare compares the float value with a 1.4 float value, so it matches.
    [But this may not happen if you calculate the value by for example adding 1.0 to 0.4 to make 1.4].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. the chess project
    By Hussain Hani in forum Projects and Job Recruitment
    Replies: 8
    Last Post: 05-28-2007, 02:33 AM
  2. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM