Like Tree3Likes
  • 1 Post By CommonTater
  • 1 Post By Salem
  • 1 Post By MK27

how it executes

This is a discussion on how it executes within the C Programming forums, part of the General Programming Boards category; 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 ...

  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.
    Rohit88 likes this.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,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.
    Rohit88 likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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)
    Rohit88 likes this.
    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, 04:28 PM
  4. CreateThread() executes ONLY once ... ?
    By SyntaxBubble in forum Windows Programming
    Replies: 5
    Last Post: 11-12-2003, 11:34 AM
  5. Executes Smoking Pot
    By CodeMonkey in forum Game Programming
    Replies: 6
    Last Post: 03-26-2002, 01:40 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21