FAQ: Difference between revisions

272 bytes added ,  20 September 2012
Line 526: Line 526:
Floating point arithmetic is an approximation '''in binary''' to arithmetic on real or complex numbers. Just like you cannot represent 1/3 exactly in decimal arithmetic (0.333333 is only a rough approximation to 1/3), you cannot represent some decimals like 0.1 exactly in base 2. In binary, the representation to one tenth is <math>0.\overline{00011}</math> where the bar indicates that it repeats infinitely (like how <math>1/3 = 0.\overline{3}</math> in decimal). Because this infinite repetition cannot be represented exactly, rounding errors occur for values that appear to be exact in decimal but are in fact approximations in binary, such as for example how 0.3 - 0.2 - 0.1 is not equal to zero.
Floating point arithmetic is an approximation '''in binary''' to arithmetic on real or complex numbers. Just like you cannot represent 1/3 exactly in decimal arithmetic (0.333333 is only a rough approximation to 1/3), you cannot represent some decimals like 0.1 exactly in base 2. In binary, the representation to one tenth is <math>0.\overline{00011}</math> where the bar indicates that it repeats infinitely (like how <math>1/3 = 0.\overline{3}</math> in decimal). Because this infinite repetition cannot be represented exactly, rounding errors occur for values that appear to be exact in decimal but are in fact approximations in binary, such as for example how 0.3 - 0.2 - 0.1 is not equal to zero.


This isn't an Octave bug. It happens with any program that uses [http://en.wikipedia.org/wiki/IEEE_754 IEEE 754 floating point arithmetic]. The reason why Octave and other programs use IEEE 754 floats is that they are '''fast''', because they are implemented in hardware, in your computer's processor. If you need arbitrary-precision arithmetic, which is inevitably slower as it has to be implemented in software instead of hardware, you may use the vpa function from the symbolic package.
This isn't an Octave bug. It happens with any program that uses [http://en.wikipedia.org/wiki/IEEE_754 IEEE 754 floating point arithmetic]. The reason why Octave and other programs use IEEE 754 floats is that they are '''fast''', because they are implemented in hardware, in your computer's processor.  
 
Like death and taxes, rounding errors are a fact of life. You cannot avoid them. You can only move a rounding error from one part of a computation to another, or you can use more precision and delay the rounding error. One way to delay the rounding error is to use arbitrary precision arithmetic, which is inevitably slower as it has to be implemented in software instead of hardware. You may use the vpa function from the symbolic package for this purpose.


To learn more about floating point arithmetic, consult [http://en.wikipedia.org/wiki/Floating_point_arithmetic its Wikipedia article] or the classical reference [http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html What Every Computer Scientist Should Know About Floating Point Arithmetic].
To learn more about floating point arithmetic, consult [http://en.wikipedia.org/wiki/Floating_point_arithmetic its Wikipedia article] or the classical reference [http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html What Every Computer Scientist Should Know About Floating Point Arithmetic].