FAQ: Difference between revisions

13 bytes added ,  20 September 2012
Line 524: Line 524:
== Why is this floating point computation wrong? ==
== Why is this floating point computation wrong? ==


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 1/3 = 0.\overline{3} 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. 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.