Floating-Point Numbers

From Jonathan Gardner's Tech Wiki
(Redirected from Float)
Jump to: navigation, search

Floating point numbers are represented in many different ways. All you really need to know are its limitations.

Floating point numbers are really <math>m \times 2^e</math>, where <math>m</math> is the mantissa, and <math>e</math> is the exponent. Note that both are integers.

The limitations are this:

  • Only so many bits are used to represent that mantissa. This is the bit that goes before the 'e'.
  • Only so many bits are used to represent the exponent. This is the bits that goes after the e.

The net effect is that you get situations where something should be equal to zero, but it is not. There is a bit of error in the numbers.

You will also see that numbers that are not expressable in base 2 are not quite right. For instance, 1/5 is really 0.2000000000001, or something like that.

If you want to compare two floating-point numbers, you really have to see if they are close. That is, if the absolute value of the difference of the two numbers is less than a very small number.

If you need better precision, say for an accounting app, you really need to use rational numbers. If you need even better precision, to handle values like <math>\pi</math> precisely, you'll need to use real numbers.