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.
The denominator is nonzero.
The numerator and denominator are coprime: it is in "reduced form".
- )
Instances For
- NNRat.canLift
- Rat.addCommGroup
- Rat.addCommMonoid
- Rat.addCommSemigroup
- Rat.addGroup
- Rat.addLeftCancelSemigroup
- Rat.addMonoid
- Rat.addRightCancelSemigroup
- Rat.addSemigroup
- Rat.canLift
- Rat.commGroupWithZero
- Rat.commMonoid
- Rat.commRing
- Rat.commSemigroup
- Rat.commSemiring
- Rat.instAdd
- Rat.instAddLeftMono
- Rat.instCharZero
- Rat.instDistribLattice
- Rat.instDiv
- Rat.instDivisionRing
- Rat.instField
- Rat.instInf
- Rat.instIntCast
- Rat.instInv_mathlib
- Rat.instIsOrderedAddMonoid
- Rat.instIsStrictOrderedRing
- Rat.instLE
- Rat.instLT
- Rat.instLattice
- Rat.instMul
- Rat.instNNRatCast
- Rat.instNatCast
- Rat.instNeg
- Rat.instOfNat
- Rat.instOfScientific
- Rat.instPartialOrder
- Rat.instPosMulMono
- Rat.instPowNat
- Rat.instPreorder
- Rat.instSemilatticeInf
- Rat.instSemilatticeSup
- Rat.instSub
- Rat.instSup
- Rat.instZeroLEOneClass
- Rat.isDomain
- Rat.linearOrder
- Rat.monoid
- Rat.nontrivial
- Rat.semigroup
- Rat.semiring
- Rat.smulDivisionRing
- instCoeHTCTRatOfRatCast
- instCoeTailRatOfRatCast
- instInhabitedRat
- instRatCastRat
- instReprRat
- instToStringRat
Equations
- instInhabitedRat = { default := { num := 0, den_nz := instInhabitedRat._proof_1, reduced := instInhabitedRat._proof_2 } }
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.
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 (num.natAbs.gcd den) ⋯ ⋯ ⋯ ⋯
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_nz := instInhabitedRat._proof_1, reduced := instInhabitedRat._proof_2 } else Rat.normalize num den den_nz
Form the quotient n / d
where n d : Int
.
Equations
- Rat.divInt x✝ (Int.ofNat d) = inline (mkRat x✝ d)
- Rat.divInt x✝ (Int.negSucc d) = Rat.normalize (-x✝) d.succ ⋯
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))
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.instOfScientific = { ofScientific := fun (m : Nat) (s : Bool) (e : Nat) => Rat.ofScientific (OfNat.ofNat m) s (OfNat.ofNat e) }
Equations
- a.instDecidableLt b = inferInstanceAs (Decidable (a.blt b = true))
Equations
- a.instDecidableLe b = inferInstanceAs (Decidable (b.blt a = false))