Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

harshit1202

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; }

  • one year ago
  • one year ago

  • This Question is Closed
  1. harshit1202
    Best Response
    You've already chosen the best response.
    Medals 0

    i think it is " f is not 1.0'"

    • one year ago
  2. A.Avinash_Goutham
    Best Response
    You've already chosen the best response.
    Medals 0

    y?

    • one year ago
  3. harshit1202
    Best Response
    You've already chosen the best response.
    Medals 0

    okkkkkkkkk

    • one year ago
  4. Opcode
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • one year ago
  5. harshit1202
    Best Response
    You've already chosen the best response.
    Medals 0

    there is no input statement here

    • one year ago
  6. Opcode
    Best Response
    You've already chosen the best response.
    Medals 0

    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

    • one year ago
  7. Koikkara
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

    • one year ago
  8. Opcode
    Best Response
    You've already chosen the best response.
    Medals 0

    @Koikkara That from the exact site T_T.

    • one year ago
  9. eSpeX
    Best Response
    You've already chosen the best response.
    Medals 0

    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

    • one year ago
  10. KonradZuse
    Best Response
    You've already chosen the best response.
    Medals 1

    ^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.

    • one year ago
    1 Attachment
  11. KonradZuse
    Best Response
    You've already chosen the best response.
    Medals 1

    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.... :'(

    • one year ago
  12. slotema
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

    • one year ago
  13. harshit1202
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • one year ago
  14. KonradZuse
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

    • one year ago
    • Attachments:

See more questions >>>

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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.