At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
i think it is " f is not 1.0'"
The output varies on what the input is....
there is no input statement here
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
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.
@Koikkara That from the exact site T_T.
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
^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.
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.... :'(
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.
the answer is " f is not 1.0'" in my laptop using c editor with help of linux commands
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.