Documentation

Interval.Misc.Decimal

Utilities for decimal numbers #

Decimal basics #

structure Decimal :

`n * 10^s

  • n : ℤ

    Coefficient

  • s : ℤ

    Decimal integer exponent

Instances For
    def instDecidableEqDecimal.decEq (x✝ x✝¹ : Decimal) :
    Decidable (x✝ = x✝¹)
    Equations
    Instances For
      @[instance_reducible]
      Equations
      theorem Decimal.zero_def :
      0 = { n := 0, s := 0 }
      @[instance_reducible]

      The obvious instance

      Equations
      @[instance_reducible]
      Equations
      @[instance_reducible]
      Equations
      theorem Decimal.neg_eq (x : Decimal) :
      -x = { n := -x.n, s := x.s }

      Negation of a Decimal number negates the coefficient

      @[irreducible]

      Convert to ℝ

      Equations
      Instances For
        @[irreducible]

        Convert to ℚ

        Equations
        Instances For
          @[instance_reducible]

          Convert to ℝ

          Equations
          theorem Decimal.Rat.ofScientific_eq (n : ℕ) (neg : Bool) (e : ℕ) :
          OfScientific.ofScientific n neg e = ↑n * 10 ^ if neg = true then -↑e else ↑e
          theorem Decimal.Real.ofScientific_eq (n : ℕ) (neg : Bool) (e : ℕ) :
          OfScientific.ofScientific n neg e = ↑↑n * 10 ^ if neg = true then -↑e else ↑e
          theorem Decimal.val_eq (x : Decimal) :
          x.val = ↑x.n * 10 ^ x.s

          Decimal.val in simple form

          theorem Decimal.valq_eq (x : Decimal) :
          x.valq = ↑x.n * 10 ^ x.s

          Decimal.valq in simple form

          theorem Decimal.coe_valq (x : Decimal) :
          ↑x.valq = x.val

          val and valq are related

          @[simp]
          theorem Decimal.val_neg (x : Decimal) :
          (-x).val = -x.val

          Decimal.val commutes with negation for ℚ

          @[simp]
          theorem Decimal.val_zero :
          val 0 = 0

          Decimal.val 0 = 0

          @[instance_reducible]

          Print Decimal exactly in Decimal notation

          Equations
          • One or more equations did not get rendered due to their size.

          Exactly rounded precision reduction #

          def Decimal.prec (x : Decimal) (p : ℕ) (up : Bool) :

          Round to at most p significant digits

          Equations
          Instances For
            def Decimal.aprec (x : Decimal) (p : ℤ) (up : Bool) :

            Round to absolute precision 10 ^ p

            Equations
            Instances For
              theorem Decimal.prec_le (x : Decimal) (p : ℕ) :
              (x.prec p false).val ≤ x.val

              prec rounds down if desired

              theorem Decimal.le_prec (x : Decimal) (p : ℕ) :
              x.val ≤ (x.prec p true).val

              prec rounds up if desired

              theorem Decimal.aprec_le (x : Decimal) (p : ℤ) :

              aprec rounds down if desired

              theorem Decimal.le_aprec (x : Decimal) (p : ℤ) :
              x.val ≤ (x.aprec p true).val

              aprec rounds up if desired

              Exactly rounded conversion from binary exponents to decimal #

              We start with n * 2^s and want to convert it to m * 10^t, rounding as desired. Say we're rounding down, and want to achieve precision prec: m * 10^t ≤ n * 2^s ∧ m < 10^(prec+1) m = ⌊n * 2^s / 10^t⌋ ∧ m < 10^(prec+1) m = ⌊n * 2^s / 10^t⌋ ∧ n * 2^s / 10^t < 10^(prec+1) m = ⌊n * 2^s / 10^t⌋ ∧ t = ⌈logb 10 (n * 2^s / 10^(prec+1))⌉ We'd like to compute the above without ever blowing up, say by computing a very large n * 2^s value. We can estimate t using approximate arithmetic, then shift it up or down a bit to get the minimal value that works. A t value works if f t = round (n * 2^s / 10^t) satisfies f t < 10^(prec+1) Meh, doing it without blowup seems involved: it . Let's just eat the blowup for now.

              Equations
              Instances For
                Equations
                Instances For
                  def Decimal.OfBinary.t_underestimate (n : ℕ) (s : ℤ) (prec : ℕ) :

                  We underestimate t, since we'll then increase it until we fit within prec

                  Equations
                  Instances For
                    def Decimal.OfBinary.m_of_t (n : ℕ) (s t : ℤ) (up : Bool) :

                    n * 2^s / 10^t, rounding as desired

                    Equations
                    Instances For
                      theorem Decimal.OfBinary.m_of_t_le (n : ℕ) (s t : ℤ) :
                      ↑(m_of_t n s t false) ≤ ↑n * 2 ^ s / 10 ^ t

                      m_of_t rounds down if desired

                      theorem Decimal.OfBinary.le_m_of_t (n : ℕ) (s t : ℤ) :
                      ↑n * 2 ^ s / 10 ^ t ≤ ↑(m_of_t n s t true)

                      m_of_t rounds up if desired

                      theorem Decimal.OfBinary.antitone_m_of_t (n : ℕ) (s : ℤ) (up : Bool) :
                      Antitone fun (x : ℤ) => m_of_t n s x up

                      m_of_t n s t is antitone in t

                      theorem Decimal.OfBinary.exists_t (n : ℕ) (s t : ℤ) (up : Bool) :
                      ∃ (dt : ℕ), m_of_t n s (t + ↑dt) up ≤ 1

                      A sufficiently large t means m ≤ 1

                      theorem Decimal.OfBinary.exists_t' (n : ℕ) (s t : ℤ) (prec : ℕ) (up : Bool) :
                      ∃ (dt : ℕ), m_of_t n s (t + ↑dt) up ≤ 10 ^ (prec + 1)

                      A sufficiently large t means m ≤ 10 ^ (prec + 1)

                      def Decimal.OfBinary.ofBinaryNat (n : ℕ) (s : ℤ) (prec : ℕ) (up : Bool) :

                      Convert binary to Decimal, rounding to a desired decimal precision

                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For
                        theorem Decimal.OfBinary.ofBinaryNat_le (n : ℕ) (s : ℤ) (prec : ℕ) :
                        (ofBinaryNat n s prec false).val ≤ ↑n * 2 ^ s

                        ofBinaryNat rounds down if desired

                        theorem Decimal.OfBinary.le_ofBinaryNat (n : ℕ) (s : ℤ) (prec : ℕ) :
                        ↑n * 2 ^ s ≤ (ofBinaryNat n s prec true).val

                        ofBinaryNat rounds up if desired

                        def Decimal.ofBinary (n s : ℤ) (prec : ℕ) (up : Bool) :

                        Convert binary to Decimal, rounding to a desired decimal precision

                        Equations
                        • One or more equations did not get rendered due to their size.
                        Instances For
                          theorem Decimal.ofBinary_le (n s : ℤ) (prec : ℕ) :
                          (ofBinary n s prec false).val ≤ ↑n * 2 ^ s

                          ofBinary rounds down if desired

                          theorem Decimal.le_ofBinary (n s : ℤ) (prec : ℕ) :
                          ↑n * 2 ^ s ≤ (ofBinary n s prec true).val

                          ofBinary rounds up if desired

                          Comparison #

                          For now we very lazily convert to ℚ, even though this can be very inefficient.

                          def Decimal.ble (x y : Decimal) :

                          Comparison via (possibly very inefficient) conversion to ℚ

                          Equations
                          Instances For
                            @[instance_reducible]
                            Equations
                            @[instance_reducible]
                            Equations
                            @[instance_reducible]
                            Equations
                            @[instance_reducible]
                            Equations
                            theorem Decimal.le_def (x y : Decimal) :
                            x ≤ y ↔ x.ble y = true
                            theorem Decimal.lt_def (x y : Decimal) :
                            x < y ↔ (!y.ble x) = true
                            theorem Decimal.le_iff (x y : Decimal) :
                            x ≤ y ↔ x.val ≤ y.val
                            theorem Decimal.lt_iff (x y : Decimal) :
                            x < y ↔ x.val < y.val
                            @[instance_reducible]
                            Equations
                            @[instance_reducible]
                            Equations
                            theorem Decimal.min_def (x y : Decimal) :
                            x ⊓ y = if x ≤ y then x else y
                            theorem Decimal.max_def (x y : Decimal) :
                            x ⊔ y = if x ≤ y then y else x
                            @[simp]
                            theorem Decimal.val_min (x y : Decimal) :
                            (x ⊓ y).val = min x.val y.val
                            @[simp]
                            theorem Decimal.val_max (x y : Decimal) :
                            (x ⊔ y).val = max x.val y.val

                            Exact arithmetic #

                            Exact addition of Decimal

                            Equations
                            Instances For

                              Exact division by 2 for Decimal

                              Equations
                              Instances For
                                @[instance_reducible]
                                Equations
                                @[instance_reducible]
                                Equations
                                theorem Decimal.add_def (x y : Decimal) :
                                x + y = x.add y
                                theorem Decimal.sub_def (x y : Decimal) :
                                x - y = x + -y
                                @[simp]
                                theorem Decimal.val_add {x y : Decimal} :
                                (x + y).val = x.val + y.val

                                Decimal.add is exact

                                @[simp]
                                theorem Decimal.val_sub {x y : Decimal} :
                                (x - y).val = x.val - y.val

                                Decimal.sub is exact

                                @[simp]
                                theorem Decimal.val_div2 {x : Decimal} :
                                x.div2.val = x.val / 2

                                Decimal.div2 is exact

                                Decimal intervals and balls #

                                A nonempty interval of Decimal

                                Instances For
                                  structure Decimal.Ball :

                                  A nonempty ball of Decimal

                                  Instances For
                                    @[instance_reducible]
                                    Equations
                                    @[instance_reducible]
                                    Equations
                                    @[instance_reducible]
                                    Equations
                                    @[instance_reducible]
                                    Equations
                                    Equations
                                    Instances For
                                      @[instance_reducible]
                                      Equations
                                      • One or more equations did not get rendered due to their size.

                                      Turn an interval into a ball, exactly

                                      Equations
                                      Instances For
                                        @[simp]

                                        .ball commutes with approx

                                        def Decimal.Ball.prec (x : Ball) (p : ℕ) :

                                        Precision reduce an Ball, growing it slightly if necessary

                                        Equations
                                        • One or more equations did not get rendered due to their size.
                                        Instances For
                                          theorem Decimal.Ball.approx_prec (x : Ball) (p : ℕ) (y : ℝ) (m : approx x y) :
                                          approx (x.prec p) y

                                          Ball.prec is conservative