SourcePro : Currency Module User’s Guide : Technical Notes : Base 2 Representation of Decimal Fractions
Base 2 Representation of Decimal Fractions
The following table illustrates the nature of errors caused by base 2 representation of fractions. The table shows the value of 0.1 approximated in base 2, using increasing numbers of bits.
Table 8 – Errors caused by base 2 representation of fractions 
Bits
(1/2)^bits
Base 2. approximation
Error
1
0.50000000000000
0.00000000000000
0.10000000000000
2
0.25000000000000
0.00000000000000
0.10000000000000
3
0.12500000000000
0.00000000000000
0.10000000000000
4
0.06250000000000
0.06250000000000
0.03750000000000
5
0.03125000000000
0.09375000000000
0.00625000000000
6
0.01562500000000
0.09375000000000
0.00625000000000
7
0.00781250000000
0.09375000000000
0.00625000000000
8
0.00390625000000
0.09765625000000
0.00234375000000
9
0.00195312500000
0.09960937500000
0.00039062500000
10
0.00097656250000
0.09960937500000
0.00039062500000
11
0.00048828125000
0.09960937500000
0.00039062500000
12
0.00024414062500
0.09985351562500
0.00014648437500
13
0.00012207031250
0.09997558593750
0.00002441406250
14
0.00006103515625
0.09997558593750
0.00002441406250
15
0.00003051757812
0.09997558593750
0.00002441406250
16
0.00001525878906
0.09999084472656
0.00000915527344
17
0.00000762939453
0.09999847412109
0.00000152587891
18
0.00000381469727
0.09999847412109
0.00000152587891
19
0.00000190734863
0.09999847412109
0.00000152587891
20
0.00000095367432
0.09999942779541
0.00000057220459
The values in the table are not exact, since they were generated using the built-in base 2 floating point types and have been printed to a limited precision. As you can see, the size of the error decreases as a larger number of bits are used. Unfortunately, it will never be zero for a finite number of bits. As a result, calculations performed using a base 2 representation produce counter-intuitive and problematic results in many circumstances. For example, the following code fragment will result in an error message:
 
double hundredth = 0.01;
double sum = 0.0;
for(int j=0; j<100; j++) sum += hundredth;
sum -= 1.0;
if(sum != 0.0) cout << “error!\n”;