Documentation

LeanPool.Flean.IntRounding

Integer Rounding Functions #

This module collects the integer-valued rounding primitives (round0, roundinf, roundup, rounddown, roundnearest) used to round a rational to a mantissa, together with their basic correctness properties such as round-to-even behaviour on half-integers.

@[reducible, inline]

An integer-valued rounding rule taking a sign bit and a rational mantissa.

Equations
Instances For
    def round0 (s : Bool) (q : ℚ) :

    Round toward zero: the rounded mantissa is ⌊q⌋, ignoring the sign.

    Equations
    Instances For
      @[simp]
      theorem round0_apply (s : Bool) (q : ℚ) :

      round0 ignores its sign argument.

      def roundinf (s : Bool) (q : ℚ) :

      Round away from zero: the rounded mantissa is ⌈q⌉, ignoring the sign.

      Equations
      Instances For
        @[simp]
        theorem roundinf_apply (s : Bool) (q : ℚ) :

        roundinf ignores its sign argument.

        def roundup (s : Bool) (q : ℚ) :

        Round down (toward negative infinity), branching on the sign.

        Equations
        Instances For
          def rounddown (s : Bool) (q : ℚ) :

          Round up (toward positive infinity), branching on the sign.

          Equations
          Instances For
            def roundNearInt (q : ℚ) :

            Round to the nearest integer, with ties broken to the even integer.

            Equations
            Instances For
              def roundnearest (s : Bool) (q : ℚ) :

              Round to nearest as an IntRounder: ties to even, ignoring the sign.

              Equations
              Instances For
                @[simp]

                roundnearest ignores its sign argument.

                theorem round_near_int_of_pos {q : ℚ} (h : 0 < q) :
                theorem round_near_le_round_near {q1 q2 : ℚ} (h : q1 ≤ q2) :
                theorem round_near_eq_of (q : ℚ) (z : ℤ) :
                ↑z - 1 / 2 < q ∧ q < ↑z + 1 / 2 → z = roundNearInt q
                theorem fract_eq_ceil_of_pos {q : ℚ} (h : 0 < Int.fract q) :
                Int.fract q = q + 1 - ↑⌈q⌉
                theorem round_near_int_le (q : ℚ) :
                |↑(roundNearInt q) - q| ≤ 1 / 2
                theorem round_near_add_half (z : ℤ) (h : z % 2 = 0) :
                roundNearInt (↑z + 1 / 2) = z
                theorem round_near_sub_half (z : ℤ) (h : z % 2 = 0) :
                roundNearInt (↑z - 1 / 2) = z
                theorem round_of_add_half (z : ℤ) :
                roundNearInt (↑z + 1 / 2) % 2 = 0

                The set of rationals whose floor is z, namely [z, z + 1).

                Equations
                Instances For
                  def ceilInterval (z : ℤ) :

                  The set of rationals whose ceiling is z, namely (z - 1, z].

                  Equations
                  Instances For

                    The set of rationals that round to nearest z: a closed interval when z is even (ties round to it) and an open interval otherwise.

                    Equations
                    Instances For
                      theorem Icc_of_Ioo {α : Type} [PartialOrder α] {q x y : α} (h : q ∈ Set.Icc x y) (not_x : q ≠ x) (not_y : q ≠ y) :
                      q ∈ Set.Ioo x y

                      A positivity extension proving 0 ≤ roundNearInt q from 0 ≤ q.

                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For
                        class ValidRounder (f : Bool → ℚ → ℕ) :

                        A rounding rule is valid if it is monotone on nonnegative inputs and is a left inverse of the cast ℕ → ℚ (so it fixes integer mantissas).

                        • le_iff_le (s : Bool) (q1 q2 : ℚ) : 0 ≤ q1 → q1 ≤ q2 → f s q1 ≤ f s q2

                          The rounder is monotone in the mantissa on nonnegative inputs.

                        • leftInverse (s : Bool) : Function.LeftInverse (f s) Nat.cast

                          The rounder fixes integer mantissas: it left-inverts the cast ℕ → ℚ.

                        Instances

                          The rounder obtained by flipping the sign bit, used to relate rounding of q and -q.

                          Equations
                          Instances For
                            theorem neg_neg_r (r : IntRounder) :
                            r.neg.neg = r
                            theorem round_eq_or (r : IntRounder) [rh : ValidRounder r] (b : Bool) {q : ℚ} (h : 0 ≤ q) :
                            r b q = round0 b q ∨ r b q = roundinf b q
                            theorem round_eq_or' (r : IntRounder) [rh : ValidRounder r] (b : Bool) {q : ℚ} (h : 0 ≤ q) :
                            r b q = rounddown b q ∨ r b q = roundup b q
                            theorem roundr_le (r : IntRounder) [rh : ValidRounder r] {b : Bool} {n : ℕ} {q : ℚ} (h : 0 ≤ q) (h' : q ≤ ↑n) :
                            r b q ≤ n
                            theorem le_roundr (r : IntRounder) [rh : ValidRounder r] {b : Bool} {n : ℕ} {q : ℚ} (h' : ↑n ≤ q) :
                            n ≤ r b q