FAQ: Difference between revisions

1 byte added ,  21 July 2014
Line 580: Line 580:
== 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 fractions like <math>1/10</math> 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 with a finite number of digits, 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 fractions like <math>1/10</math> exactly in base 2. In binary, the representation to one tenth is <math>0.0\overline{0011}</math> where the bar indicates that it repeats infinitely (like how <math>1/6 = 0.1\overline{6}</math> in decimal). Because this infinite repetition cannot be represented exactly with a finite number of digits, 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. Unless you are using very exotic hardware, Octave will use your computer's processor for floating point arithmetic.
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. Unless you are using very exotic hardware, Octave will use your computer's processor for floating point arithmetic.