Code:`main()`

{

float a= 0.7 ;

if (a< 0.7)

printf ( "c" ) ;

else

printf ( "C++· ) ;

}

explain the output

Printable View

- 06-21-2013royroyfloat outputCode:
`main()`

{

float a= 0.7 ;

if (a< 0.7)

printf ( "c" ) ;

else

printf ( "C++· ) ;

}

explain the output - 06-21-2013grumpy
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. - 06-21-2013jim mcnamara
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 - 06-21-2013Nominal Animal
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;

}

*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.1__0110___{b}(= 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.00__0011___{b}in binary (0011 repeating forever). Therefore, many decimal numbers can only be approximated by binary floating-point formats. Smallish integers in magnitude (up to ±2^{24}for floats, and ±2^{53}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

*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. - 06-23-2013cstryx
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);

Code:`0.700000`

Code:`float a = 0.7;`

printf("%.20f\n", a);

Code:`0.69999998807907104492`

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;

}

- 06-23-2013Rbsupercool
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++ ... - 06-23-2013cstryx
You didn't explain anything.. This right here is wrong though:

Technically**a**is not 0.7... Thus, this is wrong too:

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.