Interview Question

  • -
    0
    of 0 votes
    11
    Answers

    Explain the output :
    #include<stdio.h>
    int main()
    { double i=0;
    i+=0.1;
    i+=0.1;
    i+=0.1;
    i+=0.1;
    i+=0.1;
    i+=0.1;
    i+=0.1;
    if(i==0.7)
    printf("Equal : i=0.7\n");
    i+=0.1;
    if(i==0.8) printf("Equal : i=0.8\n");
    else printf("Not Equal : i=0.8\n");
    return 0;
    }

    - Barney on July 06, 2012 in United States Report Duplicate | Flag
    C

Country: United States


Comment hidden because of low score. Click to expand.
0
of 0 vote

output :
Equal : i=0.7
Equal : i=0.8
why because here i==0.7 "0.7" is taken as double and 'i' is also double so it is true,but if we take 'float i' then it is false.

- Siva Krishna on July 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I've executed this program on gcc and also on dev c++ .
and changing double to float , still give me the same answer .
Getting output : Equal : i=0.7
Not Equal : i=0.8
Why is it so ?

- Barney on July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No I've executed it on gcc by replacing double to float it is giving only "Not Equal : i=0.8" as output,plz check once again

- Siva Krishna on July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here
if( i == 0.7) //here i is double and 0.7 gets type caste from float to double

if(i==0.8) // here i is double and 0.8 also gets type caste as above so in both case it should print Not Equal.

Am i correct?

- Arulmozhi on July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes , U r ryt .
But for double i=0;

answer is

Equal : i=0.7
Not Equal : i=0.8

Checked many times.

- Barney on July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

*You cannot compare 2 floats or doubles using == *

Floats and doubles are by their nature imprecise, and doing arthmetic with them causes them to have what's known as roundoff error. 0.1 * 7 may be 0.69999993427 or 0.700000000003482, and thus not equal to 0.7. And 0.1 * 7 is not the same as 0.1 + 0.1 + 0.1 + ... + 0.1 7 times. And for given a, b, c, a+b+c may differ from a+c+b. Perhaps an even more appalling thing is that it's even possible that if you do x=0.1 +0.1; y = 0.1 + 0.1 and then compare x==y later, they may or may not be different!

So is C broken? No. Just don't compare imprecise types with precise comparisons. Instead, always use this style of comparisons:

Const double epsilon = 1e-6; // for example
If (abs(x-y) < epsilon ) { consider them equal; }

- eugene.yarovoi on July 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 votes

Thanks for the elaborate answer

how is that for

if( i == 0.7) //Prints Equal
and
if(i==0.8) //Prints UnEqual

- Arulmozhi on July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Who knows. Maybe the roundoff errors cancel out just right. Point is that you're doing something you're not supposed to by comparing imprecise types with ==, and as a result, the output is difficult to predict.

- eugene.yarovoi on July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Very rightly said. I would also like to add some food for thought.
The actual values that are being stored are not the one being passed, rather they are stores in the form of exponent & mantissa, they get silently rounded off to the approximated values
because of the recurring nature of the binary form being converted. Also,it depends on the architecture, whether the intermediate values are stored somewhere[more likely to get the approximated or rounded values] or the directly used to get the result. Hence a+b+c is different from a+c+b.

- Aashish on July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What shondik is saying is that the hardware doesn't even have any way of representing 0.1 exactly. Since the representation is in what is similar to binary scientific notation (think 1.23434 E +5, that kind of thing, but in binary), things like 0.1 in decimal may actually be nonterminating decimals in binary. Since these binary decimals can only be stored at some finite level of precision, you basically incur roundoff error simply by storing a number.

This reminds me of a Saturday Morning Breakfast Cereal comic:

- eugene.yarovoi on July 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

the reason getting output as "Not Equal : i=0.8 " is that we cant compare float/double as i==0.7. reason is that 0.7 is a constant and i being a float or double would have value as 0.700000 and similarly double i ll have the trailing zeros. this makes them not equal to each other and results in false value.

- Anonymous on July 26, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book walking you through getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More