TIL: numbers don't always print the same way

Laurent.O
Mon, 02 May 2022 19:16:56 +0000
General

(merely trying out a today-i-learnt tag)

Numbers may not print the same depending on the underlying formatter, even for base Racket. For example:

> (~r 1234567890123456789.123)
"1234567890123456768"
> 1234567890123456789.123
1234567890123456800.0

@ryanc explains why:

It seems to be a consequence of the following:

> (inexact->exact 1234567890123456789.123)
1234567890123456768

Here's what I think is happening:

One view of a floating-point number is that it represents a set (an interval) of reals. The printer's goal is to print some finite representative so that the reader can identify the correct set and produce exactly the same floating-point number. A secondary goal is to prefer zeros in "don't care" positions to avoid misleading humans about how much precision is actually present.

Another view of a floating-point number is that it represents a specific rational number (calculated from the mantissa, exponent, etc). This example represents an integer, and inexact->exact is producing that specific integer.

Arguably, ~r should be doing the first thing, but it's doing the second thing instead. Mea culpa. On the other hand, this behavior is consistent with the docs sentence "The exactness or inexactness of x does not affect its formatting", and the first behavior is not.

5 posts - 4 participants