-
Notifications
You must be signed in to change notification settings - Fork 450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add Float.toBits
and Float.fromBits
#6094
Conversation
This PR adds raw transmutation of floating-point numbers to and from `UInt64`. Floats and UInts share the same endianness across all supported platforms. The IEEE 754 standard precisely specifies the bit layout of floats. Note that `Float.toBits` is distinct from `Float.toUInt64`, which attempts to preserve the numeric value rather than the bitwise value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow that was fast. Thanks!
I don't think this is sound unless you clear NaN payloads first, or take a proof that the float is a canonical NaN. Because float implementations are nondeterministic in NaN payload bits and this violates the expectation of |
Mathlib CI status (docs):
|
Canonicalization would be costly. Is what's missing is that no pair of nans should compare equal, even identical bit patterns? |
The opposite: two nans with different payloads should be equal according to lean's |
Why should they be equal? IEEE-754 semantics is encodable in Lean. |
If NaNs are not treated as lean-equal, then you have much worse consequences: IMO the most pragmatic solution which solves peoples' immediate needs here is to just provide this function as |
The function is defined as |
Even |
Ok, I think get the issue now. Hmm, tricky. |
This may be of interest: double-float support in the ACL2 theorem prover: |
So an example of this concern is something like having distinct NaN bit patterns, |
Two common reasons you might encounter nondeterminism like that:
|
Note that this is not the first time this issue has come up, see #1459 for nondeterminism leakage through the |
Does #6097 address these concerns raised here? |
Yes it does. Was there also a function exposed for converting float arrays to byte arrays? That might need the same treatment. |
This PR adds raw transmutation of floating-point numbers to and from `UInt64`. Floats and UInts share the same endianness across all supported platforms. The IEEE 754 standard precisely specifies the bit layout of floats. Note that `Float.toBits` is distinct from `Float.toUInt64`, which attempts to preserve the numeric value rather than the bitwise value. closes leanprover#6071
This PR adds raw transmutation of floating-point numbers to and from
UInt64
. Floats and UInts share the same endianness across all supported platforms. The IEEE 754 standard precisely specifies the bit layout of floats. Note thatFloat.toBits
is distinct fromFloat.toUInt64
, which attempts to preserve the numeric value rather than the bitwise value.closes #6071