-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(Data/Rat): move Float functions to separate file (#1035)
Co-authored-by: Kim Morrison <[email protected]>
- Loading branch information
Showing
3 changed files
with
31 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
import Batteries.Data.Rat.Basic | ||
import Batteries.Data.Rat.Float | ||
import Batteries.Data.Rat.Lemmas |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/- | ||
Copyright (c) 2022 Mario Carneiro. All rights reserved. | ||
Released under Apache 2.0 license as described in the file LICENSE. | ||
Authors: Mario Carneiro | ||
-/ | ||
import Batteries.Data.Rat.Basic | ||
import Batteries.Lean.Float | ||
|
||
/-! # Rational Numbers and Float -/ | ||
|
||
namespace Rat | ||
|
||
/-- Convert this rational number to a `Float` value. -/ | ||
protected def toFloat (a : Rat) : Float := a.num.divFloat a.den | ||
|
||
/-- Convert this floating point number to a rational value. -/ | ||
protected def _root_.Float.toRat? (a : Float) : Option Rat := | ||
a.toRatParts.map fun (v, exp) => | ||
mkRat (v.sign * v.natAbs <<< exp.toNat) (1 <<< (-exp).toNat) | ||
|
||
/-- | ||
Convert this floating point number to a rational value, | ||
mapping non-finite values (`inf`, `-inf`, `nan`) to 0. | ||
-/ | ||
protected def _root_.Float.toRat0 (a : Float) : Rat := a.toRat?.getD 0 | ||
|
||
instance : Coe Rat Float := ⟨Rat.toFloat⟩ | ||
|
||
end Rat |