Skip to content
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

RFC: Float <-> UInt8/ByteArray conversions #6071

Closed
seanmcl opened this issue Nov 13, 2024 · 1 comment · Fixed by #6094
Closed

RFC: Float <-> UInt8/ByteArray conversions #6071

seanmcl opened this issue Nov 13, 2024 · 1 comment · Fixed by #6094
Labels
P-high We will work on this issue RFC Request for comments

Comments

@seanmcl
Copy link

seanmcl commented Nov 13, 2024

Proposal

In order to reason about Python's ndarray type stored on disk, we need to be able to read a file of bytes and interpret it as a Lean Array of Float. Since the float bits are represented in IEEE-754 binary format, we need a way to go from UInt64 or ByteArray to Float and vice versa. There seems to be no way to accomplish this today.

For example, the following code should

#eval
  let f := 1.67
  let n : UInt64 := 0x3FFAB851EB851EB8  -- Closest double to 1.67 in hex
  let f' : Float := Float.ofUInt64Bytes n
  let n' := f.toUInt64Bytes
  f == f' && n == n'

should eval to true.

A brief thread on Zulip where it was deemed impossible to do this today.

@seanmcl seanmcl added the RFC Request for comments label Nov 13, 2024
@leanprover-bot leanprover-bot added the P-high We will work on this issue label Nov 15, 2024
@Kha
Copy link
Member

Kha commented Nov 15, 2024

RFC accepted! Implementation to follow soon.

github-merge-queue bot pushed a commit that referenced this issue Nov 15, 2024
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 #6071
JovanGerb pushed a commit to JovanGerb/lean4 that referenced this issue Jan 21, 2025
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P-high We will work on this issue RFC Request for comments
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants