Thread: how it executes

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    14

    how it executes

    Code:
    #include<stdio.h>
    main()
    {
       float a=.7;
       if(a<.7)
            printf("abcd");
       else
            printf("qrst");
    }
    I thought it would print 'qrst' ...it prints 'abcd'...im confused ?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Make it print a itself... you might be in for a surprise.

    What Every Computer Scientist Should Know About Floating-Point Arithmetic

    Floating point numbers are not *exact* numbers... they are approximations.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > float a=.7;
    > if(a<.7)
    By itself, .7 is a double constant (not a float constant).

    So the assignment has a hidden double to float truncation.

    And the comparison has a hidden float to double promotion (of a).

    Plus all the things CT said.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    What CommonTater says is true, but specifically what's happened here is you assigned a (non-representable) double value to a float, amounting to a loss of precision, and then you test against a double as well. If you do this:

    Code:
       if(a<.7f)
    You'll get the expected outcome. By "non-representable", I mean that 0.7 cannot be precisely stored as a floating point number in a computer (including doubles). Because of the nature of how floating point numbers are stored, they are only exact WRT to "inverse power of 2" numbers. "Inverse power of 2" is not a real term, lol, but I use it to refer to fractional numbers produced by dividing 1/2, so the series is 0.5, 0.25, 0.125, 0.0625, and so on. Notice 0.1 is not in there. Try this code:

    Code:
    #include <stdio.h>
    
    int main() {
            float i;
            for (i=0.0f; i<20; i+=0.1f) {
                    printf("%f\n",i);
            }
            return 0;
    }
    And you'll see the imprecision show up (before that, it is there, just many decimal places away). "Precision" is limited by the number of bits used, hence, it is finite. Doubles have twice as many bits as floats, and so capable of more precision (but they are still not perfect, ie, infinitely precise). By default, numbers like "0.7" are considered doubles. If you assign that to a float, the compiler takes care of it, and the number loses some precision. So then you compare it to "0.7" (a double) and they are not equal -- the float will be less because of the precision issue. But if you use "0.7f", the compiler knows this is a float and the two numbers will match.

    However, just adding f is not a total solution. In general, never use == with floats or doubles after you've done some arithmetic; always use ranges, eg:

    Code:
    // instead of == 0.7
    if (a < 0.7001f && a > 0.6999f)
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    14
    that was great ......thanks everyone

  6. #6
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    A useful overview of floating-point comparison can be found here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  2. Return to menu after switch executes
    By wco5002 in forum C++ Programming
    Replies: 5
    Last Post: 07-06-2008, 10:46 PM
  3. Program executes, but doesn't start
    By Marauder_Pilot in forum C Programming
    Replies: 1
    Last Post: 11-30-2007, 05:28 PM
  4. CreateThread() executes ONLY once ... ?
    By SyntaxBubble in forum Windows Programming
    Replies: 5
    Last Post: 11-12-2003, 12:34 PM
  5. Executes Smoking Pot
    By CodeMonkey in forum Game Programming
    Replies: 6
    Last Post: 03-26-2002, 02:40 PM

Tags for this Thread