Weird floating point vs rational behavior

For Float64/32/16, it’s printing the shortest decimal value that rounds to the same value (when rounded to the nearest binary floating point number).

In particular, it uses a state-of-the-art algorithm called Ryū. (Previously it used another algorithm called Grisu that did the same thing.) In particular, as described in the Ryū paper, the criteria for display are:

as laid out in a classic 1990 paper.

See also these posts:

and many others… (Note, however, that BigFloat printing uses a separate algorithm implemented in the GNU MPFR library, and IIRC it is not always the shortest decimal representation.)

5 Likes