A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 3 years ago
guess the output of following program
#include <stdio.h>
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;
}
anonymous
 3 years ago
guess the output of following program #include <stdio.h> 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; }

This Question is Closed

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0i think it is " f is not 1.0'"

Opcode
 3 years ago
Best ResponseYou've already chosen the best response.0The output varies on what the input is....

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0there is no input statement here

Opcode
 3 years ago
Best ResponseYou've already chosen the best response.0My 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://solutiondisemble.blogspot.com/2011/08/solutioncpuzzle9.html

Koikkara
 3 years ago
Best ResponseYou've already chosen the best response.0This 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.

Opcode
 3 years ago
Best ResponseYou've already chosen the best response.0@Koikkara That from the exact site T_T.

eSpeX
 3 years ago
Best ResponseYou've already chosen the best response.0This 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

KonradZuse
 3 years ago
Best ResponseYou've already chosen the best response.1^You're actually incorrect. It doesn't loop 9 times it loops 10 times. 09 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.

KonradZuse
 3 years ago
Best ResponseYou've already chosen the best response.1What 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.... :'(

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0A 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.

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0the answer is " f is not 1.0'" in my laptop using c editor with help of linux commands

KonradZuse
 3 years ago
Best ResponseYou've already chosen the best response.1The 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.
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.