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 / 2z = 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 q1q1 q2f 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