1. Strange issue in floating point addition

Hi All,
I got a program which declares a float variable as f.
and then iterates a for loop for 10 times adding a 0.1f to this variable
well u assume the value is 1.0f but it is not true the value is something like 1.0000001
why is this so. I tried this on windows using visual studio

Regards,
Stephen

2. I ran this code in linux/debian

Code:
```#include<stdio.h>
int main()
{
double a=0.0;
int i ;
for  (i = 1 ; i<=10 ; i++)
{
a+=0.1f;
printf("value : %f\n",a);
}
}```
This works fine.

Output is :
Code:
```value : 0.100000
value : 0.200000
value : 0.300000
value : 0.400000
value : 0.500000
value : 0.600000
value : 0.700000
value : 0.800000
value : 0.900000
value : 1.000000```

3. Originally Posted by Alexander jack
This works fine.
Try some more output precision
Code:
```#include <stdio.h>
int main()
{
double a=0.0;
int i ;
for  (i = 1 ; i<=10 ; i++)
{
a+=0.1f;
printf("value : %f  %0.9f\n",a,a);
}
}```
Output:
Code:
```value : 0.100000  0.100000001
value : 0.200000  0.200000003
value : 0.300000  0.300000004
value : 0.400000  0.400000006
value : 0.500000  0.500000007
value : 0.600000  0.600000009
value : 0.700000  0.700000010
value : 0.800000  0.800000012
value : 0.900000  0.900000013
value : 1.000000  1.000000015```
Kurt

4. Any idea why the answer is so

Hi ZUK,
Any idea why this sort of thing is happening , is it something to do with floating point addition algorithm used by the processor.

Regards,
Stephen

5. The idea behind floating point is that it will NOT equal any particular decimal number, exactly. What it will do, if you work it right, is give you the answer beyond any reasonable resolution your program requires.

It's a bit like Pi. We know that Pi approximately equals 3.1, but that isn't enough resolution for a lot of calculations. OK, how about 3.14159265358979?

It's still NOT the value of Pi, but it's close enough to get you within one 1/100th of a millimeter, in a circle extending out to Jupiter.

So that's probably good enough.

There are ways of "normalizing" floats to an exact value, but it doesn't alter the fact that they have limits to their resolution, whether normalized or not.

6. Floating point numbers are stored in the form of 1.Nx2^M. This is much like scientific notation, except with base 2 instead of 10. As a result numbers such as 1/10 cannot be expressed accurately, because it would require infinate precision to represent it. This is similar to how 1/7 or 1/9 cannot be represented with a finite number of digits in base 10.