Stream: ideas

Topic: `F32.to_bits_lossless()`


view this post on Zulip notramo (Oct 07 2026 at 08:25):

What is the reason for lossy .to_bits() on F32 and F64?

A .to_bits_lossless() would be useful, that would return the exact bits I put in via .from_bits(). It would allow a more ergonomic API for the CBOR library I'm working on.

view this post on Zulip Richard Feldman (Oct 07 2026 at 12:12):

it would break a language-wide invariant we intentionally have around all Roc pure functions giving the same answers for the same arguments regardless of what target they're running on

view this post on Zulip Richard Feldman (Oct 07 2026 at 12:13):

fighting different CPUs can represent NaNs using different bit patterns

view this post on Zulip Richard Feldman (Oct 07 2026 at 12:13):

they all semantically count as NaN, so their meaning doesn't currently change the behavior of Roc programs

view this post on Zulip Richard Feldman (Oct 07 2026 at 12:14):

but if we expose the exact bits without normalizing NaNs first, we lose that entire language-level guarantee :sweat_smile:

view this post on Zulip Richard Feldman (Oct 07 2026 at 12:20):

so we intentionally normalize anytime the bits would become observable

view this post on Zulip notramo (Oct 07 2026 at 12:50):

Does it change also the negative zero and infinity? Or only NaN?

view this post on Zulip notramo (Oct 07 2026 at 12:55):

The CBOR spec allows for CBOR-based protocols to specify what to do with NaN, infinity and negative zero. Either accept, reject, or normalize all of them or just some. To allow later validation, these properties should not be lost after extracting the numbers from the CBOR serialized blob.


Last updated: Oct 15 2026 at 17:56 UTC