# Floating point visualised: IEEE 754, exponent and mantissa

> Interactive floating-point encoding: sign, characteristic (excess-q) and mantissa step by step, with bit visualisation and rounding error.

Address: https://marckimmel.de/en/informatik/zahlenkodierungen/gleitkomma
Deutsche Fassung: https://marckimmel.de/informatik/zahlenkodierungen/gleitkomma.md

IEEE 754 encoding with sign, characteristic (Excess-q) and mantissa (fixed-point). Constant relative error.

Floating-point c_(GK,k,n) encodes a real in normalised form m * 2^e: 1 sign bit, n-k characteristic bits (Excess-q with q = 2^(n-k-1) - 1), k-1 mantissa bits (fixed-point without leading 1). Reserved bit patterns for zero, ±infinity, NaN and subnormals. Absolute rounding error grows with the exponent, but relative error stays bounded by 2^(-k).

In C provided as float (binary32, k=24, n=32) and double (binary64, k=53, n=64). Because of catastrophic cancellation and non-associative arithmetic, sum numbers grouped by magnitude. Always compare with a tolerance instead of ==.

## Frequently asked questions

### What is the difference between fixed point and floating point?

In fixed point the binary point sits at a fixed position, the absolute error is the same everywhere and the range is small. Floating point stores a mantissa and an exponent, so the point moves with the magnitude. This makes the range very large, and the relative error stays the same across all magnitudes.

### How is an IEEE 754 float built?

A float (binary32) has 32 bits: 1 sign bit, 8 exponent bits in excess-127 code and 23 mantissa bits without the leading 1, which is fixed by normalisation. A double (binary64) has 1 sign bit, 11 exponent bits in excess-1023 code and 52 mantissa bits.

### Why is 0.1 + 0.2 not exactly 0.3?

0.1 and 0.2 have no finite representation in binary, just like 1/3 in decimal. Both are rounded to the nearest representable number, and their sum does not hit the rounded 0.3 exactly. In double, 0.1 + 0.2 therefore gives 0.30000000000000004. Floating point numbers are compared with a tolerance, not with ==.

## Related

- [Number Encodings](https://marckimmel.de/en/informatik/zahlenkodierungen.md)
