Your programs can do arithmetic calculations correctly.
For example, with the bc program:
jose@luthien ~ $ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type 'warranty'.
0.1+0.1
.2
0.1+0.2
.3
10*0.67
6.70
10*0.68
6.80
10*0.69
6.90
With Java:
package testmatematicas;
import java.math.BigDecimal;
public class TestMatematicas {
public static void main(String[] args) {
BigDecimal ceroPuntoUno = new BigDecimal("0.1");
BigDecimal suma = ceroPuntoUno.add( ceroPuntoUno );
System.out.println( suma );
BigDecimal ceroPuntoDos = new BigDecimal("0.2");
suma = ceroPuntoUno.add(ceroPuntoDos);
System.out.println( suma );
BigDecimal diez = new BigDecimal("10");
BigDecimal ceroPuntoSesentaYSiete = new BigDecimal("0.67");
BigDecimal multiplicacion = diez.multiply(ceroPuntoSesentaYSiete);
System.out.println( multiplicacion );
BigDecimal ceroPuntoSesentaYOcho = new BigDecimal("0.68");
multiplicacion = diez.multiply(ceroPuntoSesentaYOcho);
System.out.println( multiplicacion );
BigDecimal ceroPuntoSesentaYNueve = new BigDecimal("0.69");
multiplicacion = diez.multiply(ceroPuntoSesentaYNueve);
System.out.println( multiplicacion );
}
}
The output is:
0.2
0.3
6.70
6.80
6.90
And, in fact, in your example the computer is not calculating the sum incorrectly. What the computer is doing is what it always does: Do what you said, STRICTLY, which does not have to be what you want it to do.
When you make 0.1+0.2
it seems that you ask him to add 0.1 to 0.2, whose result should be 0.3
But it's not like that. What you have actually told the computer to do is:
Interpret and execute the expression contained in the string "0.1 + 0.2"
Which entails the following actions:
- Lexicographical analysis that divides the string into 3 symbols (tokens):
0.1
, +
and 0.2
-
Syntactic analysis that, for example (can be of many forms), creates a syntax tree structure:
-
Semantic analysis that converts 0.1 to the nearest value that is representable with the chosen data type, in this case the implicit one. And the same with 0.2. Here is a source of error. The program will not handle the 0.1 value but the closest one that is representable in the chosen data type, which in javascript is double precision as specified in standard IEEE 754 . And in that 0.1 format it is not possible to represent it exactly, so there is a small error.
- Execution. That adds the two values and converts the result to the nearest one that is representable . This is another source of errors.
- Printing. Print a visual representation of the sum value obtained. It may happen that the result is correct (for example 0.125 + 0.125 = 0.25) but that fewer decimal places (0.2) are printed for the chosen printing format. In this case the computer has calculated the exact solution but has shown an approximation. And it can also happen that the result is inaccurate (for example 0.1 + 0.2 = 0.30000000000000004) but that by the chosen printing format, decimals are omitted, giving the appearance of an exact result (.3) even though the sum value stored in the computer is slightly inaccurate.
Conclusions
It is possible to make exact calculations on a computer. There are specific programs and languages for it.
In general-purpose programming languages, such as C ++, Java or Javascript, approximate calculations are made by default, for performance reasons and because it is sufficient for multiple applications. In these languages it is also possible to make exact calculations but you have to use the appropriate libraries and / or language elements.