Thread: float output

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    24

    float output

    Code:
    main() 
    { 
    float  a=  0.7  ; 
    if  (a<  0.7) 
    printf  ( "c" ) ; 
    else 
    printf  (  "C++· )  ; 
    }


    explain the output

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    How about, for something different, you explain the output? You will learn much more by doing that than you will if someone spoon-feeds you the answer.

    Relevant information about floating point is readily available. In fact, any basic text on numerical analysis explains what is going on.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    .
    Join Date
    Nov 2003
    Posts
    307
    This is easy to understand:
    http://www.cygnus-software.com/paper...ringfloats.htm

    This is more useful and higher level - David Goldberg's article:
    What Every Computer Scientist Should Know About Floating-Point Arithmetic

  4. #4
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Make that
    Code:
    #include <stdio.h>
    int main(void)
    { 
        float a = 0.7; 
        if  (a < 0.7) 
            printf("C\n") ; 
        else 
            printf("C++\n");
        return 0;
    }
    In C, 0.7 is a double-precision floating point constant (i.e. of double type), whereas 0.7f would be a single-precision floating-point constant (i.e. of float type).

    Since a is a float, you're comparing a float to a double, i.e. two floating-point numbers with different precision.

    Seven tenths, 7/10, in binary is 0.10110b (= 2-1+2-3+2-4+... = 0.5+0.125+0.0625+...), where the sequence 0110 repeats forever. It cannot be expressed exactly in finite number of binary digits. Therefore, different binary floating-point representations can only approximate it.

    (Negative powers of ten cannot be represented exactly by any finite number of binary digits, because 1/10 = 0.000011b in binary (0011 repeating forever). Therefore, many decimal numbers can only be approximated by binary floating-point formats. Smallish integers in magnitude (up to ±224 for floats, and ±253 for doubles) can be represented exactly, though.)

    For IEEE-754 float (binary32), 7/10 = 1.01100110011001100110011×2-1.

    For IEEE-754 double (binary64), 7/10 = 1.011001100110011001100110011001100110011001100110 0110×2-1.

    Why on earth would anyone think those two values are the same?

    By the way, the two values are stored in memory as
    Code:
        Sign Exponent           Mantissa 
    0.7f = 0 01111110     (1)01100110011001100110011
    0.7  = 0 01111111110 (1)0110011001100110011001100110011001100110011001100110
    on all architectures that support IEEE binary32 and binary64. The (1) is implicit, not stored in memory; that's the way the formats work. (If you count them, the first has 32 bits, and the second 64 bits.)

    I intentionally wrote this in a way that avoids explicitly expressing the reason in a form you can write down as an answer. I really like to help, but I don't like doing your homework for you.

    However, everything you need to understand the situation and the reason, and to formulate a precise, correct answer, is contained in this post. All you need is to read and understand.
    Last edited by Nominal Animal; 06-21-2013 at 02:06 PM.

  5. #5
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    It's not exactly 0.7, and if you see that it is, it's being shown as a rounded value.

    For instance:
    Code:
    float a = 0.7;
    printf("%f\n", a);
    OUTPUT:
    Code:
    0.700000
    Now, let's redefine this a bit:
    Code:
    float a = 0.7;
    printf("%.20f\n", a);
    OUTPUT:
    Code:
    0.69999998807907104492
    Typically because of this lack of precision with floating point datatypes because they are representations of special integral values in the background.. We use an epsilon to check and compare. DBL_EPSILON or FLT_EPSILON from float.h.

    Code:
    int main()
    {
    	double d1 = 0.1 * 7.0;
    	double d2 = 0.7;
    	printf("1. %.20f\n2. %.20f\n", d1, d2);
    	if (abs(d1 - d2) <= DBL_EPSILON)
    	{
    		printf("Equal!\n");
    	}
    	return ERROR_SUCCESS;
    }
    Obviously each constant for each respective datatype.
    Last edited by cstryx; 06-23-2013 at 04:51 AM.

  6. #6
    Registered User
    Join Date
    Jun 2013
    Posts
    5
    u hav given "a" a value 0.7 ... now a equals to 0.7 .. in the next line u hav made conditions.. since a == 0.7 n its not less than 0.7 so the if condition will not be executed and the codes after else statement will be executed..
    so the print out will be .. c++ .. but it gives c ... replace the " < " with a " > " u will get c++ ...

  7. #7
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    Quote Originally Posted by Rbsupercool View Post
    u hav given "a" a value 0.7 ... now a equals to 0.7 .. in the next line u hav made conditions.. since a == 0.7 n its not less than 0.7 so the if condition will not be executed and the codes after else statement will be executed..
    so the print out will be .. c++ .. but it gives c ... replace the " < " with a " > " u will get c++ ...
    You didn't explain anything.. This right here is wrong though:
    Quote Originally Posted by Rbsupercool View Post
    u hav given "a" a value 0.7 ... now a equals to 0.7
    Technically a is not 0.7... Thus, this is wrong too:
    Quote Originally Posted by Rbsupercool View Post
    since a == 0.7 n its not less than 0.7
    Quote Originally Posted by Rbsupercool View Post
    so the print out will be .. c++ .. but it gives c ... replace the " < " with a " > " u will get c++ ...
    The point was not to try and get "c" as the output, but to explain the code, and perhaps why it is printing out "c" and not "C++" as one would expect.
    Last edited by cstryx; 06-23-2013 at 05:56 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. double/float output is inaccurate.
    By slee8251 in forum C Programming
    Replies: 8
    Last Post: 03-13-2012, 01:27 AM
  2. Replies: 3
    Last Post: 12-05-2011, 11:32 PM
  3. Replies: 8
    Last Post: 07-08-2005, 09:12 AM
  4. float gives weird output
    By Xarr in forum C Programming
    Replies: 4
    Last Post: 05-25-2004, 07:44 PM
  5. Unresolved external 'convert(float, float)'
    By Ipsec Espah in forum C++ Programming
    Replies: 4
    Last Post: 05-21-2003, 10:08 AM