makadeade
home about archive (feed)

Decimal places in IEEE 754

Published: June 25th 2026 (week 26 of 2026) in programming

IEEE 754, the floating-point number standard, has finite precision. The number of decimal places a double-precision IEEE 754 number can represent is 15. It was not something I have really thought about until it bit me.

I used the following code to implement the natural logarithm of 2 in Viua VM's assembly language:

Natural logarithm of 2, hardcoded
.symbol ln2
.label ln2
    double $0.l, 0.693147180559945309417232121458
    return $0.l

If you are aware of IEEE 754 limitations, you can immediately see the problem in the above snippet. Otherwise, the code will look fairly innocuous: it is just setting a register to a fixed value, and then returning it. Surely, there is nothing wrong with that.

Turns out that there is plenty of wrong with that, because the value given cannot be accurately represented by a double-precision IEEE 754. I took the hardcoded value from Wikipedia, where it is given with the precision of 30 decimal places. That is very precise. In fact, a bit too precise if what you have are double-precision floats; you would need quadruple-precision ie, 128-bit wide, floats to represent the value accurately.

But where exactly is the problem here?

In the test suite. Since the value could not be accurately represented by my chosen data type the code was returning two different values, seemingly at random. Sometimes the output was 0.6931471805599453, sometimes 0.693147180559945. The difference is the 16th digit — the 3 was not always there. It had all the rights to behave weirdly, but I could not figure out what exactly was happening.

And then, quite a bit frustrated, I have checked how many decimal digits can a double-precision IEEE 754 number represent.

Lo and behold... the limit is 15.

Turns out I was dumb. It was not the first time, and, I am certain, neither was it the last.

Tags

Previous: Linux distribution timeline (1992-2010)