The following piece of code outputs 0.1000000000001

``````float a=0.1;
printf("%lf",a);
``````

But if I add a 10 times as

``````long float b=0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1;
printf("lf",b);
``````

It outputs as 0.999999999999989
But if a=0.1 is stored as 0.1000000000001 then the answer should be greater than 10 due to the 1 in last column.Then why is the output less then 10

## All 3 Replies

Thankyou for your answer actually I read the link that you gave before I posted this article..But my doubt was that since the value of 0.1 shown above is slightly greater than 0.1 then why does the addition yield a value less than 1.

But my doubt was that since the value of 0.1 shown above is slightly greater than 0.1 then why does the addition yield a value less than 1.

You say you read the article, but then ask a question that belies your assertion. A combination of inaccuracy at higher precisions and arithmetic rounding is why you're getting unexpected results. Read the article again, and this time actually try to understand it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.