EN

IEEE 754 floating-point and fixed-point converter

float, double, half, bfloat16 and FP8 bit by bit, Modbus byte order and Q format

Number systems: Binary and hexadecimal Gray code BCD codes Floating and fixed point Parity, Hamming and CRC

IEEE 754 floating-point converter

Enter a decimal number and see how it is stored in each format: the sign, exponent and mantissa bits, the exact value that ends up stored and the rounding error. You can also type the hexadecimal or click the bits one by one.

Real number
Try:

sign · exponent · mantissa (fraction)

The same value in every format

Byte order of a 32-bit REAL (Modbus)

A float takes up two 16-bit registers. If the value you read makes no sense, try another order: it is the most common fault when reading a REAL over Modbus.

Fixed-point converter (Q format)

In fixed point the number is stored as an integer and the binary point sits at an agreed position: with n fractional bits, the integer is \(x \cdot 2^n\). It is common in DSPs, microcontrollers without a floating-point unit and some analog cards.

Q format

IEEE 754 floating point

To store very large or very small numbers, scientific notation in base 2 is used. The IEEE 754 standard splits the bits into three fields: sign s, exponent e (stored in excess, with a bias) and mantissa f (only the fractional part, because the leading 1 is implied):

\[ x = (-1)^{s} \cdot 1.f_2 \cdot 2^{\,e - \text{bias}} \qquad \text{bias} = 2^{E-1} - 1 \]
Example: 12.375 in binary32 (float)

\(12.375 = 1100.011_2 = 1.100011_2 \cdot 2^3\). Sign 0. Exponent \(3 + 127 = 130 = 1000\,0010_2\). Mantissa: the bits after the point, \(100\,0110\,0000\dots\). Putting it all together: \(0\,10000010\,10001100000000000000000 = \text{0x41460000}\).

Special values

Formats

Why 0.1 + 0.2 is not 0.3

0.1 in binary is recurring (\(0.0\overline{0011}_2\)), so it is rounded: in float 0.100000001490116119384765625 is stored. In double, the sum 0.1 + 0.2 gives 0.30000000000000004. That is why two real numbers are not compared with ==, but by checking whether their difference is smaller than a tolerance. Nor is it a good idea to use a REAL as a counter: from 16,777,216 (\(2^{24}\)) upwards a float can no longer add 1.

Fixed point and scaling

In Qm.n format there are m integer bits and n fractional bits. In Q15 (signed 16 bits, n = 15) values from −1 to almost +1 are represented with a resolution of \(2^{-15} \approx 3 \cdot 10^{-5}\): 0.5 is stored as 16384 (0x4000). It is fast and predictable, but you have to watch the range because, if it is exceeded, the value saturates or wraps around.

PLCs apply the same idea to analog inputs: in Siemens, 0–10 V or 4–20 mA are read as an integer from 0 to 27648, so 13824 corresponds to 50 %. That scaling is done by functions such as SCALE or NORM_X.

Related: binary and hexadecimal converter, ADC and DAC conversion and 4-20 mA transmitter.