fix: Float32.ofInt (#7277)

This PR fixes a bug in Float32.ofInt, which previously returned a
Float(64).

Closes https://github.com/leanprover/lean4/issues/7264
This commit is contained in:
Sean McLaughlin 2025-03-02 15:22:35 -08:00 committed by GitHub
parent f094652481
commit 255810db64
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 3 deletions

View file

@ -80,9 +80,9 @@ instance : OfScientific Float32 where
def Float32.ofNat (n : Nat) : Float32 :=
OfScientific.ofScientific n false 0
def Float32.ofInt : Int → Float
| Int.ofNat n => Float.ofNat n
| Int.negSucc n => Float.neg (Float.ofNat (Nat.succ n))
def Float32.ofInt : Int → Float32
| Int.ofNat n => Float32.ofNat n
| Int.negSucc n => Float32.neg (Float32.ofNat (Nat.succ n))
instance : OfNat Float32 n := ⟨Float32.ofNat n⟩

View file

@ -36,3 +36,16 @@ info: 0.909297
/-- info: 1606938044258990275541962092341162602522202993782792835301376.000000 -/
#guard_msgs in
#eval (2 : Float32).toFloat ^ (200 : Float32).toFloat
#guard (Float32.ofInt (-1 : Int)).toBits == 0xBF800000
-- 2^24 + 1 is the smallest Nat that can't be represented exactly in FP32 but can in FP64;
-- There are 23 bits of mantissa and an implicit leading 1. Additionally, powers of
-- 2 (within range) are exactly representable in either format.
-- This shows that we are not accidentally representing Float32 with 64 bits;
#guard
let n := Int.pow 2 24
(Float.ofInt n) - (Float32.ofInt n).toFloat == 0
#guard
let n := Int.pow 2 24 + 1
(Float.ofInt n) - (Float32.ofInt n).toFloat != 0