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
      Equations
      theorem Decimal.zero_def :
      0 = { n := 0, s := 0 }

      The obvious instance

      Equations
      Equations
      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

          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

          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
                            Equations
                            Equations
                            Equations
                            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
                            Equations
                            • One or more equations did not get rendered due to their size.
                            Equations
                            theorem Decimal.min_def (x y : Decimal) :
                            xy = if x y then x else y
                            theorem Decimal.max_def (x y : Decimal) :
                            xy = if x y then y else x
                            @[simp]
                            theorem Decimal.val_min (x y : Decimal) :
                            (xy).val = min x.val y.val
                            @[simp]
                            theorem Decimal.val_max (x y : Decimal) :
                            (xy).val = max x.val y.val

                            Exact arithmetic #

                            Exact addition of Decimal

                            Equations
                            Instances For

                              Exact division by 2 for Decimal

                              Equations
                              Instances For
                                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
                                    Equations
                                    Equations
                                    Instances For
                                      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