Basics for the Rational Numbers #
Rational numbers, implemented as a pair of integers num / den
such that the
denominator is positive and the numerator and denominator are coprime.
- mk' :: (
- num : Int
The numerator of the rational number is an integer.
- den : Nat
The denominator of the rational number is a natural number.
- den_nz : self.den ≠ 0
The denominator is nonzero.
- reduced : Nat.Coprime (Int.natAbs self.num) self.den
The numerator and denominator are coprime: it is in "reduced form".
- )
Instances For
Equations
- instInhabitedRat = { default := { num := 0, den := 1, den_nz := instInhabitedRat.proof_1, reduced := instInhabitedRat.proof_2 } }
Equations
- One or more equations did not get rendered due to their size.
Equations
- One or more equations did not get rendered due to their size.
Auxiliary definition for Rat.normalize
. Constructs num / den
as a rational number,
dividing both num
and den
by g
(which is the gcd of the two) if it is not 1.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Construct a normalized Rat
from a numerator and nonzero denominator.
This is a "smart constructor" that divides the numerator and denominator by
the gcd to ensure that the resulting rational number is normalized.
Equations
- Rat.normalize num den den_nz = Rat.maybeNormalize num den (Nat.gcd (Int.natAbs num) den) ⋯ ⋯
Instances For
Construct a rational number from a numerator and denominator.
This is a "smart constructor" that divides the numerator and denominator by
the gcd to ensure that the resulting rational number is normalized, and returns
zero if den
is zero.
Equations
- mkRat num den = if den_nz : den = 0 then { num := 0, den := 1, den_nz := mkRat.proof_1, reduced := mkRat.proof_2 } else Rat.normalize num den den_nz
Instances For
Equations
- Rat.instNatCastRat = { natCast := fun (n : Nat) => Rat.ofInt ↑n }
Equations
- Rat.instIntCastRat = { intCast := Rat.ofInt }
Form the quotient n / d
where n d : Int
.
Equations
- Rat.divInt x✝ x = match x✝, x with | n, Int.ofNat d => inline (mkRat n d) | n, Int.negSucc d => Rat.normalize (-n) (Nat.succ d) ⋯
Instances For
Form the quotient n / d
where n d : Int
.
Equations
- Rat.«term_/._» = Lean.ParserDescr.trailingNode `Rat.term_/._ 70 70 (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.symbol " /. ") (Lean.ParserDescr.cat `term 71))
Instances For
Implements "scientific notation" 123.4e-5
for rational numbers. (This definition is
@[irreducible]
because you don't want to unfold it. Use Rat.ofScientific_def
,
Rat.ofScientific_true_def
, or Rat.ofScientific_false_def
instead.)
Equations
- Rat.ofScientific m s e = if s = true then Rat.normalize (↑m) (10 ^ e) ⋯ else ↑(m * 10 ^ e)
Instances For
Equations
- Rat.instOfScientificRat = { ofScientific := Rat.ofScientific }
Equations
- Rat.instDecidableLtRatInstLTRat a b = inferInstanceAs (Decidable (Rat.blt a b = true))
Equations
- Rat.instDecidableLeRatInstLERat a b = inferInstanceAs (Decidable (Rat.blt b a = false))
Division of rational numbers. Note: div a 0 = 0
. Written with a separate function Rat.div
as a wrapper so that the definition is not unfolded at .instance
transparency.
Equations
- Rat.instDivRat = { div := Rat.div }