Ask your own question, for FREE!
Computer Science 10 Online
OpenStudy (anonymous):

guess the output of following program #include int main() { float f=0.0f; int i; for(i=0;i<10;i++) f = f + 0.1f; if(f == 1.0f) printf("f is 1.0 \n"); else printf("f is NOT 1.0\n"); return 0; }

OpenStudy (anonymous):

i think it is " f is not 1.0'"

OpenStudy (anonymous):

y?

OpenStudy (anonymous):

okkkkkkkkk

OpenStudy (opcode):

The output varies on what the input is....

OpenStudy (anonymous):

there is no input statement here

OpenStudy (opcode):

My bad... I read it too fast. After reading this multiple times I'm puzzled... >.< So I used google... Yush I cheated :( Turns out this is a puzzle lol. The answer can be found here: http://solution-disemble.blogspot.com/2011/08/solution-c-puzzle-9.html

OpenStudy (koikkara):

This is not a bug but a fundamental property of floating point numbers - they are not exact representations of numbers. Floating point numbers are represented by an exponent multiplied by an arithmetic series.There are gaps between floating point numbers where the number suffers from what is called a floating point approximation error. Try this: f=0.1f; printf("%.20f\n" , f); This prints out f to 20 decimal places and you can see the approximation error. If you stick the printf inside the for loop you can see the real result of f is not equal to 1.0 and that's why the test fails. 0.5 for example does not fall between a gap so for(i=0; i<2; i++) f = f + 0.5; works and passes your if condition.... Source: TCS interview GUIDE.

OpenStudy (opcode):

@Koikkara That from the exact site T_T.

OpenStudy (espex):

This will print "f is NOT 1.0" The reason is because you are adding 0.1 to itself 9 times. Your loop is going from 0 to 9 and when it reaches 10, the for statement sees that this is no < 10 and exits. Through the iterations of your program you start with f=0.0 as you enter the loop iteration Value of f 1 0.1 2 0.2 3 0.3 4 0.4 5 0.5 6 0.6 7 0.7 8 0.8 9 0.9 10 loop exits before adding to f

OpenStudy (konradzuse):

^You're actually incorrect. It doesn't loop 9 times it loops 10 times. 0-9 is 10. If you look at i++ it's incremental AFTERWARDS. i = 0 f = 0.1 i = 1 f = 0.2 i = 2 f = 0.3 i = 3 f = 0.4 i = 4 f = 0.5 i = 5 f = 0.6 i = 6 f = 0.7 i = 7 f = 0.8 i = 8 f = 0.9 i = 9 f = 1.0 However for some reason the code does some funky crap, so I need to go see what exactly is going on, however you can see it goes to 1.0, not .9.

OpenStudy (konradzuse):

What REALLY upsets me about this is http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems HFSHFSDAHFSHADFSHDA WHAT"S THE POINT OF FLOATS THEN IF THEY WILL MESS UP SO EASILY.... :'(

OpenStudy (anonymous):

A nice thing about integers is that comparing them is clear: 10 equals 10 and 9 does not euqal 10. With floating point numbers things are a lot less clear. Does 10.01 equal 10? What about 9.999998? There is no clear answer, it all depends on the application. While for some applications 10.01 might equal 10, other applications might need more accuracy. That's why you always need to compare ranges. For example, x equals 10 if it falls between 9.99 and 10.01. One way to write that in C is `fabs( x - 10 ) < 0.01` (fabs is a function defined in math.h). Another issue is that when you try to store a floating point number, let's say 10, it can be stored as 10.00000001. This is due to rounding the number to fit the internal representation (which is the IEEE 754 format most (if not all) of the time. Because of these rounding errors, you should always compare floating point numbers based on their range, as I mentioned above, even if you do really simple calculation like adding 0.1.

OpenStudy (anonymous):

the answer is " f is not 1.0'" in my laptop using c editor with help of linux commands

OpenStudy (konradzuse):

The answer should be 1.0, but yes, it's due to errors, this is when you use commands like BigDecimal() or DecimalFormat(). Floats use Base 2, whereas Ints use Base 10, it can get messy, and as mentioned above it's an accuracy issue.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!