1. floating point issues

The following piece of code outputs 0.1000000000001

Code:
```float a=0.1;
printf("%lf",a);```
But if I add a 10 times as

Code:
```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

2. 0.1 does NOT have an exact FP representation (it's an approximation).
So adding 10 of them together will always result in an approximation for 1.0, rather than 1.0 exactly.

What Every Computer Scientist Should Know About Floating-Point Arithmetic

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

4. There is no rule that says the floating point approximation of 0.1 exceeds 0.1. It can be less - whether it is or not is a property of the floating point representation supported by your compiler and host system.

And 0.1000000000001 cannot be represented exactly using floating point either.

5. Originally Posted by Saurabh Mehta
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.
Maybe the intermediate computations (which might have been performed at compile time) resulted in rounding to a value very slightly less than 0.2, 0.3, etc. Maybe the conversion from float to long double (long float?! What?!) resulted in a slight change of value when the approximation was made.

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

To start, 0.1 by itself is a double constant, so line 1 involves a 'double-to-float' conversion.
Then on line 2, there is a 'float-to-double' conversion to call printf.

> The following piece of code outputs 0.1000000000001
A float has approximately 6 decimal digits of precision. So anything after the 6th digit is just random rounding noise.
For a double, it's about 15 decimal digits.

But since the lowest precision used was a float, you can't really trust past 6 digits anyway.