Floating-Point Value
In one sentence: A floating-point number stores a value as a sign, a power of two and a binary fraction, and this formula turns those three parts back into the number.
Read it out loud: “x equals negative one to the power of s, times 1 point f, times 2 to the power of e minus 127. This form covers normal single-precision numbers. x is the number the 32 bits stand for. s is the sign bit, 0 for positive and 1 for negative. 1 point f is the significand: a 1, a point, then the 23 stored fraction bits f. e is the exponent field, the 8 stored bits read as a whole number. The 2 with e minus 127 as a small raised number is the scale, two multiplied by itself that many times.”
Before you start
The ideas and signs this page uses, explained from zero. For powers of ten, Greek letters and other notation in general, see Math basics.
- Powers of ten and scientific notation, like
means 1 followed by 38 zeros, a number so large that it is written as a power. A power of ten is ten multiplied by itself a set number of times, and the small raised number is the exponent. Scientific notation writes a number as a short number times a power of ten. So means 3.4 with the decimal point moved 38 places to the right. A negative exponent moves the point to the left, so .
- Binary fractions, like
A binary fraction works like a decimal fraction, but each place is a power of two. The places after a binary point are worth , , and so on. So means one half, means one quarter, and means three quarters. A fraction such as one tenth has no finite binary form, as the explanation below shows.
- Bits, bytes and 32-bit numbers
A bit is one 0 or 1. A byte is 8 bits. A 32-bit number is a pattern of 32 bits, which is four bytes. The computer can only store a fixed number of bits for each number, so every decimal value it keeps has to fit inside those 32 bits. That limit is the reason floating-point numbers are sometimes not exact.
- Hexadecimal and the prefix 0x
Hexadecimal is base 16, which uses the digits 0 to 9 and the letters A to F. One hex digit stands for exactly four bits. The prefix 0x shows that the digits that follow are hexadecimal. So is a 32-bit pattern written in eight hex digits, and the hex digit D stands for the four bits 1101.
- The IEEE 754 standard
IEEE 754 is the standard that defines how computers store decimal values as binary floating-point numbers. Most processors follow it, so the same bits give the same number on different machines. The standard defines several sizes. This page covers the most common one, single precision, which uses 32 bits.
What each symbol means
| Symbol | Meaning |
|---|---|
| The number that the 32 bits stand for. It is a decimal value with no unit of its own, and its size depends on the bits. The example pattern stands for . | |
| The sign factor. It equals when the sign bit is 0, and when the sign bit is 1. It has no unit, and it only decides whether the number is positive or negative. | |
| The sign bit, the first of the 32 bits. It is 0 for a positive number and 1 for a negative number. It has no unit, because it is one bit. | |
| The significand, a binary number from 1 up to nearly 2. Its leading 1 is implied and not stored, and the 23 stored fraction bits follow the binary point. It has no unit. It holds the digits of the number itself. | |
| The 23 stored fraction bits, written after the leading 1. Bit by bit they are worth one half, one quarter, one eighth and so on. They have no unit and are read as a binary fraction. | |
| The exponent field, 8 stored bits read as a whole number from 0 to 255. It has no unit. The real power of two is , which is why the field is said to have a bias of 127. | |
| The scale factor, a power of two that sets the size of the number. It has no unit. A stored exponent of 130 gives a real power of , so the scale is . |
The formula piece by piece
The 23 stored fraction bits, the smallest detail of the number. Each bit that is 1 adds a fraction, such as one half or one quarter, to the significand. Together they give the digits after the leading 1.
Add the implied leading 1 to the fraction. The result is a binary number from 1 up to nearly 2. An all-zero fraction gives exactly 1, and a fraction of one half gives 1.5.
The sign factor from the sign bit. If the sign bit is 0, the factor is and the number stays positive. If it is 1, the factor is and the number becomes negative.
The scale factor from the exponent field. Subtract the bias 127 from the stored exponent to get the real power, then raise 2 to that power. A stored exponent of 127 gives a scale of 1, and a stored exponent of 128 gives a scale of 2.
The whole formula in one sentence: the number equals the sign factor times the significand times the scale factor, so the sign, the digits and the size are combined into one decimal value.
What the formula is doing, and why
What goes in and what comes out. You give the formula 32 bits: one sign bit , eight exponent bits and 23 fraction bits . It returns the decimal number that those bits stand for. This 32-bit form, called single precision, is one of the most common ways that computers store decimal numbers.
Why it works like scientific notation. A number such as has a sign, a string of digits and a power that sets its scale. A floating-point number does the same in base two. The fraction bits are the digits, the exponent is a power of two that moves the binary point to the right size, and the sign bit gives the plus or minus.
Why the leading 1 is not stored, and why the bias is 127. Every normal number can be scaled so that its binary digits start with 1, as in . Because that leading 1 is always present, the format does not store it, and this gives one more bit of precision. The stored exponent is a whole number from 0 to 255, so the bias 127 is subtracted to give a real power from up to .
Why 0.1 is not exact. In binary, one tenth is , and its denominator has a factor of 5. A fraction has a finite binary form only when its denominator, in lowest terms, is a power of two. So repeats forever in binary, as , with the pattern 0011 repeating. The 23 fraction bits cut the pattern off, so the stored value is . That is very close to 0.1, but it is not equal to it.
Limits of the format. A single-precision value keeps about 7 decimal digits. Whole numbers are exact only up to . The largest finite value is about . Exponent field values 0 and 255 are reserved for zero, very small numbers, infinity and not-a-number, so the normal formula above covers exponent fields 1 to 254.
An everyday picture
Scientific notation on a calculator is the closest everyday idea. The display has room for a fixed number of digits, such as , and the exponent moves the decimal point to the right size. Floating point does the same with bits. The 23 fraction bits are the fixed digits, and the exponent is the power of two that sets the scale.
Worked example: Decoding a 32-bit float
Suppose a game stores one number, the size of a sprite on screen, as the 32 bits . These are the sign, the eight exponent bits and the 23 fraction bits, in that order. So , the stored exponent field is , and the fraction bits give in binary, which is one half.
- Split the 32 bits into three parts. The first bit is the sign bit, the next eight bits are the exponent field, and the last 23 bits are the fraction. Counting them gives , so the split uses every bit exactly once.
- Read the sign. The sign bit is , so the sign factor is . Multiplying by 1 changes nothing, so the number is positive.
- Read the exponent field. The eight bits stand for . The real power of two is , because the bias 127 is subtracted from the stored value. So the scale factor is .
- Read the fraction bits. The 23 fraction bits begin with a 1 and then have 22 zeros. The first bit after the binary point is worth one half, and the rest add nothing. So , and the significand is .
- Multiply the three parts. The formula says , which here is . The sign is positive, the significand is 1.5, and the scale is 4, so these 32 bits store exactly the number 6.
- Check a value that is not exact. The 32-bit pattern for 0.1 is . Its sign bit is 0, and its exponent field is , which is 123, so the real power is . Its 23 fraction bits are a pattern that is cut off, so the significand is about . Multiplying gives , which is close to 0.1 but not equal to it.
Answer: The 32 bits store exactly 6, because the sign is positive, the significand is 1.5 and the scale is 4. Many decimal values, such as 0.1, have no exact binary form, so the computer stores the nearest pattern instead. For 0.1 that stored value is about 0.1000000015.
The numbers in this example are recomputed by script every time the site is built.
Common mistakes
- Expecting decimals to be stored exactly. In Python, with ordinary 64-bit floats gives , not , because each value is rounded to binary first.
- Forgetting the implied leading 1. The stored fraction bits are not the whole significand. An all-zero fraction with a normal exponent means , not .
- Forgetting the bias. A stored exponent of 130 is a real exponent of 3, so subtract 127 before you use it as a power of two.
- Assuming every whole number is exact. Single precision stores whole numbers exactly only up to . The value rounds to .
- Testing floats with after arithmetic. Compare them with a small tolerance instead, because rounding errors add up.
Where you will meet it
- Graphics and game engines, where positions, colors and lighting values are often 32-bit floats.
- Scientific computing in Python, such as the float32 and float64 types in the NumPy library.
- Machine learning, where model weights and activations are often stored as 32-bit or 16-bit floats.
- Java’s float and double types, which follow IEEE 754.
Questions people ask
Why is 0.1 not exact in binary?
Because has a factor of 5 in its denominator, and a fraction has a finite binary form only when its denominator, in lowest terms, is a power of two. Decimal has the same kind of problem with , which never ends.
What is the difference between single and double precision?
Double precision uses 64 bits: 1 sign bit, 11 exponent bits and 52 fraction bits. It keeps about 15 to 16 decimal digits, compared with about 7 for single precision.
Should I use floats for money?
Not for exact totals. Store amounts as whole cents in an integer, or use a decimal type designed for money. Binary rounding can make totals a cent off.
Related formulas
- Binary Number Value: Binary uses only the digits 0 and 1, and this formula turns a row of those digits into the ordinary number they stand for.
- Two's Complement: Two’s complement is the usual way computers write negative whole numbers in binary, and this formula reads a bit pattern as a positive or negative number.
- Hexadecimal Number Value: Hexadecimal counts in sixteens using the digits 0 to 9 and the letters A to F, and this formula turns a hex number into the ordinary number it stands for.
- Bits Needed: This formula counts the fewest bits you need so that every one of N items gets its own pattern of 0s and 1s.