How are floating point values presented by the CPU?

Posted: (EET/GMT+2)

 

Programming needs at least two types of numbers: integers (whole numbers) and decimal numbers, aka floating-point values. This post has information about the CPU uses floating point values and how they are stored in RAM.

On most systems, floating point values follow the IEEE 754 format.

A 32-bit floating point number (single precision) is divided into three parts:

Sign (1 bit)
Exponent (8 bits)
Mantissa (23 bits)

The value is calculated from these parts as:

(-1)^sign * 1.mantissa * 2^(exponent - bias)

The exponent is stored with a bias (127 for single precision), which allows both positive and negative exponents.

For example, the value 1.5 is represented approximately as:

Sign:     b0
Exponent: b0111 1111 (d127)
Mantissa: b1000 0000 0000 0000 0000 000

b = binary 1/0, d = decimal

Because of this representation, not all decimal values can be stored exactly.

0.1 + 0.2 != 0.3

This is due to rounding in the binary representation.

When might you need this information? Low-level debugging is one, or looking at raw memory dumps. Hope this helps!