tallge

Tools / Number bases and floats

Number base converter and IEEE 754 float inspector

Two converters on one page. The first shows an integer in every base and as the bytes a program would write. The second shows exactly which value a float stores, and why 0.1 + 0.2 is not 0.3.

Integer

Decimal, or with 0x, 0o or 0b; underscores allowed

Examples:

Floating point

For example 0.1, -2.5e-8, inf, or 0x3fb999999999999a

Examples:

Two’s complement

A negative integer in n bits is stored as 2n minus its absolute value, so -1 is all ones and the top bit marks the sign. The same bits read as unsigned give a large positive number: 0xFFFFFFFF is 4294967295 unsigned and -1 signed. The tool shows both readings and refuses a number that fits neither, rather than silently cutting it to the width.

Byte order matters as soon as the number leaves the program. Network protocols and most file formats use big-endian (most significant byte first). x86 and most ARM systems store integers little-endian in memory.

Why 0.1 is not 0.1

A float stores a sign, an exponent and a binary fraction. 0.1 has no finite binary fraction, so the nearest float64 is stored: exactly 0.1000000000000000055511151231257827021181583404541015625. The tool prints that exact value, computed with integer arithmetic. It also prints the shortest decimal that reads back to the same float, which is what JavaScript and Python print. That is why 0.1 + 0.2 prints 0.30000000000000004: the sum of two rounded values is not the float nearest to 0.3.

FormatExponent bitsFraction bitsExact integers up toDecimal digits
float32823224 = 16,777,216about 7
float641152253 = 9,007,199,254,740,992about 15 to 17

Above those limits not every integer exists: 16777217 becomes 16777216 as a float32, and 9007199254740993 becomes 9007199254740992 as a float64, which is why JSON ids above 253 get corrupted in JavaScript. Very small numbers lose precision gradually as subnormals before they reach zero.

What was checked

Integer parsing, the bit layouts at 8, 16, 32 and 64 bits and the byte orders were compared with Python's int and int.to_bytes on 6,000 cases. The float64 and float32 bit patterns and exact values of 4,000 decimals were compared with Python's struct and decimal.Decimal. One difference is deliberate: Python refuses to pack a number too large for float32, while IEEE 754 rounding gives infinity, which the tool shows with a warning.