What Are Floating Point Numbers?
Let’s begin with a definition of floating point numbers. Real numbers are expressed as fixed point numbers or floating point numbers. A fixed point number has a radix point, or decimal point, at a fixed location somewhere in the collection of numbers. For example:
This number represents 100 with two digits or hundredths of value, the two zeroes in this case to the right of the decimal point. We’re used to seeing this sort of fixed point number when we see or write down amounts of money.
With floating point numbers, the decimal point might appear in many possible places in a set of numbers. Floating point numbers contain a collection of numbers, a base number, and an exponent to help determine where to place the decimal point. For example:
100.22 = 10022 x 10-2
In this example, our currency value of 100.22 can be represented in a computer as 10022 (called the significand) times 10 (the base number) with -2 as exponent. The relative position of the decimal point is indicated by the exponent. The -2 exponent tells the computer to place the decimal point 2 spaces counting from the right least significant edge of the number, which results in 100.22. If the exponent was a positive number, we would count from the left.
Floating point numbers make it easy to manage very large and small numbers. For example, an astronomer might want to do calculations with the speed measured in seconds of a moon around a planet.
Here’s another example:
100.220011 = 10022 x 10-6
In this example, the exponent -6 positions the decimal point six spaces counting from the right edge of the number, or 100.220011. While we have used base 10 in these two examples, computers can use 2 (binary), 10 (decimal), or 16 (hexadecimal) as base numbers. You also could calculate with base 3, in some cases. Base 10 is simplest to demonstrate here.
Floating Point Numbers and Computers
In computer hardware and software, fixed point numbers can cause problems when they’re computed and stored. Our 100.22 fixed point number, for example, would be translated by a computer into a binary number. The computer also would have to manage the decimal point information when doing calculations with the number. While floating point numbers include the number with a base number and exponent — for example, 10022 x 10-2 — to indicate decimal point location, processing this information takes computer resources.
Every computer has a special chip called an FPU or Floating Point Unit. Computer speed also can be measured in terms of FLOPS, or Floating Point Operations Per Second, the speed the computer can process floating point numbers.
Which leads to an interesting point: there are different ways to calculate floating point numbers. Since the 1990s most computers have used the IEEE 754 standard to ensure computers return the same results when calculating these numbers.
Here’s another interesting point about how computers store and compute numbers: results do not have to be precise. They only have to be reasonable. Programmers who code with floating point numbers, for example, to store and retrieve numbers in a database, must pay close attention to ensure their code does not accidentally change these numbers.
How does this work in the real world? A bank might use a computer to store a transaction for $100.22 as a fixed number or 10022 x 10-2 as a floating point number, then use code to represent the value as dollars and cents only when displayed in a web page or other software. The computer stores the number in a way that works best for computers while humans see the value they recognize as currency.
There is a lot more to floating point numbers and computing, of course. Follow the links below to discover more details.
Floating Point Numbers
IEEE 754 Floating Point Number Standard
What can be done to programming languages to avoid floating point pitfalls?
Floating point arithmetics in 19 programming languages
Model Monetary Data
This tutorial shows how you handle monetary data and floating point numbers with the MongoDB database. It’s a great practical example of how coders think through the problem.