IntegerEndpointRounding

Lean checked

Statement

This explanation was written alongside the formalization to describe the statement and its reasoning.

Lemma

Let q and w be signed and unsigned 64-bit integers, respectively, and let fp be a biased pair. Suppose one of the following holds:
  • w=0 and fp=(0,0);
  • w>0, q<-342, and fp=(0,0);
  • w>0, q>308, and fp=(0,2047).
Then some binary64 carrier word is the unrestricted assembly of fp and is the nonnegative nearest-even rounding of the exact real number w10^q.

Lean statement

Trellis authored these statements and proofs. Lean checks the formal proofs; the assumptions used by each proof are listed below.

import Tablet.DecimalGoalVocabularyDefault
import Tablet.F64
import Tablet.num_imp_dec2flt_common_BiasedFp

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: IntegerEndpointRounding]
theorem IntegerEndpointRounding :
    ∀ (q : Std.I64) (w : Std.U64)
        (fp : num.imp.dec2flt.common.BiasedFp),
      ((w.val = 0 ∧
          fp = { m := 0#u64, p_biased := 0#i32 }) ∨
        (0 < w.val ∧ q.val < -342 ∧
          fp = { m := 0#u64, p_biased := 0#i32 }) ∨
        (0 < w.val ∧ 308 < q.val ∧
          fp = { m := 0#u64, p_biased := 2047#i32 })) →
      ∃ target : F64,
        DecimalGoalVocabularyDefault.assembledWord fp target ∧
        DecimalGoalVocabularyDefault.roundsMagnitude
          ((w.val : Real) * (10 : Real) ^ q.val) target := by
Proof

Natural-language proof

The status above describes the Lean proof. Its explanatory prose may remain a sketch after that proof closes.

Explanatory prose status

Prose retained as authored. The explanatory prose is preserved as it stood when captured.

Proof

Unfold DecimalGoalVocabularyDefault. Put U=2^{52}. The pair (0,0) assembles to raw word zero, and (0,2047) assembles to 2047U, the positive-infinity word. Both are members of the complete word carrier F64.
If w=0, the exact value is zero. The finite value of word zero is zero, so its distance is zero and no distinct finite word is closer. Any equality of distances with a distinct word is impossible; word zero also has even low bit. It is below the infinity word, and zero is below the overflow threshold. Thus all conjuncts of roundsMagnitude hold.
Suppose w>0 and q<-342. Since w\le2^{64}-1 and q\le-343,
0<w10^q\le(2^{64}-1)10^{-343}<2^{-1075}. \tag{1}
The final strict inequality follows after multiplying by the positive 10^{343}2^{1075} from the integer comparison (2^{64}-1)2^{1075}<10^{343}. Adjacent subnormal words have spacing 2^{-1074}, so (1) places the input strictly below the midpoint between zero and the least positive subnormal. Word zero is therefore strictly closer than every positive finite word; the quantified competitor and tie clauses in roundsMagnitude follow. The input is also below the overflow threshold. Hence the assembled zero word is the required target.
Finally suppose w>0 and q>308. Then
w10^q\ge10^{309}>2^{1024}>2^{1024}-2^{970}.
The last expression is exactly the overflow threshold in DecimalGoalVocabularyDefault. Consequently its infinity branch requires, and is satisfied by, word 2047U. Choosing the corresponding F64 carrier establishes both assembly and rounding in all three cases.

Lean proof


  set_option maxRecDepth 1000000 in
   intro q w fp h
  rcases h with hzero | hsmall | hover
  · rcases hzero with ⟨hw, hfp⟩
    subst fp
    refine ⟨⟨BitVec.ofNat 64 0⟩, ?_, ?_⟩
    · change (⟨BitVec.ofNat 64 0⟩ : F64).val =
        Nat.lor (0#u64).val (((0#i32).val.emod (2 ^ 64)).toNat * (2 ^ 52)) % (2 ^ 64)
      rfl
    · dsimp [DecimalGoalVocabularyDefault]
      have target_val : (⟨BitVec.ofNat 64 0⟩ : F64).val = 0 := by rfl
      rw [target_val, hw]
      norm_num
      exact pow_lt_pow_right₀ (by norm_num) (by omega)
  · rcases hsmall with ⟨hw, hq, hfp⟩
    subst fp
    refine ⟨⟨BitVec.ofNat 64 0⟩, ?_, ?_⟩
    · change (⟨BitVec.ofNat 64 0⟩ : F64).val =
        Nat.lor (0#u64).val (((0#i32).val.emod (2 ^ 64)).toNat * (2 ^ 52)) % (2 ^ 64)
      rfl
    · dsimp [DecimalGoalVocabularyDefault]
      have target_val : (⟨BitVec.ofNat 64 0⟩ : F64).val = 0 := by rfl
      rw [target_val]
      let value : Nat → Real := fun n =>
        if n < 2 ^ 52 then ((n % 2 ^ 52 : Nat) : Real) / 2 ^ 1074
        else if 1075 ≤ n / 2 ^ 52 then
          ((2 ^ 52 + n % 2 ^ 52 : Nat) : Real) * 2 ^ (n / 2 ^ 52 - 1075)
        else ((2 ^ 52 + n % 2 ^ 52 : Nat) : Real) / 2 ^ (1075 - n / 2 ^ 52)
      have floor_value : ∀ n : Nat, 0 < n →
          (2 : Real) ^ (-1074 : Int) ≤ value n := by
        intro n hn
        dsimp [value]
        by_cases hsub : n < 2 ^ 52
        · have hsub' : n < 4503599627370496 := by norm_num at hsub ⊢; exact hsub
          simp [hsub', Nat.mod_eq_of_lt hsub]
          have hprod : (2 ^ 1074 : Real)⁻¹ * 2 ^ 1074 = 1 := by
            exact inv_mul_cancel₀ (by positivity)
          rw [le_div_iff₀ (by positivity : (0 : Real) < 2 ^ 1074)]
          calc
            (2 ^ 1074 : Real)⁻¹ * 2 ^ 1074 = 1 := hprod
            _ ≤ (n % 4503599627370496 : Nat) := by
              have hmod : n % 4503599627370496 = n := by
                apply Nat.mod_eq_of_lt
                norm_num at hsub ⊢
                exact hsub
              rw [hmod]
              exact_mod_cast (Nat.succ_le_iff.mp hn)
        · have hsub' : ¬ n < 4503599627370496 := by
            intro h
            apply hsub
            norm_num at h ⊢
            exact h
          simp only [if_neg hsub']
          by_cases hnorm : 1075 ≤ n / 2 ^ 52
          · have hnorm' : 1075 ≤ n / 4503599627370496 := by
              norm_num at hnorm ⊢
              exact hnorm
            rw [if_pos hnorm']
            have hpall : ∀ k : Nat, (1 : Real) ≤ 2 ^ k := by
              intro k
              induction k with
              | zero => norm_num
              | succ k ih => rw [pow_succ]; nlinarith
            have hp : (1 : Real) ≤ 2 ^ (n / 2 ^ 52 - 1075 : Nat) := hpall _
            have hm : (2 : Real) ^ (-1074 : Int) ≤ 2 ^ 52 := by
              have hm' : (2 : Real) ^ (-1074 : Int) ≤ (2 : Real) ^ (52 : Int) :=
                zpow_le_zpow_right₀ (by norm_num) (by norm_num)
              simpa using hm'
            have hnume : (2 : Real) ^ 52 ≤ ((2 ^ 52 + n % 2 ^ 52 : Nat) : Real) := by
              exact_mod_cast Nat.le_add_right _ _
            exact le_trans hm (le_trans hnume (calc
              ((2 ^ 52 + n % 2 ^ 52 : Nat) : Real) =
                  ((2 ^ 52 + n % 2 ^ 52 : Nat) : Real) * 1 := by ring
              _ ≤ ((2 ^ 52 + n % 2 ^ 52 : Nat) : Real) *
                  2 ^ (n / 2 ^ 52 - 1075 : Nat) := by
                have hc : (0 : Real) ≤ ((2 ^ 52 + n % 2 ^ 52 : Nat) : Real) := by positivity
                exact mul_le_mul_of_nonneg_left hp hc))
          · have hnorm' : ¬ 1075 ≤ n / 4503599627370496 := by
              intro h
              apply hnorm
              norm_num at h ⊢
              exact h
            rw [if_neg hnorm']
            have he : n / 2 ^ 52 < 1075 := by omega
            have hden : 2 ^ (1075 - n / 2 ^ 52 : Nat) ≤ (2 : Real) ^ 1074 := by
              apply pow_le_pow_right₀ (by norm_num)
              omega
            have hnume : (2 : Real) ^ 52 ≤ ((2 ^ 52 + n % 2 ^ 52 : Nat) : Real) := by
              exact_mod_cast Nat.le_add_right _ _
            have hquot : (2 : Real) ^ 52 / 2 ^ 1074 ≤
                ((2 ^ 52 + n % 2 ^ 52 : Nat) : Real) /
                  2 ^ (1075 - n / 2 ^ 52 : Nat) := by
              gcongr
            have hbase : (2 : Real) ^ (-1074 : Int) ≤ 2 ^ 52 / 2 ^ 1074 := by
              set_option exponentiation.threshold 2048 in
                norm_num [zpow_neg, div_eq_mul_inv]
            exact hbase.trans hquot
      have qbound : q.val ≤ -343 := by omega
      have wbound : w.val ≤ 2 ^ 64 - 1 := by
        have h := w.hBounds
        norm_num at h ⊢
        omega
      have x_nonnegative : 0 ≤ (w.val : Real) * (10 : Real) ^ q.val := by
        exact mul_nonneg (by positivity) (zpow_pos (by norm_num) _).le
      have x_small : (w.val : Real) * (10 : Real) ^ q.val <
          (2 : Real) ^ (-1075 : Int) := by
        have hpow : (10 : Real) ^ q.val ≤ (10 : Real) ^ (-343 : Int) := by
          exact zpow_le_zpow_right₀ (by norm_num) qbound
        have hmul : (w.val : Real) * (10 : Real) ^ q.val ≤
            ((2 ^ 64 - 1 : Nat) : Real) * (10 : Real) ^ (-343 : Int) := by
          have hmax : (0 : Real) ≤ (2 ^ 64 - 1 : Nat) := by positivity
          exact mul_le_mul (by exact_mod_cast wbound) hpow
            (zpow_pos (by norm_num) _).le hmax
        have hnum : ((2 ^ 64 - 1 : Nat) : Real) * (10 : Real) ^ (-343 : Int) <
            (2 : Real) ^ (-1075 : Int) := by
          rw [zpow_neg, zpow_neg, ← div_eq_mul_inv, ← one_div]
          apply (div_lt_div_iff₀ (by norm_num : (0 : Real) < 10 ^ 343)
            (by norm_num : (0 : Real) < 2 ^ 1075)).2
          have hn : (2 ^ 64 - 1 : Nat) * 2 ^ 1075 < 10 ^ 343 := by
            set_option maxRecDepth 1000000 in
              norm_num [pow_succ]
          exact_mod_cast hn
        exact hmul.trans_lt hnum
      norm_num
      refine ⟨x_nonnegative, ?_, ?_⟩
      · have hth : (2 : Real) ^ (-1075 : Int) <
            (2 : Real) ^ 1024 - 2 ^ 970 := by
          have hp : (0 : Real) < 2 ^ 970 := by positivity
          have hpow : (2 : Real) ^ 970 < 2 ^ 1024 :=
            pow_lt_pow_right₀ (by norm_num) (by omega)
          have hone : (1 : Real) < 2 ^ 1024 - 2 ^ 970 := by
            set_option exponentiation.threshold 2048 in
              have hone : (1 : Real) ≤ 2 ^ 970 := by
                exact_mod_cast (show (1 : Nat) ≤ 2 ^ 970 by omega)
              nlinarith [hpow, hone]
          have hsmallone : (2 : Real) ^ (-1075 : Int) < 1 := by
            rw [zpow_neg]
            exact inv_lt_one_of_one_lt₀ (by norm_num)
          exact lt_trans hsmallone hone
        exact x_small.trans hth
      · intro v hv hne
        have vpos : 0 < v.val := by
          by_contra hn
          have hz : v.val = 0 := by omega
          apply hne
          simpa [target_val] using hz
        have hfloor := floor_value v.val vpos
        have htwo : 2 * ((w.val : Real) * (10 : Real) ^ q.val) ≤ value v.val := by
          have hhalf : (w.val : Real) * (10 : Real) ^ q.val ≤
              (2 : Real) ^ (-1075 : Int) := x_small.le
          have hpow2 : 2 * (2 : Real) ^ (-1075 : Int) = (2 : Real) ^ (-1074 : Int) := by
            calc
              2 * (2 : Real) ^ (-1075 : Int) =
                  (2 : Real) ^ (1 : Int) * 2 ^ (-1075 : Int) := by norm_num
              _ = (2 : Real) ^ (-1074 : Int) := by
                rw [← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
                norm_num
          calc
            2 * ((w.val : Real) * (10 : Real) ^ q.val) ≤
                2 * (2 : Real) ^ (-1075 : Int) :=
              mul_le_mul_of_nonneg_left hhalf (by norm_num)
            _ = (2 : Real) ^ (-1074 : Int) := hpow2
            _ ≤ value v.val := hfloor
        have hfloor' : (2 : Real) ^ (-1074 : Int) ≤
            (if v.val < 2 ^ 52 then ((v.val % 2 ^ 52 : Nat) : Real) / 2 ^ 1074
             else if 1075 ≤ v.val / 2 ^ 52 then
               ((2 ^ 52 + v.val % 2 ^ 52 : Nat) : Real) *
                 2 ^ (v.val / 2 ^ 52 - 1075)
             else ((2 ^ 52 + v.val % 2 ^ 52 : Nat) : Real) /
               2 ^ (1075 - v.val / 2 ^ 52)) := by
          simpa only [value] using hfloor
        have htwo' : 2 * ((w.val : Real) * (10 : Real) ^ q.val) ≤
            (if v.val < 2 ^ 52 then ((v.val % 2 ^ 52 : Nat) : Real) / 2 ^ 1074
             else if 1075 ≤ v.val / 2 ^ 52 then
               ((2 ^ 52 + v.val % 2 ^ 52 : Nat) : Real) *
                 2 ^ (v.val / 2 ^ 52 - 1075)
             else ((2 ^ 52 + v.val % 2 ^ 52 : Nat) : Real) /
               2 ^ (1075 - v.val / 2 ^ 52)) := by
          simpa only [value] using htwo
        have habsx : |(w.val : Real) * (10 : Real) ^ q.val| =
            (w.val : Real) * |(10 : Real) ^ q.val| := by
          rw [abs_mul, abs_of_nonneg (by positivity)]
        rw [← habsx]
        have hupper : (w.val : Real) * (10 : Real) ^ q.val ≤
            (if v.val < 4503599627370496 then ((v.val % 4503599627370496 : Nat) : Real) / 2 ^ 1074
             else if 1075 ≤ v.val / 4503599627370496 then
               ((4503599627370496 + v.val % 4503599627370496 : Nat) : Real) *
                 2 ^ (v.val / 4503599627370496 - 1075)
             else ((4503599627370496 + v.val % 4503599627370496 : Nat) : Real) /
               2 ^ (1075 - v.val / 4503599627370496)) := by
          norm_num at htwo' ⊢
          linarith [htwo']
        have hnon : (w.val : Real) * (10 : Real) ^ q.val -
            (if v.val < 4503599627370496 then ((v.val % 4503599627370496 : Nat) : Real) / 2 ^ 1074
             else if 1075 ≤ v.val / 4503599627370496 then
               ((4503599627370496 + v.val % 4503599627370496 : Nat) : Real) *
                 2 ^ (v.val / 4503599627370496 - 1075)
             else ((4503599627370496 + v.val % 4503599627370496 : Nat) : Real) /
               2 ^ (1075 - v.val / 4503599627370496)) ≤ 0 := by
          norm_num at hupper ⊢
          linarith
        have hnon' : (w.val : Real) * (10 : Real) ^ q.val -
            (if (↑v : Nat) < 4503599627370496 then
                (↑((↑v : Nat) % 4503599627370496) : Real) / 2 ^ 1074
             else if 1075 ≤ (↑v : Nat) / 4503599627370496 then
               (↑(4503599627370496 + (↑v : Nat) % 4503599627370496) : Real) *
                 2 ^ ((↑v : Nat) / 4503599627370496 - 1075)
             else (↑(4503599627370496 + (↑v : Nat) % 4503599627370496) : Real) /
               2 ^ (1075 - (↑v : Nat) / 4503599627370496)) ≤ 0 := by
          simpa only using hnon
        have hnon_goal : (w.val : Real) * (10 : Real) ^ q.val -
            (if (↑v : Nat) < 4503599627370496 then
                (↑((↑v : Nat) % 4503599627370496) : Real) / 2 ^ 1074
             else if 1075 ≤ (↑v : Nat) / 4503599627370496 then
               (↑(4503599627370496 + (↑v : Nat) % 4503599627370496) : Real) *
                 2 ^ ((↑v : Nat) / 4503599627370496 - 1075)
             else (↑(4503599627370496 + (↑v : Nat) % 4503599627370496) : Real) /
               2 ^ (1075 - (↑v : Nat) / 4503599627370496)) ≤ 0 := by
          simpa only using hnon'
        norm_num at hnon_goal
        change |(w.val : Real) * (10 : Real) ^ q.val| ≤
          |(w.val : Real) * (10 : Real) ^ q.val -
            (if (↑v : Nat) < 4503599627370496 then
                (↑((↑v : Nat) % 4503599627370496) : Real) / 2 ^ 1074
             else if 1075 ≤ (↑v : Nat) / 4503599627370496 then
               (4503599627370496 + ((↑v : Nat) % 4503599627370496 : Nat) : Real) *
                 2 ^ ((↑v : Nat) / 4503599627370496 - 1075)
             else (4503599627370496 + ((↑v : Nat) % 4503599627370496 : Nat) : Real) /
               2 ^ (1075 - (↑v : Nat) / 4503599627370496))|
        have hfinal : (w.val : Real) * (10 : Real) ^ q.val -
            (if (↑v : Nat) < 4503599627370496 then
                (↑((↑v : Nat) % 4503599627370496) : Real) / 2 ^ 1074
             else if 1075 ≤ (↑v : Nat) / 4503599627370496 then
               (4503599627370496 + ((↑v : Nat) % 4503599627370496 : Nat) : Real) *
                 2 ^ ((↑v : Nat) / 4503599627370496 - 1075)
             else (4503599627370496 + ((↑v : Nat) % 4503599627370496 : Nat) : Real) /
               2 ^ (1075 - (↑v : Nat) / 4503599627370496)) ≤ 0 :=
          sub_nonpos.mpr hnon_goal
        rw [abs_of_nonneg x_nonnegative, abs_of_nonpos hfinal]
        have htwo_goal : 2 * ((w.val : Real) * (10 : Real) ^ q.val) ≤
            (if (↑v : Nat) < 4503599627370496 then
                (↑((↑v : Nat) % 4503599627370496) : Real) / 2 ^ 1074
             else if 1075 ≤ (↑v : Nat) / 4503599627370496 then
               (4503599627370496 + ((↑v : Nat) % 4503599627370496 : Nat) : Real) *
                 2 ^ ((↑v : Nat) / 4503599627370496 - 1075)
             else (4503599627370496 + ((↑v : Nat) % 4503599627370496 : Nat) : Real) /
               2 ^ (1075 - (↑v : Nat) / 4503599627370496)) := by
          norm_num at htwo' ⊢
          exact htwo'
        linarith [htwo_goal]
  · rcases hover with ⟨hw, hq, hfp⟩
    subst fp
    refine ⟨⟨BitVec.ofNat 64 (2047 * 2 ^ 52)⟩, ?_, ?_⟩
    · change (⟨BitVec.ofNat 64 (2047 * 2 ^ 52)⟩ : F64).val =
        Nat.lor (0#u64).val (((2047#i32).val.emod (2 ^ 64)).toNat * (2 ^ 52)) % (2 ^ 64)
      have hs : (2047#i32).val = 2047 := by
        exact IScalar.ofInt_val_eq (by norm_num)
      have hz : (0#u64).val = 0 := by rfl
      rw [hs, hz]
      norm_num [Int.toNat, UScalar.val, BitVec.ofNat]
    ·
      have carrier_eq : (⟨BitVec.ofNat 64 (2047 * 2 ^ 52)⟩ : F64) =
          (9218868437227405312#u64) := by
        apply UScalar.eq_of_val_eq
        norm_num [UScalar.val, BitVec.ofNat]
      rw [carrier_eq]
      dsimp [DecimalGoalVocabularyDefault]
      have qlow : (309 : Int) ≤ q.val := by omega
      have wone : (1 : Real) ≤ (w.val : Real) := by
        exact_mod_cast (Nat.succ_le_iff.mp hw)
      have hp : (10 : Real) ^ (309 : Int) ≤ (10 : Real) ^ q.val := by
        exact zpow_le_zpow_right₀ (by norm_num) qlow
      have hx309 : (10 : Real) ^ (309 : Int) ≤
          (w.val : Real) * (10 : Real) ^ q.val := by
        calc
          (10 : Real) ^ (309 : Int) ≤ (10 : Real) ^ q.val := hp
          _ ≤ (w.val : Real) * (10 : Real) ^ q.val := by
            simpa only [one_mul] using
              (mul_le_mul_of_nonneg_right wone
                (zpow_pos (by norm_num : (0 : Real) < 10) q.val).le)
      have hbig : (2 : Real) ^ 1024 - 2 ^ 970 ≤ (10 : Real) ^ (309 : Int) := by
        rw [zpow_ofNat]
        set_option maxRecDepth 1000000 in
          set_option exponentiation.threshold 2048 in norm_num
      have hthreshold : (2 : Real) ^ 1024 - 2 ^ 970 ≤
          (w.val : Real) * (10 : Real) ^ q.val := hbig.trans hx309
      have target_raw : (9218868437227405312#u64).val =
          2047 * 2 ^ 52 := by
        norm_num [UScalar.val, BitVec.ofNat]
      rw [target_raw]
      norm_num
      have hxnonnegative : 0 ≤ (w.val : Real) * (10 : Real) ^ q.val := by
        exact mul_nonneg (by positivity) (zpow_pos (by norm_num) _).le
      refine ⟨hxnonnegative, ?_⟩
      calc
        (2 : Real) ^ 1024 = (2 ^ 1024 - 2 ^ 970) + 2 ^ 970 := by ring
        _ ≤ (w.val : Real) * (10 : Real) ^ q.val + 2 ^ 970 := by
          simpa [add_comm] using add_le_add_left hthreshold (2 ^ 970 : Real)

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalGoalVocabularyDefault
import Tablet.F64
import Tablet.num_imp_dec2flt_common_BiasedFp

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: IntegerEndpointRounding]
theorem IntegerEndpointRounding :
    ∀ (q : Std.I64) (w : Std.U64)
        (fp : num.imp.dec2flt.common.BiasedFp),
      ((w.val = 0 ∧
          fp = { m := 0#u64, p_biased := 0#i32 }) ∨
        (0 < w.val ∧ q.val < -342 ∧
          fp = { m := 0#u64, p_biased := 0#i32 }) ∨
        (0 < w.val ∧ 308 < q.val ∧
          fp = { m := 0#u64, p_biased := 2047#i32 })) →
      ∃ target : F64,
        DecimalGoalVocabularyDefault.assembledWord fp target ∧
        DecimalGoalVocabularyDefault.roundsMagnitude
          ((w.val : Real) * (10 : Real) ^ q.val) target := by
-- BODY
  set_option maxRecDepth 1000000 in
   intro q w fp h
  rcases h with hzero | hsmall | hover
  · rcases hzero with ⟨hw, hfp⟩
    subst fp
    refine ⟨⟨BitVec.ofNat 64 0⟩, ?_, ?_⟩
    · change (⟨BitVec.ofNat 64 0⟩ : F64).val =
        Nat.lor (0#u64).val (((0#i32).val.emod (2 ^ 64)).toNat * (2 ^ 52)) % (2 ^ 64)
      rfl
    · dsimp [DecimalGoalVocabularyDefault]
      have target_val : (⟨BitVec.ofNat 64 0⟩ : F64).val = 0 := by rfl
      rw [target_val, hw]
      norm_num
      exact pow_lt_pow_right₀ (by norm_num) (by omega)
  · rcases hsmall with ⟨hw, hq, hfp⟩
    subst fp
    refine ⟨⟨BitVec.ofNat 64 0⟩, ?_, ?_⟩
    · change (⟨BitVec.ofNat 64 0⟩ : F64).val =
        Nat.lor (0#u64).val (((0#i32).val.emod (2 ^ 64)).toNat * (2 ^ 52)) % (2 ^ 64)
      rfl
    · dsimp [DecimalGoalVocabularyDefault]
      have target_val : (⟨BitVec.ofNat 64 0⟩ : F64).val = 0 := by rfl
      rw [target_val]
      let value : Nat → Real := fun n =>
        if n < 2 ^ 52 then ((n % 2 ^ 52 : Nat) : Real) / 2 ^ 1074
        else if 1075 ≤ n / 2 ^ 52 then
          ((2 ^ 52 + n % 2 ^ 52 : Nat) : Real) * 2 ^ (n / 2 ^ 52 - 1075)
        else ((2 ^ 52 + n % 2 ^ 52 : Nat) : Real) / 2 ^ (1075 - n / 2 ^ 52)
      have floor_value : ∀ n : Nat, 0 < n →
          (2 : Real) ^ (-1074 : Int) ≤ value n := by
        intro n hn
        dsimp [value]
        by_cases hsub : n < 2 ^ 52
        · have hsub' : n < 4503599627370496 := by norm_num at hsub ⊢; exact hsub
          simp [hsub', Nat.mod_eq_of_lt hsub]
          have hprod : (2 ^ 1074 : Real)⁻¹ * 2 ^ 1074 = 1 := by
            exact inv_mul_cancel₀ (by positivity)
          rw [le_div_iff₀ (by positivity : (0 : Real) < 2 ^ 1074)]
          calc
            (2 ^ 1074 : Real)⁻¹ * 2 ^ 1074 = 1 := hprod
            _ ≤ (n % 4503599627370496 : Nat) := by
              have hmod : n % 4503599627370496 = n := by
                apply Nat.mod_eq_of_lt
                norm_num at hsub ⊢
                exact hsub
              rw [hmod]
              exact_mod_cast (Nat.succ_le_iff.mp hn)
        · have hsub' : ¬ n < 4503599627370496 := by
            intro h
            apply hsub
            norm_num at h ⊢
            exact h
          simp only [if_neg hsub']
          by_cases hnorm : 1075 ≤ n / 2 ^ 52
          · have hnorm' : 1075 ≤ n / 4503599627370496 := by
              norm_num at hnorm ⊢
              exact hnorm
            rw [if_pos hnorm']
            have hpall : ∀ k : Nat, (1 : Real) ≤ 2 ^ k := by
              intro k
              induction k with
              | zero => norm_num
              | succ k ih => rw [pow_succ]; nlinarith
            have hp : (1 : Real) ≤ 2 ^ (n / 2 ^ 52 - 1075 : Nat) := hpall _
            have hm : (2 : Real) ^ (-1074 : Int) ≤ 2 ^ 52 := by
              have hm' : (2 : Real) ^ (-1074 : Int) ≤ (2 : Real) ^ (52 : Int) :=
                zpow_le_zpow_right₀ (by norm_num) (by norm_num)
              simpa using hm'
            have hnume : (2 : Real) ^ 52 ≤ ((2 ^ 52 + n % 2 ^ 52 : Nat) : Real) := by
              exact_mod_cast Nat.le_add_right _ _
            exact le_trans hm (le_trans hnume (calc
              ((2 ^ 52 + n % 2 ^ 52 : Nat) : Real) =
                  ((2 ^ 52 + n % 2 ^ 52 : Nat) : Real) * 1 := by ring
              _ ≤ ((2 ^ 52 + n % 2 ^ 52 : Nat) : Real) *
                  2 ^ (n / 2 ^ 52 - 1075 : Nat) := by
                have hc : (0 : Real) ≤ ((2 ^ 52 + n % 2 ^ 52 : Nat) : Real) := by positivity
                exact mul_le_mul_of_nonneg_left hp hc))
          · have hnorm' : ¬ 1075 ≤ n / 4503599627370496 := by
              intro h
              apply hnorm
              norm_num at h ⊢
              exact h
            rw [if_neg hnorm']
            have he : n / 2 ^ 52 < 1075 := by omega
            have hden : 2 ^ (1075 - n / 2 ^ 52 : Nat) ≤ (2 : Real) ^ 1074 := by
              apply pow_le_pow_right₀ (by norm_num)
              omega
            have hnume : (2 : Real) ^ 52 ≤ ((2 ^ 52 + n % 2 ^ 52 : Nat) : Real) := by
              exact_mod_cast Nat.le_add_right _ _
            have hquot : (2 : Real) ^ 52 / 2 ^ 1074 ≤
                ((2 ^ 52 + n % 2 ^ 52 : Nat) : Real) /
                  2 ^ (1075 - n / 2 ^ 52 : Nat) := by
              gcongr
            have hbase : (2 : Real) ^ (-1074 : Int) ≤ 2 ^ 52 / 2 ^ 1074 := by
              set_option exponentiation.threshold 2048 in
                norm_num [zpow_neg, div_eq_mul_inv]
            exact hbase.trans hquot
      have qbound : q.val ≤ -343 := by omega
      have wbound : w.val ≤ 2 ^ 64 - 1 := by
        have h := w.hBounds
        norm_num at h ⊢
        omega
      have x_nonnegative : 0 ≤ (w.val : Real) * (10 : Real) ^ q.val := by
        exact mul_nonneg (by positivity) (zpow_pos (by norm_num) _).le
      have x_small : (w.val : Real) * (10 : Real) ^ q.val <
          (2 : Real) ^ (-1075 : Int) := by
        have hpow : (10 : Real) ^ q.val ≤ (10 : Real) ^ (-343 : Int) := by
          exact zpow_le_zpow_right₀ (by norm_num) qbound
        have hmul : (w.val : Real) * (10 : Real) ^ q.val ≤
            ((2 ^ 64 - 1 : Nat) : Real) * (10 : Real) ^ (-343 : Int) := by
          have hmax : (0 : Real) ≤ (2 ^ 64 - 1 : Nat) := by positivity
          exact mul_le_mul (by exact_mod_cast wbound) hpow
            (zpow_pos (by norm_num) _).le hmax
        have hnum : ((2 ^ 64 - 1 : Nat) : Real) * (10 : Real) ^ (-343 : Int) <
            (2 : Real) ^ (-1075 : Int) := by
          rw [zpow_neg, zpow_neg, ← div_eq_mul_inv, ← one_div]
          apply (div_lt_div_iff₀ (by norm_num : (0 : Real) < 10 ^ 343)
            (by norm_num : (0 : Real) < 2 ^ 1075)).2
          have hn : (2 ^ 64 - 1 : Nat) * 2 ^ 1075 < 10 ^ 343 := by
            set_option maxRecDepth 1000000 in
              norm_num [pow_succ]
          exact_mod_cast hn
        exact hmul.trans_lt hnum
      norm_num
      refine ⟨x_nonnegative, ?_, ?_⟩
      · have hth : (2 : Real) ^ (-1075 : Int) <
            (2 : Real) ^ 1024 - 2 ^ 970 := by
          have hp : (0 : Real) < 2 ^ 970 := by positivity
          have hpow : (2 : Real) ^ 970 < 2 ^ 1024 :=
            pow_lt_pow_right₀ (by norm_num) (by omega)
          have hone : (1 : Real) < 2 ^ 1024 - 2 ^ 970 := by
            set_option exponentiation.threshold 2048 in
              have hone : (1 : Real) ≤ 2 ^ 970 := by
                exact_mod_cast (show (1 : Nat) ≤ 2 ^ 970 by omega)
              nlinarith [hpow, hone]
          have hsmallone : (2 : Real) ^ (-1075 : Int) < 1 := by
            rw [zpow_neg]
            exact inv_lt_one_of_one_lt₀ (by norm_num)
          exact lt_trans hsmallone hone
        exact x_small.trans hth
      · intro v hv hne
        have vpos : 0 < v.val := by
          by_contra hn
          have hz : v.val = 0 := by omega
          apply hne
          simpa [target_val] using hz
        have hfloor := floor_value v.val vpos
        have htwo : 2 * ((w.val : Real) * (10 : Real) ^ q.val) ≤ value v.val := by
          have hhalf : (w.val : Real) * (10 : Real) ^ q.val ≤
              (2 : Real) ^ (-1075 : Int) := x_small.le
          have hpow2 : 2 * (2 : Real) ^ (-1075 : Int) = (2 : Real) ^ (-1074 : Int) := by
            calc
              2 * (2 : Real) ^ (-1075 : Int) =
                  (2 : Real) ^ (1 : Int) * 2 ^ (-1075 : Int) := by norm_num
              _ = (2 : Real) ^ (-1074 : Int) := by
                rw [← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
                norm_num
          calc
            2 * ((w.val : Real) * (10 : Real) ^ q.val) ≤
                2 * (2 : Real) ^ (-1075 : Int) :=
              mul_le_mul_of_nonneg_left hhalf (by norm_num)
            _ = (2 : Real) ^ (-1074 : Int) := hpow2
            _ ≤ value v.val := hfloor
        have hfloor' : (2 : Real) ^ (-1074 : Int) ≤
            (if v.val < 2 ^ 52 then ((v.val % 2 ^ 52 : Nat) : Real) / 2 ^ 1074
             else if 1075 ≤ v.val / 2 ^ 52 then
               ((2 ^ 52 + v.val % 2 ^ 52 : Nat) : Real) *
                 2 ^ (v.val / 2 ^ 52 - 1075)
             else ((2 ^ 52 + v.val % 2 ^ 52 : Nat) : Real) /
               2 ^ (1075 - v.val / 2 ^ 52)) := by
          simpa only [value] using hfloor
        have htwo' : 2 * ((w.val : Real) * (10 : Real) ^ q.val) ≤
            (if v.val < 2 ^ 52 then ((v.val % 2 ^ 52 : Nat) : Real) / 2 ^ 1074
             else if 1075 ≤ v.val / 2 ^ 52 then
               ((2 ^ 52 + v.val % 2 ^ 52 : Nat) : Real) *
                 2 ^ (v.val / 2 ^ 52 - 1075)
             else ((2 ^ 52 + v.val % 2 ^ 52 : Nat) : Real) /
               2 ^ (1075 - v.val / 2 ^ 52)) := by
          simpa only [value] using htwo
        have habsx : |(w.val : Real) * (10 : Real) ^ q.val| =
            (w.val : Real) * |(10 : Real) ^ q.val| := by
          rw [abs_mul, abs_of_nonneg (by positivity)]
        rw [← habsx]
        have hupper : (w.val : Real) * (10 : Real) ^ q.val ≤
            (if v.val < 4503599627370496 then ((v.val % 4503599627370496 : Nat) : Real) / 2 ^ 1074
             else if 1075 ≤ v.val / 4503599627370496 then
               ((4503599627370496 + v.val % 4503599627370496 : Nat) : Real) *
                 2 ^ (v.val / 4503599627370496 - 1075)
             else ((4503599627370496 + v.val % 4503599627370496 : Nat) : Real) /
               2 ^ (1075 - v.val / 4503599627370496)) := by
          norm_num at htwo' ⊢
          linarith [htwo']
        have hnon : (w.val : Real) * (10 : Real) ^ q.val -
            (if v.val < 4503599627370496 then ((v.val % 4503599627370496 : Nat) : Real) / 2 ^ 1074
             else if 1075 ≤ v.val / 4503599627370496 then
               ((4503599627370496 + v.val % 4503599627370496 : Nat) : Real) *
                 2 ^ (v.val / 4503599627370496 - 1075)
             else ((4503599627370496 + v.val % 4503599627370496 : Nat) : Real) /
               2 ^ (1075 - v.val / 4503599627370496)) ≤ 0 := by
          norm_num at hupper ⊢
          linarith
        have hnon' : (w.val : Real) * (10 : Real) ^ q.val -
            (if (↑v : Nat) < 4503599627370496 then
                (↑((↑v : Nat) % 4503599627370496) : Real) / 2 ^ 1074
             else if 1075 ≤ (↑v : Nat) / 4503599627370496 then
               (↑(4503599627370496 + (↑v : Nat) % 4503599627370496) : Real) *
                 2 ^ ((↑v : Nat) / 4503599627370496 - 1075)
             else (↑(4503599627370496 + (↑v : Nat) % 4503599627370496) : Real) /
               2 ^ (1075 - (↑v : Nat) / 4503599627370496)) ≤ 0 := by
          simpa only using hnon
        have hnon_goal : (w.val : Real) * (10 : Real) ^ q.val -
            (if (↑v : Nat) < 4503599627370496 then
                (↑((↑v : Nat) % 4503599627370496) : Real) / 2 ^ 1074
             else if 1075 ≤ (↑v : Nat) / 4503599627370496 then
               (↑(4503599627370496 + (↑v : Nat) % 4503599627370496) : Real) *
                 2 ^ ((↑v : Nat) / 4503599627370496 - 1075)
             else (↑(4503599627370496 + (↑v : Nat) % 4503599627370496) : Real) /
               2 ^ (1075 - (↑v : Nat) / 4503599627370496)) ≤ 0 := by
          simpa only using hnon'
        norm_num at hnon_goal
        change |(w.val : Real) * (10 : Real) ^ q.val| ≤
          |(w.val : Real) * (10 : Real) ^ q.val -
            (if (↑v : Nat) < 4503599627370496 then
                (↑((↑v : Nat) % 4503599627370496) : Real) / 2 ^ 1074
             else if 1075 ≤ (↑v : Nat) / 4503599627370496 then
               (4503599627370496 + ((↑v : Nat) % 4503599627370496 : Nat) : Real) *
                 2 ^ ((↑v : Nat) / 4503599627370496 - 1075)
             else (4503599627370496 + ((↑v : Nat) % 4503599627370496 : Nat) : Real) /
               2 ^ (1075 - (↑v : Nat) / 4503599627370496))|
        have hfinal : (w.val : Real) * (10 : Real) ^ q.val -
            (if (↑v : Nat) < 4503599627370496 then
                (↑((↑v : Nat) % 4503599627370496) : Real) / 2 ^ 1074
             else if 1075 ≤ (↑v : Nat) / 4503599627370496 then
               (4503599627370496 + ((↑v : Nat) % 4503599627370496 : Nat) : Real) *
                 2 ^ ((↑v : Nat) / 4503599627370496 - 1075)
             else (4503599627370496 + ((↑v : Nat) % 4503599627370496 : Nat) : Real) /
               2 ^ (1075 - (↑v : Nat) / 4503599627370496)) ≤ 0 :=
          sub_nonpos.mpr hnon_goal
        rw [abs_of_nonneg x_nonnegative, abs_of_nonpos hfinal]
        have htwo_goal : 2 * ((w.val : Real) * (10 : Real) ^ q.val) ≤
            (if (↑v : Nat) < 4503599627370496 then
                (↑((↑v : Nat) % 4503599627370496) : Real) / 2 ^ 1074
             else if 1075 ≤ (↑v : Nat) / 4503599627370496 then
               (4503599627370496 + ((↑v : Nat) % 4503599627370496 : Nat) : Real) *
                 2 ^ ((↑v : Nat) / 4503599627370496 - 1075)
             else (4503599627370496 + ((↑v : Nat) % 4503599627370496 : Nat) : Real) /
               2 ^ (1075 - (↑v : Nat) / 4503599627370496)) := by
          norm_num at htwo' ⊢
          exact htwo'
        linarith [htwo_goal]
  · rcases hover with ⟨hw, hq, hfp⟩
    subst fp
    refine ⟨⟨BitVec.ofNat 64 (2047 * 2 ^ 52)⟩, ?_, ?_⟩
    · change (⟨BitVec.ofNat 64 (2047 * 2 ^ 52)⟩ : F64).val =
        Nat.lor (0#u64).val (((2047#i32).val.emod (2 ^ 64)).toNat * (2 ^ 52)) % (2 ^ 64)
      have hs : (2047#i32).val = 2047 := by
        exact IScalar.ofInt_val_eq (by norm_num)
      have hz : (0#u64).val = 0 := by rfl
      rw [hs, hz]
      norm_num [Int.toNat, UScalar.val, BitVec.ofNat]
    ·
      have carrier_eq : (⟨BitVec.ofNat 64 (2047 * 2 ^ 52)⟩ : F64) =
          (9218868437227405312#u64) := by
        apply UScalar.eq_of_val_eq
        norm_num [UScalar.val, BitVec.ofNat]
      rw [carrier_eq]
      dsimp [DecimalGoalVocabularyDefault]
      have qlow : (309 : Int) ≤ q.val := by omega
      have wone : (1 : Real) ≤ (w.val : Real) := by
        exact_mod_cast (Nat.succ_le_iff.mp hw)
      have hp : (10 : Real) ^ (309 : Int) ≤ (10 : Real) ^ q.val := by
        exact zpow_le_zpow_right₀ (by norm_num) qlow
      have hx309 : (10 : Real) ^ (309 : Int) ≤
          (w.val : Real) * (10 : Real) ^ q.val := by
        calc
          (10 : Real) ^ (309 : Int) ≤ (10 : Real) ^ q.val := hp
          _ ≤ (w.val : Real) * (10 : Real) ^ q.val := by
            simpa only [one_mul] using
              (mul_le_mul_of_nonneg_right wone
                (zpow_pos (by norm_num : (0 : Real) < 10) q.val).le)
      have hbig : (2 : Real) ^ 1024 - 2 ^ 970 ≤ (10 : Real) ^ (309 : Int) := by
        rw [zpow_ofNat]
        set_option maxRecDepth 1000000 in
          set_option exponentiation.threshold 2048 in norm_num
      have hthreshold : (2 : Real) ^ 1024 - 2 ^ 970 ≤
          (w.val : Real) * (10 : Real) ^ q.val := hbig.trans hx309
      have target_raw : (9218868437227405312#u64).val =
          2047 * 2 ^ 52 := by
        norm_num [UScalar.val, BitVec.ofNat]
      rw [target_raw]
      norm_num
      have hxnonnegative : 0 ≤ (w.val : Real) * (10 : Real) ^ q.val := by
        exact mul_nonneg (by positivity) (zpow_pos (by norm_num) _).le
      refine ⟨hxnonnegative, ?_⟩
      calc
        (2 : Real) ^ 1024 = (2 ^ 1024 - 2 ^ 970) + 2 ^ 970 := by ring
        _ ≤ (w.val : Real) * (10 : Real) ^ q.val + 2 ^ 970 := by
          simpa [add_comm] using add_le_add_left hthreshold (2 ^ 970 : Real)

end dec2flt_model

Proof structure

Follow the declared dependencies to read the supporting statements and proofs.

Explore this dependency tree

Inspect related source (44)
More program dependencies (11)
Standard-library dependencies (30)

Related source and models come from the extraction records and declared dependencies.

Proof assumptions and review

Lean checks the formal proof. Trellis also reviews how the statement corresponds to the intended requirement.

Transitive assumptions

This list combines the assumptions recorded for this declaration and its dependencies. It may include assumptions used by auxiliary declarations in those modules.

Helper theorems included
All 7 entries
Technical checking records
Local closure summary
{
  "node": "IntegerEndpointRounding",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "7e2ad2278bb1d3130a5ac55ef05721785a25b842d4e3de25355a4e40d57c6ecf",
  "active_statement_hash": "166e636fce30c435541fc4bb7255d05a57d3bf611b097cf492af4961cda56e46",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "ab64d13f256386ee0e494a5abcc6a5db49512cf3d90bbdfcabd6327793a31f95",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "sidecar-sc-20261003-040321-81654f-IntegerEndpointRounding-cycle-142",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "DecimalGoalVocabularyDefault": "2bbf2be48b5a4a4b"
  },
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "f76c8174a4d58eb5",
    "DecimalGoalVocabularyDefault": "512f8d0aea894250",
    "F64": "ec4e25fbaf4e11d4",
    "num_imp_dec2flt_common_BiasedFp": "63baac539d218a81"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "matches current fingerprint",
  "assessment_fingerprint": "sound-v2:0bb195771f80647f5db6a18c12f1a7f68ef89c14350c3933e859a9d9aa9e75b1",
  "current_fingerprint": "sound-v2:0bb195771f80647f5db6a18c12f1a7f68ef89c14350c3933e859a9d9aa9e75b1",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}