Code:main() { float a= 0.7 ; if (a< 0.7) printf ( "c" ) ; else printf ( "C++· ) ; }
explain the output
Code:main() { float a= 0.7 ; if (a< 0.7) printf ( "c" ) ; else printf ( "C++· ) ; }
explain the output
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.
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
Make thatIn 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).Code:#include <stdio.h> int main(void) { float a = 0.7; if (a < 0.7) printf("C\n") ; else printf("C++\n"); return 0; }
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.10110_{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.000011_{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
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.)Code:Sign Exponent Mantissa 0.7f = 0 01111110 (1)01100110011001100110011 0.7 = 0 01111111110 (1)0110011001100110011001100110011001100110011001100110
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.
It's not exactly 0.7, and if you see that it is, it's being shown as a rounded value.
For instance:
OUTPUT:Code:float a = 0.7; printf("%f\n", a);
Now, let's redefine this a bit:Code:0.700000
OUTPUT:Code:float a = 0.7; printf("%.20f\n", a);
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:0.69999998807907104492
Obviously each constant for each respective datatype.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; }
Last edited by cstryx; 06-23-2013 at 04:51 AM.
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:
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.
Last edited by cstryx; 06-23-2013 at 05:56 AM.