Skip to content

Commit

Permalink
fix: Inhabited Float produced a bogus run-time value (#6136)
Browse files Browse the repository at this point in the history
This PR fixes the run-time evaluation of `(default : Float)`.
  • Loading branch information
Kha authored Nov 20, 2024
1 parent 2fbc466 commit 7fbe8e3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Init/Data/Float.lean
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ opaque floatSpec : FloatSpec := {
structure Float where
val : floatSpec.float

instance : Inhabited Float := ⟨{ val := floatSpec.val }⟩
instance : Nonempty Float := ⟨{ val := floatSpec.val }⟩

@[extern "lean_float_add"] opaque Float.add : Float → Float → Float
@[extern "lean_float_sub"] opaque Float.sub : Float → Float → Float
Expand Down Expand Up @@ -136,6 +136,9 @@ instance : ToString Float where

@[extern "lean_uint64_to_float"] opaque UInt64.toFloat (n : UInt64) : Float

instance : Inhabited Float where
default := UInt64.toFloat 0

instance : Repr Float where
reprPrec n prec := if n < UInt64.toFloat 0 then Repr.addAppParen (toString n) prec else toString n

Expand Down
1 change: 1 addition & 0 deletions tests/compiler/float.lean
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def tst1 : IO Unit := do
IO.println (Float.ofInt 0)
IO.println (Float.ofInt 42)
IO.println (Float.ofInt (-42))
IO.println (default : Float)
IO.println (0 / 0 : Float).toUInt8
IO.println (0 / 0 : Float).toUInt16
IO.println (0 / 0 : Float).toUInt32
Expand Down
1 change: 1 addition & 0 deletions tests/compiler/float.lean.expected.out
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ true
0.000000
42.000000
-42.000000
0.000000
0
0
0
Expand Down

0 comments on commit 7fbe8e3

Please sign in to comment.