Documentation

Interval.Floating.Conversion

Conversion from ℕ, ℤ, ℚ, Float to Floating #

Normalization given n ∈ [2^62, 2^63] #

A normalized n, s pair ready for conversion to Floating

Instances For
    noncomputable def Floating.Convert.val (x : Convert) :
    Equations
    Instances For
      theorem Floating.Convert.n_mod (x : Convert) :
      x.n % 2 ^ 64 = x.n
      @[irreducible]

      Build a Floating out of n * 2^(s - 2^63), rounding if required

      Equations
      Instances For
        theorem Floating.valid_convert_tweak :
        2 ^ 62 ∈ Set.Ico (2 ^ 62) (2 ^ 63)
        @[irreducible]
        def Floating.convert_tweak (n : ℕ) (s : ℤ) (norm : n ∈ Set.Icc (2 ^ 62) (2 ^ 63)) :

        Build a Floating out of n * 2^(s - 2^63), rounding if required

        Equations
        Instances For
          theorem Floating.val_convert_tweak (n : ℕ) (s : ℤ) (norm : n ∈ Set.Icc (2 ^ 62) (2 ^ 63)) :
          (convert_tweak n s norm).val = ↑n * 2 ^ (s - 2 ^ 63)

          convert_tweak is correct

          theorem Floating.approx_convert {a : ℝ} {n : ℕ} {s : ℤ} {norm : n ∈ Set.Icc (2 ^ 62) (2 ^ 63)} {up : Bool} (an : if up = true then a ≤ ↑n * 2 ^ (s - 2 ^ 63) else ↑n * 2 ^ (s - 2 ^ 63) ≤ a) :
          Rounds ((convert_tweak n s norm).finish up) a up

          Prove a (convert_tweak _ _ _).finish _ call is correct, in context.

          Conversion from ℕ #

          theorem Floating.ofNat_norm {n : ℕ} {up : Bool} :
          have t := n.log2; have s := t - 62; ¬t ≤ 62 → n.shiftRightRound s up ∈ Set.Icc (2 ^ 62) (2 ^ 63)
          @[irreducible]
          def Floating.ofNat (n : ℕ) (up : Bool) :

          Conversion from ℕ to Floating, rounding up or down

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

            Conversion from ℕ literals to Floating, rounding down arbitrarily. Use Interval.ofNat if you want something trustworthy (it rounds both ways).

            Equations
            theorem Floating.val_ofNat' {n : ℕ} (lt : n < 2 ^ 63 := by norm_num) {up : Bool} :
            (ofNat n up).val = ↑n

            Small naturals convert exactly

            theorem Floating.val_ofNat {n : ℕ} [n.AtLeastTwo] (lt : n < 2 ^ 63 := by norm_num) :
            val (OfNat.ofNat n) = ↑n

            Small naturals convert exactly

            theorem Floating.ofNat_ne_nan {n : ℕ} (lt : n < 2 ^ 63) (up : Bool) :

            Small naturals do not overflow. Much larger values also do not overflow, but this is all we need at the moment.

            theorem Floating.approx_ofNat (n : ℕ) (up : Bool) :
            Rounds (ofNat n up) (↑n) up

            ofNat rounds the desired way

            theorem Floating.ofNat_le {n : ℕ} (h : ofNat n false ≠ nan) :
            (ofNat n false).val ≤ ↑n

            approx_ofNat, down version

            theorem Floating.le_ofNat {n : ℕ} (h : ofNat n true ≠ nan) :
            ↑n ≤ (ofNat n true).val

            approx_ofNat, up version

            Combined version, for use in Interval construction

            Conversion from ℤ #

            @[irreducible]
            def Floating.ofInt (n : ℤ) (up : Bool) :

            Conversion from ℤ

            Equations
            Instances For
              theorem Floating.approx_ofInt (n : ℤ) (up : Bool) :
              Rounds (ofInt n up) (↑n) up

              ofInt rounds the desired way

              theorem Floating.ofInt_le {n : ℤ} (h : ofInt n false ≠ nan) :
              (ofInt n false).val ≤ ↑n

              approx_ofInt, down version

              theorem Floating.le_ofInt {n : ℤ} (h : ofInt n true ≠ nan) :
              ↑n ≤ (ofInt n true).val

              approx_ofInt, up version

              Combined version, for use in Interval construction

              Conversion from ℚ #

              theorem Floating.ofRat_norm {x : ℚ} {up : Bool} (x0 : ¬x = 0) :
              have r := x.log2; have n := x.num.natAbs; have p := if r ≤ 62 then (n <<< (62 - r).toNat, x.den) else (n, x.den <<< (r - 62).toNat); p.1.rdiv p.2 up ∈ Set.Icc (2 ^ 62) (2 ^ 63)

              ofRat_abs gives the right input to convert_tweak

              @[irreducible, inline]

              Conversion from ℚ, taking absolute values and rounding up or down

              Equations
              • One or more equations did not get rendered due to their size.
              Instances For
                @[irreducible]
                def Floating.ofRat (x : ℚ) (up : Bool) :

                Conversion from ℚ, rounding up or down

                Equations
                Instances For
                  theorem Floating.approx_ofRat_abs (x : ℚ) (up : Bool) :
                  Rounds (ofRat_abs x up) (↑|x|) up

                  ofRat_abs rounds the desired way

                  theorem Floating.approx_ofRat (x : ℚ) (up : Bool) :
                  Rounds (ofRat x up) (↑x) up

                  ofRat rounds the desired way

                  theorem Floating.ofRat_le {x : ℚ} (h : ofRat x false ≠ nan) :
                  (ofRat x false).val ≤ ↑x

                  approx_ofRat, down version

                  theorem Floating.le_ofRat {x : ℚ} (h : ofRat x true ≠ nan) :
                  ↑x ≤ (ofRat x true).val

                  approx_ofRat, up version

                  Combined version, for use in Interval construction

                  Conversion from Float #

                  @[irreducible]

                  Convert a Float to Floating. This could be fast, but we don't need it to be.

                  Equations
                  Instances For

                    ofFloat rounding is self-consistent