lean4-htt/tests/lean/run/float32.lean
Sean McLaughlin 255810db64
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
2025-03-02 23:22:35 +00:00

51 lines
1.3 KiB
Text

/-- info: 2.100000 -/
#guard_msgs in
#eval (2.1 : Float32)
/-- info: 3.200000 -/
#guard_msgs in
#eval (2.1 : Float32) + 1.1
/-- info: 0.900000 -/
#guard_msgs in
#eval (2.1 : Float32) - 1.2
def test1 : IO Unit := do
IO.println ((2 : Float32).sin);
IO.println ((2 : Float32).cos);
IO.println ((2 : Float32).sqrt);
IO.println ((2 : Float32) ^ (100 : Float32));
/--
info: 0.909297
-0.416147
1.414214
1267650600228229401496703205376.000000
-/
#guard_msgs in
#eval test1
/-- info: 0.909297 -/
#guard_msgs in
#eval (2 : Float32).sin.toFloat
/-- info: 0.909297 -/
#guard_msgs in
#eval (2 : Float).sin.toFloat32
/-- 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