P529_T1_EncodingArithmetic

Lean checked

Statement

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

Lemma · Finite encoding arithmetic and both saturation tests

Let -1022\le e\le1260, m,m_4 be natural numbers, and t=X/2^{e-52} satisfy 0<t<T=2^{53}. Assume e>-1022 implies U=2^{52}\le t. Suppose m satisfies both nearest half-step inequalities for t and is even at either tie. If m\ge T, suppose m_4 independently satisfies both nearest half-step inequalities for t/2 and is even at either tie. Define the raw arithmetic result, in source test order, by
r=\begin{cases} 2047U,&e\ge1024,\\ 2047U,&e<1024,\ m\ge T,\ e\ge1023,\\ (e+1024-\mathbf1_{m_4<U})U+(m_4\bmod U), &e<1023,\ m\ge T,\\ (e+1023-\mathbf1_{m<U})U+(m\bmod U), &e<1024,\ m<T . \end{cases}
Integer coefficients in the last two rows are converted to naturals; the proof establishes their nonnegativity. Then X\ge0 and at least one of these raw-word alternatives holds, with U=2^{52}, T=2^{53}, J=2047U and H=2^{1024}-2^{970}:
  1. r=J and H\le X;
  2. for some natural j\le T, r=j, X\le T2^{-1074}, X\le j2^{-1074}+2^{-1075}, j2^{-1074}\le X+2^{-1075}, and (X=(j+1/2)2^{-1074}\ \lor\ X=(j-1/2)2^{-1074}) \Longrightarrow j\bmod2=0;
  3. for some natural p,j with 2\le p\le2046, U\le j\le T, and p<2046 or j<T, put \Delta=2^{p-1075}. Then r=pU+(j-U), U\Delta\le X\le T\Delta, X<H, X\le j\Delta+\Delta/2, j\Delta\le X+\Delta/2, and (X=(j+1/2)\Delta\ \lor\ X=(j-1/2)\Delta) \Longrightarrow j\bmod2=0.

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
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model

-- [TABLET NODE: P529_T1_EncodingArithmetic]
theorem P529_T1_EncodingArithmetic (X : Real) (e : Int) (m m4 : Nat)
    (hexpLo : -1022 <= e) (hexpHi : e <= 1260) :
    let t := X / (2 : Real) ^ (e - 52)
    0 < t -> t < (2 : Real) ^ (53 : Nat) ->
    (-1022 < e -> (2 ^ 52 : Nat) <= t) ->
    t <= (m : Real) + 1 / 2 -> (m : Real) <= t + 1 / 2 ->
    (t = (m : Real) + 1 / 2 \/ t = (m : Real) - 1 / 2 -> m % 2 = 0) ->
    (2 ^ 53 <= m ->
      t / 2 <= (m4 : Real) + 1 / 2 /\ (m4 : Real) <= t / 2 + 1 / 2 /\
      (t / 2 = (m4 : Real) + 1 / 2 \/ t / 2 = (m4 : Real) - 1 / 2 ->
        m4 % 2 = 0)) ->
    let raw : Nat :=
      if 1024 <= e then 2047 * (2 ^ 52)
      else if 2 ^ 53 <= m then
        if 1023 <= e then 2047 * (2 ^ 52)
        else (e + 1024 - (if m4 < 2 ^ 52 then 1 else 0)).toNat * (2 ^ 52) +
          m4 % (2 ^ 52)
      else (e + 1023 - (if m < 2 ^ 52 then 1 else 0)).toNat * (2 ^ 52) +
        m % (2 ^ 52)
    0 <= X /\ ((raw = 2047 * (2 ^ 52) /\
        (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) <= X) \/
      (exists j : Nat,
        j <= 2 ^ 53 /\ raw = j /\
        X <= (2 ^ 53 : Nat) * (2 : Real) ^ (-1074 : Int) /\
        X <= (j : Real) * (2 : Real) ^ (-1074 : Int) + (2 : Real) ^ (-1075 : Int) /\
        (j : Real) * (2 : Real) ^ (-1074 : Int) <= X + (2 : Real) ^ (-1075 : Int) /\
        (X = ((j : Real) + 1 / 2) * (2 : Real) ^ (-1074 : Int) \/
          X = ((j : Real) - 1 / 2) * (2 : Real) ^ (-1074 : Int) -> j % 2 = 0)) \/
      (exists p j : Nat,
        2 <= p /\ p <= 2046 /\ 2 ^ 52 <= j /\ j <= 2 ^ 53 /\
        (p < 2046 \/ j < 2 ^ 53) /\
        raw = p * (2 ^ 52) + (j - 2 ^ 52) /\
        let delta : Real := (2 : Real) ^ ((p : Int) - 1075)
        (2 ^ 52 : Nat) * delta <= X /\ X <= (2 ^ 53 : Nat) * delta /\
        X < (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) /\
        X <= (j : Real) * delta + delta / 2 /\
        (j : Real) * delta <= X + delta / 2 /\
        (X = ((j : Real) + 1 / 2) * delta \/
          X = ((j : Real) - 1 / 2) * delta -> j % 2 = 0))) := 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

All scales are positive, so t>0 gives X>0. Nearest inequalities and t<T give m<T+1/2, hence m\le T. If m\ge T, then m=T and T-1/2\le t<T. Dividing by two gives U-1/4\le t/2<U. The separate inequalities for m_4 imply U-3/4\le m_4<U+1/2. Since m_4 is integral, m_4=U.
If the first saturation test succeeds, e\ge1024>-1022 gives t\ge U and X=t2^{e-52}\ge U2^{e-52}=2^e\ge2^{1024}>H. The raw result is J, so the infinity alternative holds.
Assume now e\le1023. If carry occurs and the second test succeeds, e=1023 and m=T. The first nearest lower bound gives
X\ge(T-1/2)2^{971}=2^{1024}-2^{970}=H,
so again the infinity alternative holds. If carry occurs but the second test is false, e\le1022 and m_4=U, so the decrement indicator is zero, the remainder is zero, and r=(e+1024)U. When e=-1022, take the low-grid witness j=T. When e>-1022, take p=e+1023, j=T. Then 2\le p\le2045 and r=(p+1)U=pU+(T-U). For either witness the first-round nearest inequalities scaled by 2^{e-52} give the stated distance bounds; j=T is even. The magnitude interval follows from U\le t<T in the normal case, or t<T in the low-grid case.
In the noncarry case m<T, set j=m. For e=-1022, the coefficient is one before conditional decrement. If m<U, decrement gives zero and the remainder is m. If U\le m<T=2U, no decrement occurs and the remainder is m-U. Both cases give r=m=j, so the low-grid alternative follows by scaling the first-round inequalities and tie implication by 2^{-1074}. Also X=t2^{-1074}<T2^{-1074}.
For e>-1022, t\ge U and m\ge t-1/2\ge U-1/2 force m\ge U by integrality. Thus no decrement occurs, and r=(e+1023)U+(m-U). Take p=e+1023, j=m; then 2\le p\le2046 and U\le j<T. The scale 2^{e-52} is exactly \Delta=2^{p-1075}. The assumed U\le t<T gives the binade bounds and scaling gives the nearest inequalities and the exact even-tie implication.
Finally verify the finite threshold clause, including the top endpoint. For every finite case with e\le1022, X<T2^{e-52}=2^{e+1} \le2^{1023}<H. If e=1023 and the result is finite, carry cannot occur, so m\le T-1. The nearest upper bound gives t\le T-1/2. Equality forces m=T-1 and t=m+1/2, contradicting the even-tie implication because T-1 is odd. Hence t<T-1/2 and X<(T-1/2)2^{971}=H. All raw coefficients are nonnegative: the first bias is at least one and its conditional decrement at most one; the carry bias is at least two and its decrement is zero. This proves every alternative and covers zero/subnormal, finite carry and exact top-overflow ties.

Lean proof


  set_option maxHeartbeats 2000000 in
  exact (by
    intro t htpos htT hnormal htupper hmlower htie hcarry
    intro raw
    change 0 < t at htpos
    change t < (2 : Real) ^ (53 : Nat) at htT
    change (-1022 < e -> (2 ^ 52 : Nat) <= t) at hnormal
    change t <= (m : Real) + 1 / 2 at htupper
    change (m : Real) <= t + 1 / 2 at hmlower
    change (t = (m : Real) + 1 / 2 \/ t = (m : Real) - 1 / 2 -> m % 2 = 0) at htie
    change (2 ^ 53 <= m ->
      t / 2 <= (m4 : Real) + 1 / 2 /\ (m4 : Real) <= t / 2 + 1 / 2 /\
      (t / 2 = (m4 : Real) + 1 / 2 \/ t / 2 = (m4 : Real) - 1 / 2 -> m4 % 2 = 0)) at hcarry
    have hs : 0 < (2 : Real) ^ (e - 52) := zpow_pos (by norm_num) _
    have hXscale : X = t * (2 : Real) ^ (e - 52) := by
      dsimp [t]
      exact (div_mul_cancel₀ X (ne_of_gt hs)).symm
    have hXpos : 0 < X := by rw [hXscale]; exact mul_pos htpos hs
    have hXnonneg : 0 <= X := le_of_lt hXpos
    let p : Nat := (e + 1023).toNat
    have hpvalGlobal : (-1022 < e) -> (p : Int) = e + 1023 := by
      intro heN
      dsimp [p]
      have hle : (1 : Int) <= e + 1023 := by
        have := add_le_add_right (le_of_lt heN) (1023 : Int)
        simpa [add_comm] using this
      exact Int.toNat_of_nonneg (le_trans (by norm_num) hle)
    have hmupper : m <= 2 ^ 53 := by
      have hmreal : (m : Real) < (2 : Real) ^ (53 : Nat) + 1 := by linarith
      have hn : m < 2 ^ 53 + 1 := by exact_mod_cast hmreal
      norm_num at hn
      omega
    have hmodOfLt {a b : Nat} (hab : a < b) : a % b = a := by
      exact Nat.mod_eq_of_lt hab
    have hsuccEvenOdd {a b : Nat} (hab : a + 1 = 2 * b) : a % 2 = 1 := by
      omega
    have hhalfShift (a b : Real) (h : a + 1 / 2 = b - 1 / 2) : a = b - 1 := by
      linarith
    have hminusOneSucc (a b : Real) (h : a = b - 1) : a + 1 = b := by
      linarith
    have hdouble53 : 2 ^ 53 = 2 * 2 ^ 52 := by
      calc
        2 ^ 53 = 2 ^ (52 + 1) := by norm_num
        _ = 2 ^ 52 * 2 := by rw [Nat.pow_succ]
        _ = 2 * 2 ^ 52 := Nat.mul_comm _ _
    have hpow52cast : (2 ^ 52 : Nat) = (2 : Real) ^ (52 : Nat) := by
      norm_num [Nat.cast_pow]
    have hscalePow : (2 ^ 52 : Nat) * (2 : Real) ^ (e - 52) = (2 : Real) ^ (e : Int) := by
      rw [hpow52cast, ← zpow_natCast (2 : Real) 52, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
      congr 1
      omega
    have htopH : (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) <=
        (2 : Real) ^ (1024 : Int) := by
      have heq : (2 : Real) ^ (1024 : Int) = (2 : Real) ^ (1024 : Nat) := by
        norm_num [zpow_natCast]
        rfl
      rw [heq]
      exact sub_le_self _ (by positivity)
    have hroundGrid (s : Real) (hsp : 0 < s)
        (hseq : (2 : Real) ^ (e - 52) = s) :
        X <= (m : Real) * s + s / 2 /\ (m : Real) * s <= X + s / 2 := by
      have hu := mul_le_mul_of_nonneg_right htupper (le_of_lt hsp)
      have hl := mul_le_mul_of_nonneg_right hmlower (le_of_lt hsp)
      rw [hXscale, hseq]
      constructor <;> nlinarith [hu, hl]
    have hcapGrid (s : Real) (hsp : 0 < s)
        (hseq : (2 : Real) ^ (e - 52) = s) :
        X <= (2 ^ 53 : Nat) * s := by
      have hc : t * s < (2 ^ 53 : Real) * s := mul_lt_mul_of_pos_right htT hsp
      rw [hXscale, hseq]
      have hcast : (2 ^ 53 : Nat) = (2 : Real) ^ (53 : Nat) := by norm_num [Nat.cast_pow]
      rw [hcast]
      exact hc.le
    have htieGrid (s : Real) (hsp : 0 < s)
        (hseq : (2 : Real) ^ (e - 52) = s) :
        (X = ((m : Real) + 1 / 2) * s \/ X = ((m : Real) - 1 / 2) * s) -> m % 2 = 0 := by
      intro hh
      rcases hh with hu | hl
      · have ht' : t = (m : Real) + 1 / 2 := by
          apply mul_left_cancel₀ (ne_of_gt hsp)
          calc
            s * t = X := by rw [hXscale, hseq]; ring
            _ = ((m : Real) + 1 / 2) * s := hu
            _ = s * ((m : Real) + 1 / 2) := by ring
        exact htie (Or.inl ht')
      · have ht' : t = (m : Real) - 1 / 2 := by
          apply mul_left_cancel₀ (ne_of_gt hsp)
          calc
            s * t = X := by rw [hXscale, hseq]; ring
            _ = ((m : Real) - 1 / 2) * s := hl
            _ = s * ((m : Real) - 1 / 2) := by ring
        exact htie (Or.inr ht')
    have hmLower (hN : -1022 < e) : 2 ^ 52 <= m := by
      have htn := hnormal hN
      have htnReal : (2 ^ 52 : Nat) <= t := by exact_mod_cast htn
      have hmReal : ((2 ^ 52 : Nat) : Real) - 1 / 2 <= (m : Real) := by
        have hmBound : (m : Real) + 1 / 2 >= t := by linarith [htupper]
        linarith
      by_contra h
      have hn : m + 1 <= 2 ^ 52 := by omega
      have hr : (m : Real) + 1 <= (2 ^ 52 : Real) := by exact_mod_cast hn
      linarith
    have hthreshold : (2 : Real) ^ (1023 : Int) <
        (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) := by
      have hsmall : (2 : Real) ^ (970 : Nat) < (2 : Real) ^ (1023 : Nat) := by
        have hs : (2 : Real) ^ (970 : Int) < (2 : Real) ^ (1023 : Int) :=
          zpow_lt_zpow_right₀ (by norm_num : (1 : Real) < 2) (by norm_num : (970 : Int) < 1023)
        simpa only [zpow_natCast] using hs
      have hpow : (2 : Real) ^ (1024 : Nat) =
          2 * (2 : Real) ^ (1023 : Nat) := by
        rw [show (1024 : Nat) = 1023 + 1 by norm_num, pow_succ]
        ring
      have hcast : (2 : Real) ^ (1023 : Int) = (2 : Real) ^ (1023 : Nat) := by
        norm_num [zpow_natCast]
        rfl
      rw [hcast, hpow]
      have hgap : 0 < (2 : Real) ^ (1023 : Nat) - (2 : Real) ^ (970 : Nat) :=
        sub_pos.mpr hsmall
      calc
        (2 : Real) ^ (1023 : Nat) <
            (2 : Real) ^ (1023 : Nat) +
              ((2 : Real) ^ (1023 : Nat) - (2 : Real) ^ (970 : Nat)) :=
          lt_add_of_pos_right _ hgap
        _ = 2 * (2 : Real) ^ (1023 : Nat) - (2 : Real) ^ (970 : Nat) := by ring
    have htopRelation : ((2 : Real) ^ (53 : Nat) - 1 / 2) *
        (2 : Real) ^ (971 : Nat) =
        (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) := by
      have htop : (2 : Real) ^ (53 : Nat) * (2 : Real) ^ (971 : Nat) =
          (2 : Real) ^ (1024 : Nat) := by
        rw [← zpow_natCast (2 : Real) 53, ← zpow_natCast (2 : Real) 971,
          ← zpow_natCast (2 : Real) 1024, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
        congr 1
      have hhalf : (1 / 2 : Real) * (2 : Real) ^ (971 : Nat) =
          (2 : Real) ^ (970 : Nat) := by
        rw [show (1 / 2 : Real) = (2 : Real) ^ (-1 : Int) by norm_num [zpow_neg]]
        rw [← zpow_natCast (2 : Real) 971, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
        norm_num [zpow_natCast]
        rfl
      calc
        ((2 : Real) ^ (53 : Nat) - 1 / 2) * (2 : Real) ^ (971 : Nat) =
            (2 : Real) ^ (53 : Nat) * (2 : Real) ^ (971 : Nat) -
              (1 / 2 : Real) * (2 : Real) ^ (971 : Nat) := by ring
        _ = (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) := by rw [htop, hhalf]
    have hfiniteBelow (he : e <= 1022) :
        X < (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) := by
      have hTcast : (2 ^ 53 : Nat) = (2 : Real) ^ (53 : Nat) := by norm_num [Nat.cast_pow]
      have hTscale : (2 ^ 53 : Nat) * (2 : Real) ^ (e - 52) =
          (2 : Real) ^ (e + 1) := by
        rw [hTcast]
        calc
          (2 : Real) ^ (53 : Nat) * (2 : Real) ^ (e - 52) =
              2 * ((2 ^ 52 : Nat) * (2 : Real) ^ (e - 52)) := by
                have hp : (2 : Real) ^ (53 : Nat) = 2 * (2 : Real) ^ (52 : Nat) := by
                  rw [show (53 : Nat) = 52 + 1 by norm_num, pow_succ]
                  ring
                rw [hp, hpow52cast]
                ring
          _ = 2 * (2 : Real) ^ (e : Int) := by rw [hscalePow]
          _ = (2 : Real) ^ (e + 1) := by
            rw [zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
            ring
      have htBound : X < (2 ^ 53 : Nat) * (2 : Real) ^ (e - 52) := by
        have hc := mul_lt_mul_of_pos_right htT hs
        calc
          X = t * (2 : Real) ^ (e - 52) := hXscale
          _ < (2 : Real) ^ (53 : Nat) * (2 : Real) ^ (e - 52) := hc
          _ = (2 ^ 53 : Nat) * (2 : Real) ^ (e - 52) := by rw [hTcast]
      have hp : (2 : Real) ^ (e + 1) <= (2 : Real) ^ (1023 : Int) :=
        zpow_le_zpow_right₀ (by norm_num : (1 : Real) <= 2) (show e + 1 <= 1023 by omega)
      calc
        X < (2 ^ 53 : Nat) * (2 : Real) ^ (e - 52) := htBound
        _ = (2 : Real) ^ (e + 1) := hTscale
        _ <= (2 : Real) ^ (1023 : Int) := hp
        _ < (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) := hthreshold
    refine ⟨hXnonneg, ?_⟩
    by_cases hextop : 1024 <= e
    · left
      constructor
      · simp [raw, hextop]
      · have hnormval := hnormal (by omega)
        have hnormreal : (2 ^ 52 : Real) <= t := by exact_mod_cast hnormval
        have hpowMono : (2 : Real) ^ (1024 : Int) <= (2 : Real) ^ (e : Int) :=
          zpow_le_zpow_right₀ (by norm_num : (1 : Real) <= 2) (by omega)
        have hmulse : (2 ^ 52 : Real) * (2 : Real) ^ (e - 52) <= X := by
          rw [hXscale]
          exact mul_le_mul_of_nonneg_right hnormreal (le_of_lt hs)
        have hepow : (2 : Real) ^ (e : Int) <=
            (2 ^ 52 : Real) * (2 : Real) ^ (e - 52) := by
          rw [hpow52cast] at hscalePow
          exact le_of_eq hscalePow.symm
        exact le_trans (le_trans (le_trans htopH hpowMono) hepow) hmulse
    · by_cases hcarrym : 2 ^ 53 <= m
      · have hm : m = 2 ^ 53 := Nat.le_antisymm hmupper hcarrym
        have hm4 : m4 = 2 ^ 52 := by
          have hc := hcarry hcarrym
          rcases hc with ⟨h4lo, h4hi, _⟩
          have h4upper : m4 <= 2 ^ 52 := by
            have ht2 : t / 2 < (2 : Real) ^ (52 : Nat) := by nlinarith [htT]
            by_contra hn
            have hn' : 2 ^ 52 + 1 <= m4 := by omega
            have hr : (2 ^ 52 : Real) + 1 <= (m4 : Real) := by exact_mod_cast hn'
            nlinarith
          have h4lower : 2 ^ 52 <= m4 := by
            have htlo : (2 : Real) ^ (52 : Nat) - 1 / 4 <= t / 2 := by
              rw [hm] at hmlower
              push_cast at hmlower
              nlinarith
            by_contra hn
            have hn' : m4 + 1 <= 2 ^ 52 := by omega
            have hr : (m4 : Real) + 1 <= (2 ^ 52 : Real) := by exact_mod_cast hn'
            nlinarith [h4lo]
          exact Nat.le_antisymm h4upper h4lower
        by_cases hecarryTop : 1023 <= e
        · have he : e = 1023 := by omega
          subst e
          have htlo : (2 ^ 53 : Real) - 1 / 2 <= t := by
            rw [hm] at hmlower
            push_cast at hmlower
            linarith
          have hXL : (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) <= X := by
            have hrel : ((2 : Real) ^ (53 : Nat) - 1 / 2) * (2 : Real) ^ (971 : Nat) =
                (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) := by
              have htop : (2 : Real) ^ (53 : Nat) * (2 : Real) ^ (971 : Nat) =
                  (2 : Real) ^ (1024 : Nat) := by
                rw [← zpow_natCast (2 : Real) 53, ← zpow_natCast (2 : Real) 971,
                  ← zpow_natCast (2 : Real) 1024, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
                congr 1
              have hhalf : (1 / 2 : Real) * (2 : Real) ^ (971 : Nat) =
                  (2 : Real) ^ (970 : Nat) := by
                rw [show (1 / 2 : Real) = (2 : Real) ^ (-1 : Int) by norm_num [zpow_neg]]
                rw [← zpow_natCast (2 : Real) 971, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
                norm_num [zpow_natCast]
                rfl
              calc
                ((2 : Real) ^ (53 : Nat) - 1 / 2) * (2 : Real) ^ (971 : Nat) =
                    (2 : Real) ^ (53 : Nat) * (2 : Real) ^ (971 : Nat) -
                      (1 / 2 : Real) * (2 : Real) ^ (971 : Nat) := by ring
                _ = (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) := by rw [htop, hhalf]
            have hmul := mul_le_mul_of_nonneg_right htlo
              (le_of_lt (by positivity : 0 < (2 : Real) ^ (971 : Nat)))
            calc
              (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) =
                  ((2 : Real) ^ (53 : Nat) - 1 / 2) * (2 : Real) ^ (971 : Nat) := hrel.symm
              _ <= t * (2 : Real) ^ (971 : Nat) := hmul
              _ = X := by
                rw [hXscale]
                rw [show (1023 : Int) - 52 = 971 by norm_num]
                rfl
          left
          constructor
          · norm_num [raw, hextop, hcarrym, hm, hm4]
          · exact hXL
        · have hecarryFin : e <= 1022 := by omega
          have hrawC : raw = (e + 1024).toNat * (2 ^ 52) := by
            simp [raw, hextop, hm, hm4, show ¬1023 <= e by omega]
          by_cases helow : e = -1022
          · have hseq : (2 : Real) ^ (e - 52) = (2 : Real) ^ (-1074 : Int) := by
              rw [helow]
              norm_num
            have hdelta : 0 < (2 : Real) ^ (-1074 : Int) := zpow_pos (by norm_num) _
            have hgrid := hroundGrid ((2 : Real) ^ (-1074 : Int)) hdelta hseq
            have hcap := hcapGrid ((2 : Real) ^ (-1074 : Int)) hdelta hseq
            have htie := htieGrid ((2 : Real) ^ (-1074 : Int)) hdelta hseq
            have hrawEq : raw = 2 ^ 53 := by
              rw [hrawC]
              have hc : (e + 1024).toNat = 2 := by rw [helow]; norm_num; rfl
              rw [hc]
              norm_num
            have hhalf : (2 : Real) ^ (-1074 : Int) / 2 =
                (2 : Real) ^ (-1075 : Int) := by
              rw [div_eq_mul_inv,
                show (2 : Real)⁻¹ = (2 : Real) ^ (-1 : Int) by norm_num [zpow_neg],
                ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
              norm_num
            have hhalfInv : (2 ^ 1074 : Real)⁻¹ / 2 = (2 ^ 1075 : Real)⁻¹ := by
              simpa [zpow_neg] using hhalf
            have hgridUp : X <= (m : Real) * (2 : Real) ^ (-1074 : Int) +
                (2 ^ 1075 : Real)⁻¹ := by
              have h' : X <= (m : Real) * (2 : Real) ^ (-1074 : Int) +
                  (2 ^ 1074 : Real)⁻¹ / 2 := by simpa [zpow_neg] using hgrid.1
              rw [← hhalfInv]
              exact h'
            have hgridLo : (m : Real) * (2 : Real) ^ (-1074 : Int) <=
                X + (2 ^ 1075 : Real)⁻¹ := by
              have h' : (m : Real) * (2 : Real) ^ (-1074 : Int) <= X +
                  (2 ^ 1074 : Real)⁻¹ / 2 := by simpa [zpow_neg] using hgrid.2
              rw [← hhalfInv]
              exact h'
            right
            left
            refine ⟨2 ^ 53, by norm_num, ?_⟩
            refine ⟨hrawEq, ?_, ?_, ?_, ?_⟩
            · exact hcap
            · simpa [hm, zpow_neg] using hgridUp
            · simpa [hm, zpow_neg] using hgridLo
            · simpa [hm] using htie
          · have heNorm : (-1022 : Int) < e := by
              set_option maxHeartbeats 1000000 in
                exact lt_of_le_of_ne (a := (-1022 : Int)) (b := e) hexpLo (Ne.symm helow)
            let p : Nat := (e + 1023).toNat
            have hpval : (p : Int) = e + 1023 := by
              dsimp [p]
              omega
            have hcoeff : (e + 1024).toNat = p + 1 := by
              dsimp [p]
              omega
            have hseq : (2 : Real) ^ (e - 52) =
                (2 : Real) ^ ((p : Int) - 1075) := by
              rw [hpval]
              have hexp : e - 52 = (e + 1023) - 1075 := by omega
              exact congrArg (fun k : Int => (2 : Real) ^ k) hexp
            set_option maxHeartbeats 1000000 in
              have hdelta : 0 < (2 : Real) ^ ((p : Int) - 1075) := by
                rw [← hseq]
                exact hs
            have hgrid := hroundGrid ((2 : Real) ^ ((p : Int) - 1075)) hdelta hseq
            have hcap := hcapGrid ((2 : Real) ^ ((p : Int) - 1075)) hdelta hseq
            have htie := htieGrid ((2 : Real) ^ ((p : Int) - 1075)) hdelta hseq
            have hrawEq : raw = p * (2 ^ 52) + (2 ^ 53 - 2 ^ 52) := by
              calc
                raw = (e + 1024).toNat * (2 ^ 52) := hrawC
                _ = p * (2 ^ 52) + (2 ^ 53 - 2 ^ 52) := by
                  rw [hcoeff]
                  norm_num
                  ring
            have hnormval := hnormal heNorm
            have hnormreal : (2 ^ 52 : Real) <= t := by exact_mod_cast hnormval
            have hXlo : (2 ^ 52 : Real) * (2 : Real) ^ ((p : Int) - 1075) <= X := by
              have hmul := mul_le_mul_of_nonneg_right hnormreal (le_of_lt hdelta)
              calc
                (2 ^ 52 : Real) * (2 : Real) ^ ((p : Int) - 1075) <=
                    t * (2 : Real) ^ ((p : Int) - 1075) := hmul
                _ = X := by rw [hXscale, hseq]
            have hXbelow := hfiniteBelow hecarryFin
            have hXloCast : (2 ^ 52 : Nat) * (2 : Real) ^ ((p : Int) - 1075) <= X := by
              rw [hpow52cast]
              exact hXlo
            right
            right
            have hpLo : 2 <= p := by
              have hi : (2 : Int) <= (p : Int) := by rw [hpval]; omega
              omega
            have hpHi : p <= 2046 := by
              have hi : (p : Int) <= 2045 := by rw [hpval]; omega
              omega
            refine ⟨p, 2 ^ 53, hpLo, hpHi, by norm_num, by norm_num,
              Or.inl (by omega), hrawEq, hXloCast, hcap, hXbelow, ?_, ?_, ?_⟩
            · simpa [hm] using hgrid.1
            · simpa [hm] using hgrid.2
            · simpa [hm] using htie
      · have hmLt : m < 2 ^ 53 := by omega
        have hrawN : raw =
            (e + 1023 - (if m < 2 ^ 52 then 1 else 0)).toNat * (2 ^ 52) + m % (2 ^ 52) := by
          dsimp only [raw]
          rw [if_neg hextop]
          by_cases hc : 2 ^ 53 <= m
          · exact False.elim (hcarrym hc)
          · rw [if_neg hc]
        by_cases htopFinite : 1023 <= e
        · have he : e = 1023 := by omega
          have hnormalValue := hnormal (by omega)
          have hnormalReal : (2 ^ 52 : Real) <= t := by exact_mod_cast hnormalValue
          have hmlo := hmLower (by omega)
          have htStrict : t < (2 ^ 53 : Real) - 1 / 2 := by
            by_contra hn
            have htLarge : (2 ^ 53 : Real) - 1 / 2 <= t := by linarith
            have hmTop : m + 1 <= 2 ^ 53 := by omega
            have hmTopR : (m : Real) + 1 <= (2 ^ 53 : Real) := by exact_mod_cast hmTop
            have hmHalf : (m : Real) + 1 / 2 <= (2 ^ 53 : Real) - 1 / 2 := by linarith
            have hmiddleLower : (m : Real) + 1 / 2 <= t := hmHalf.trans htLarge
            have htEq : t = (m : Real) + 1 / 2 := le_antisymm htupper hmiddleLower
            have hmid : (2 ^ 53 : Real) - 1 / 2 <= (m : Real) + 1 / 2 := by
              calc
                (2 ^ 53 : Real) - 1 / 2 <= t := htLarge
                _ = (m : Real) + 1 / 2 := htEq
            have hmidEq : (m : Real) + 1 / 2 = (2 ^ 53 : Real) - 1 / 2 :=
              le_antisymm hmHalf hmid
            have hmEqM : (m : Real) = (2 ^ 53 : Real) - 1 :=
              hhalfShift (m : Real) (2 ^ 53 : Real) hmidEq
            have hmEqR : (m : Real) + 1 = (2 ^ 53 : Real) :=
              hminusOneSucc (m : Real) (2 ^ 53 : Real) hmEqM
            have hmEq : m + 1 = 2 ^ 53 := by exact_mod_cast hmEqR
            have hmSuccDouble : m + 1 = 2 * 2 ^ 52 := hmEq.trans hdouble53
            have hmOdd := hsuccEvenOdd hmSuccDouble
            have heven := htie (Or.inl htEq)
            omega
          have hseq : (2 : Real) ^ (e - 52) = (2 : Real) ^ (971 : Nat) := by
            rw [he]
            norm_num
            rfl
          have hdelta : 0 < (2 : Real) ^ (971 : Nat) := by positivity
          have hgrid := hroundGrid ((2 : Real) ^ (971 : Nat)) hdelta hseq
          have hcap := hcapGrid ((2 : Real) ^ (971 : Nat)) hdelta hseq
          have htie := htieGrid ((2 : Real) ^ (971 : Nat)) hdelta hseq
          have hrawEq : raw = 2046 * (2 ^ 52) + (m - 2 ^ 52) := by
            rw [hrawN, he]
            have hrem : m % (2 ^ 52) = m - 2 ^ 52 := by
              have hbound : m < 2 * 2 ^ 52 := by
                calc
                  m < 2 ^ 53 := hmLt
                  _ = 2 * 2 ^ 52 := hdouble53
              have hsum : m < 2 ^ 52 + 2 ^ 52 := by
                calc
                  m < 2 * 2 ^ 52 := hbound
                  _ = 2 ^ 52 + 2 ^ 52 := Nat.two_mul _
              have hsub : m - 2 ^ 52 < 2 ^ 52 := by
                have := Nat.sub_lt_sub_right hmlo hsum
                simpa [Nat.add_sub_cancel_left] using this
              have hmod : (m - 2 ^ 52) % (2 ^ 52) = m - 2 ^ 52 :=
                Nat.mod_eq_of_lt hsub
              calc
                m % (2 ^ 52) = (2 ^ 52 + (m - 2 ^ 52)) % (2 ^ 52) := by
                  rw [Nat.add_sub_cancel' hmlo]
                _ = (m - 2 ^ 52) % (2 ^ 52) := by
                  rw [Nat.add_mod, Nat.mod_self, Nat.zero_add, Nat.mod_mod]
                _ = m - 2 ^ 52 := hmod
            simp only [if_neg (show ¬m < 2 ^ 52 by omega), hrem]
            have hcoeff : (1023 + 1023 - (0 : Int)).toNat = 2046 := by
              have hc : (((1023 + 1023 - (0 : Int)).toNat : Nat) : Int) = 2046 := by
                rw [Int.toNat_of_nonneg (by norm_num : (0 : Int) <= 1023 + 1023 - 0)]
                norm_num
              exact_mod_cast hc
            rw [hcoeff]
          have hUle : (2 ^ 52 : Real) <= t := by exact_mod_cast hnormalValue
          have hXlo : (2 ^ 52 : Real) * (2 : Real) ^ (971 : Nat) <= X := by
            have hmul := mul_le_mul_of_nonneg_right hUle (le_of_lt hdelta)
            calc
              (2 ^ 52 : Real) * (2 : Real) ^ (971 : Nat) <= t * (2 : Real) ^ (971 : Nat) := hmul
              _ = X := by rw [hXscale, hseq]
          have hXloCast : (2 ^ 52 : Nat) * (2 : Real) ^ (971 : Nat) <= X := by
            rw [hpow52cast]
            exact hXlo
          have hXbelow : X < (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) := by
            have hmul := mul_lt_mul_of_pos_right htStrict hdelta
            calc
              X = t * (2 : Real) ^ (e - 52) := hXscale
              _ < ((2 : Real) ^ (53 : Nat) - 1 / 2) * (2 : Real) ^ (971 : Nat) := by
                rw [hseq]
                exact hmul
              _ = (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) := htopRelation
          right
          right
          refine ⟨2046, m, by norm_num, by norm_num, hmlo, hmLt.le,
            Or.inr hmLt, hrawEq, hXloCast, hcap, hXbelow, ?_, ?_, ?_⟩
          · simpa using hgrid.1
          · simpa using hgrid.2
          · exact htie
        · have hfinite : e <= 1022 := Int.lt_add_one_iff.mp (lt_of_not_ge htopFinite)
          by_cases helow : e = -1022
          · have hseq : (2 : Real) ^ (e - 52) = (2 : Real) ^ (-1074 : Int) := by
              rw [helow]
              norm_num
            have hdelta : 0 < (2 : Real) ^ (-1074 : Int) := zpow_pos (by norm_num) _
            have hgrid := hroundGrid ((2 : Real) ^ (-1074 : Int)) hdelta hseq
            have hcap := hcapGrid ((2 : Real) ^ (-1074 : Int)) hdelta hseq
            have htie := htieGrid ((2 : Real) ^ (-1074 : Int)) hdelta hseq
            have hrawEq : raw = m := by
              rw [hrawN, helow]
              by_cases hmu : m < 2 ^ 52
              · simp only [if_pos hmu, Nat.mod_eq_of_lt hmu]
                norm_num
              · have hmlo : 2 ^ 52 <= m := le_of_not_gt hmu
                let U : Nat := 2 ^ 52
                set_option maxHeartbeats 1000000 in
                  have hrem : m % U = m - U := by
                    have hbound : m < 2 * U := by
                      calc
                        m < 2 ^ 53 := hmLt
                        _ = 2 * 2 ^ 52 := hdouble53
                        _ = 2 * U := by rfl
                    have hsum : m < U + U := by
                      calc
                        m < 2 * U := hbound
                        _ = U + U := Nat.two_mul U
                    have hmloU : U <= m := by exact hmlo
                    have hsub : m - U < U := (Nat.sub_lt_iff_lt_add hmloU).2 hsum
                    have hmod : (m - U) % U = m - U := hmodOfLt hsub
                    calc
                      m % U = (U + (m - U)) % U := by rw [Nat.add_sub_cancel' hmloU]
                      _ = (m - U) % U := by
                        rw [Nat.add_mod, Nat.mod_self, Nat.zero_add, Nat.mod_mod]
                      _ = m - U := hmod
                have hremConcrete : m % (2 ^ 52) = m - 2 ^ 52 := by simpa [U] using hrem
                simp only [if_neg hmu, hremConcrete]
                exact Nat.add_sub_cancel' hmlo
            have hhalf : (2 : Real) ^ (-1074 : Int) / 2 =
                (2 : Real) ^ (-1075 : Int) := by
              rw [div_eq_mul_inv, ← zpow_neg_one,
                ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
              norm_num
            have hgridUp : X <= (m : Real) * (2 : Real) ^ (-1074 : Int) +
                (2 : Real) ^ (-1075 : Int) := by
              rw [← hhalf]
              exact hgrid.1
            have hgridLo : (m : Real) * (2 : Real) ^ (-1074 : Int) <=
                X + (2 : Real) ^ (-1075 : Int) := by
              rw [← hhalf]
              exact hgrid.2
            right
            left
            refine ⟨m, hmLt.le, hrawEq, hcap, ?_, ?_, ?_⟩
            · exact hgridUp
            · exact hgridLo
            · exact htie
          · have heNorm : (-1022 : Int) < e :=
              lt_of_le_of_ne (a := (-1022 : Int)) (b := e) hexpLo (Ne.symm helow)
            have hmlo := hmLower heNorm
            have hpval := hpvalGlobal heNorm
            have hseq : (2 : Real) ^ (e - 52) =
                (2 : Real) ^ ((p : Int) - 1075) := by
              rw [hpval]
              have hexp : e - 52 = (e + 1023) - 1075 := by omega
              exact congrArg (fun k : Int => (2 : Real) ^ k) hexp
            set_option maxHeartbeats 1000000 in
              have hdelta : 0 < (2 : Real) ^ ((p : Int) - 1075) :=
                zpow_pos (by norm_num : (0 : Real) < 2) ((p : Int) - 1075)
            have hgrid := hroundGrid ((2 : Real) ^ ((p : Int) - 1075)) hdelta hseq
            have hcap := hcapGrid ((2 : Real) ^ ((p : Int) - 1075)) hdelta hseq
            have htie := htieGrid ((2 : Real) ^ ((p : Int) - 1075)) hdelta hseq
            set_option maxHeartbeats 1000000 in
              have hrem : m % (2 ^ 52) = m - 2 ^ 52 := by
                let U : Nat := 2 ^ 52
                have hbound : m < 2 * U := by
                  calc
                    m < 2 ^ 53 := hmLt
                    _ = 2 * 2 ^ 52 := hdouble53
                    _ = 2 * U := by rfl
                have hsum : m < U + U := by
                  calc
                    m < 2 * U := hbound
                    _ = U + U := Nat.two_mul U
                have hmloU : U <= m := by exact hmlo
                have hsub : m - U < U := (Nat.sub_lt_iff_lt_add hmloU).2 hsum
                have hmod : (m - U) % U = m - U := hmodOfLt hsub
                calc
                  m % (2 ^ 52) = m % U := by rfl
                  _ = (U + (m - U)) % U := by rw [Nat.add_sub_cancel' hmloU]
                  _ = (m - U) % U := by
                    rw [Nat.add_mod, Nat.mod_self, Nat.zero_add, Nat.mod_mod]
                  _ = m - U := hmod
            have hrawEq : raw = p * (2 ^ 52) + (m - 2 ^ 52) := by
              rw [hrawN, if_neg (show ¬m < 2 ^ 52 by omega), hrem]
              simp [p, Int.sub_zero]
            have hnormval := hnormal heNorm
            have hnormreal : (2 ^ 52 : Real) <= t := by exact_mod_cast hnormval
            have hXlo : (2 ^ 52 : Real) * (2 : Real) ^ ((p : Int) - 1075) <= X := by
              have hmul := mul_le_mul_of_nonneg_right hnormreal (le_of_lt hdelta)
              calc
                (2 ^ 52 : Real) * (2 : Real) ^ ((p : Int) - 1075) <=
                    t * (2 : Real) ^ ((p : Int) - 1075) := hmul
                _ = X := by rw [hXscale, hseq]
            have hXloCast : (2 ^ 52 : Nat) * (2 : Real) ^ ((p : Int) - 1075) <= X := by
              rw [hpow52cast]
              exact hXlo
            have hXbelow := hfiniteBelow hfinite
            have hpLo : 2 <= p := by
              have hi : (2 : Int) <= (p : Int) := by rw [hpval]; omega
              omega
            have hpHi : p <= 2046 := by
              have hi : (p : Int) <= 2045 := by rw [hpval]; omega
              omega
            right
            right
            refine ⟨p, m, hpLo, hpHi, hmlo, hmLt.le, Or.inl (by omega),
              hrawEq, hXloCast, hcap, hXbelow, ?_, ?_, ?_⟩
            · exact hgrid.1
            · exact hgrid.2
            · exact htie
  )

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalGoalVocabularyDefault
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model

-- [TABLET NODE: P529_T1_EncodingArithmetic]
theorem P529_T1_EncodingArithmetic (X : Real) (e : Int) (m m4 : Nat)
    (hexpLo : -1022 <= e) (hexpHi : e <= 1260) :
    let t := X / (2 : Real) ^ (e - 52)
    0 < t -> t < (2 : Real) ^ (53 : Nat) ->
    (-1022 < e -> (2 ^ 52 : Nat) <= t) ->
    t <= (m : Real) + 1 / 2 -> (m : Real) <= t + 1 / 2 ->
    (t = (m : Real) + 1 / 2 \/ t = (m : Real) - 1 / 2 -> m % 2 = 0) ->
    (2 ^ 53 <= m ->
      t / 2 <= (m4 : Real) + 1 / 2 /\ (m4 : Real) <= t / 2 + 1 / 2 /\
      (t / 2 = (m4 : Real) + 1 / 2 \/ t / 2 = (m4 : Real) - 1 / 2 ->
        m4 % 2 = 0)) ->
    let raw : Nat :=
      if 1024 <= e then 2047 * (2 ^ 52)
      else if 2 ^ 53 <= m then
        if 1023 <= e then 2047 * (2 ^ 52)
        else (e + 1024 - (if m4 < 2 ^ 52 then 1 else 0)).toNat * (2 ^ 52) +
          m4 % (2 ^ 52)
      else (e + 1023 - (if m < 2 ^ 52 then 1 else 0)).toNat * (2 ^ 52) +
        m % (2 ^ 52)
    0 <= X /\ ((raw = 2047 * (2 ^ 52) /\
        (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) <= X) \/
      (exists j : Nat,
        j <= 2 ^ 53 /\ raw = j /\
        X <= (2 ^ 53 : Nat) * (2 : Real) ^ (-1074 : Int) /\
        X <= (j : Real) * (2 : Real) ^ (-1074 : Int) + (2 : Real) ^ (-1075 : Int) /\
        (j : Real) * (2 : Real) ^ (-1074 : Int) <= X + (2 : Real) ^ (-1075 : Int) /\
        (X = ((j : Real) + 1 / 2) * (2 : Real) ^ (-1074 : Int) \/
          X = ((j : Real) - 1 / 2) * (2 : Real) ^ (-1074 : Int) -> j % 2 = 0)) \/
      (exists p j : Nat,
        2 <= p /\ p <= 2046 /\ 2 ^ 52 <= j /\ j <= 2 ^ 53 /\
        (p < 2046 \/ j < 2 ^ 53) /\
        raw = p * (2 ^ 52) + (j - 2 ^ 52) /\
        let delta : Real := (2 : Real) ^ ((p : Int) - 1075)
        (2 ^ 52 : Nat) * delta <= X /\ X <= (2 ^ 53 : Nat) * delta /\
        X < (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) /\
        X <= (j : Real) * delta + delta / 2 /\
        (j : Real) * delta <= X + delta / 2 /\
        (X = ((j : Real) + 1 / 2) * delta \/
          X = ((j : Real) - 1 / 2) * delta -> j % 2 = 0))) := by
-- BODY
  set_option maxHeartbeats 2000000 in
  exact (by
    intro t htpos htT hnormal htupper hmlower htie hcarry
    intro raw
    change 0 < t at htpos
    change t < (2 : Real) ^ (53 : Nat) at htT
    change (-1022 < e -> (2 ^ 52 : Nat) <= t) at hnormal
    change t <= (m : Real) + 1 / 2 at htupper
    change (m : Real) <= t + 1 / 2 at hmlower
    change (t = (m : Real) + 1 / 2 \/ t = (m : Real) - 1 / 2 -> m % 2 = 0) at htie
    change (2 ^ 53 <= m ->
      t / 2 <= (m4 : Real) + 1 / 2 /\ (m4 : Real) <= t / 2 + 1 / 2 /\
      (t / 2 = (m4 : Real) + 1 / 2 \/ t / 2 = (m4 : Real) - 1 / 2 -> m4 % 2 = 0)) at hcarry
    have hs : 0 < (2 : Real) ^ (e - 52) := zpow_pos (by norm_num) _
    have hXscale : X = t * (2 : Real) ^ (e - 52) := by
      dsimp [t]
      exact (div_mul_cancel₀ X (ne_of_gt hs)).symm
    have hXpos : 0 < X := by rw [hXscale]; exact mul_pos htpos hs
    have hXnonneg : 0 <= X := le_of_lt hXpos
    let p : Nat := (e + 1023).toNat
    have hpvalGlobal : (-1022 < e) -> (p : Int) = e + 1023 := by
      intro heN
      dsimp [p]
      have hle : (1 : Int) <= e + 1023 := by
        have := add_le_add_right (le_of_lt heN) (1023 : Int)
        simpa [add_comm] using this
      exact Int.toNat_of_nonneg (le_trans (by norm_num) hle)
    have hmupper : m <= 2 ^ 53 := by
      have hmreal : (m : Real) < (2 : Real) ^ (53 : Nat) + 1 := by linarith
      have hn : m < 2 ^ 53 + 1 := by exact_mod_cast hmreal
      norm_num at hn
      omega
    have hmodOfLt {a b : Nat} (hab : a < b) : a % b = a := by
      exact Nat.mod_eq_of_lt hab
    have hsuccEvenOdd {a b : Nat} (hab : a + 1 = 2 * b) : a % 2 = 1 := by
      omega
    have hhalfShift (a b : Real) (h : a + 1 / 2 = b - 1 / 2) : a = b - 1 := by
      linarith
    have hminusOneSucc (a b : Real) (h : a = b - 1) : a + 1 = b := by
      linarith
    have hdouble53 : 2 ^ 53 = 2 * 2 ^ 52 := by
      calc
        2 ^ 53 = 2 ^ (52 + 1) := by norm_num
        _ = 2 ^ 52 * 2 := by rw [Nat.pow_succ]
        _ = 2 * 2 ^ 52 := Nat.mul_comm _ _
    have hpow52cast : (2 ^ 52 : Nat) = (2 : Real) ^ (52 : Nat) := by
      norm_num [Nat.cast_pow]
    have hscalePow : (2 ^ 52 : Nat) * (2 : Real) ^ (e - 52) = (2 : Real) ^ (e : Int) := by
      rw [hpow52cast, ← zpow_natCast (2 : Real) 52, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
      congr 1
      omega
    have htopH : (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) <=
        (2 : Real) ^ (1024 : Int) := by
      have heq : (2 : Real) ^ (1024 : Int) = (2 : Real) ^ (1024 : Nat) := by
        norm_num [zpow_natCast]
        rfl
      rw [heq]
      exact sub_le_self _ (by positivity)
    have hroundGrid (s : Real) (hsp : 0 < s)
        (hseq : (2 : Real) ^ (e - 52) = s) :
        X <= (m : Real) * s + s / 2 /\ (m : Real) * s <= X + s / 2 := by
      have hu := mul_le_mul_of_nonneg_right htupper (le_of_lt hsp)
      have hl := mul_le_mul_of_nonneg_right hmlower (le_of_lt hsp)
      rw [hXscale, hseq]
      constructor <;> nlinarith [hu, hl]
    have hcapGrid (s : Real) (hsp : 0 < s)
        (hseq : (2 : Real) ^ (e - 52) = s) :
        X <= (2 ^ 53 : Nat) * s := by
      have hc : t * s < (2 ^ 53 : Real) * s := mul_lt_mul_of_pos_right htT hsp
      rw [hXscale, hseq]
      have hcast : (2 ^ 53 : Nat) = (2 : Real) ^ (53 : Nat) := by norm_num [Nat.cast_pow]
      rw [hcast]
      exact hc.le
    have htieGrid (s : Real) (hsp : 0 < s)
        (hseq : (2 : Real) ^ (e - 52) = s) :
        (X = ((m : Real) + 1 / 2) * s \/ X = ((m : Real) - 1 / 2) * s) -> m % 2 = 0 := by
      intro hh
      rcases hh with hu | hl
      · have ht' : t = (m : Real) + 1 / 2 := by
          apply mul_left_cancel₀ (ne_of_gt hsp)
          calc
            s * t = X := by rw [hXscale, hseq]; ring
            _ = ((m : Real) + 1 / 2) * s := hu
            _ = s * ((m : Real) + 1 / 2) := by ring
        exact htie (Or.inl ht')
      · have ht' : t = (m : Real) - 1 / 2 := by
          apply mul_left_cancel₀ (ne_of_gt hsp)
          calc
            s * t = X := by rw [hXscale, hseq]; ring
            _ = ((m : Real) - 1 / 2) * s := hl
            _ = s * ((m : Real) - 1 / 2) := by ring
        exact htie (Or.inr ht')
    have hmLower (hN : -1022 < e) : 2 ^ 52 <= m := by
      have htn := hnormal hN
      have htnReal : (2 ^ 52 : Nat) <= t := by exact_mod_cast htn
      have hmReal : ((2 ^ 52 : Nat) : Real) - 1 / 2 <= (m : Real) := by
        have hmBound : (m : Real) + 1 / 2 >= t := by linarith [htupper]
        linarith
      by_contra h
      have hn : m + 1 <= 2 ^ 52 := by omega
      have hr : (m : Real) + 1 <= (2 ^ 52 : Real) := by exact_mod_cast hn
      linarith
    have hthreshold : (2 : Real) ^ (1023 : Int) <
        (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) := by
      have hsmall : (2 : Real) ^ (970 : Nat) < (2 : Real) ^ (1023 : Nat) := by
        have hs : (2 : Real) ^ (970 : Int) < (2 : Real) ^ (1023 : Int) :=
          zpow_lt_zpow_right₀ (by norm_num : (1 : Real) < 2) (by norm_num : (970 : Int) < 1023)
        simpa only [zpow_natCast] using hs
      have hpow : (2 : Real) ^ (1024 : Nat) =
          2 * (2 : Real) ^ (1023 : Nat) := by
        rw [show (1024 : Nat) = 1023 + 1 by norm_num, pow_succ]
        ring
      have hcast : (2 : Real) ^ (1023 : Int) = (2 : Real) ^ (1023 : Nat) := by
        norm_num [zpow_natCast]
        rfl
      rw [hcast, hpow]
      have hgap : 0 < (2 : Real) ^ (1023 : Nat) - (2 : Real) ^ (970 : Nat) :=
        sub_pos.mpr hsmall
      calc
        (2 : Real) ^ (1023 : Nat) <
            (2 : Real) ^ (1023 : Nat) +
              ((2 : Real) ^ (1023 : Nat) - (2 : Real) ^ (970 : Nat)) :=
          lt_add_of_pos_right _ hgap
        _ = 2 * (2 : Real) ^ (1023 : Nat) - (2 : Real) ^ (970 : Nat) := by ring
    have htopRelation : ((2 : Real) ^ (53 : Nat) - 1 / 2) *
        (2 : Real) ^ (971 : Nat) =
        (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) := by
      have htop : (2 : Real) ^ (53 : Nat) * (2 : Real) ^ (971 : Nat) =
          (2 : Real) ^ (1024 : Nat) := by
        rw [← zpow_natCast (2 : Real) 53, ← zpow_natCast (2 : Real) 971,
          ← zpow_natCast (2 : Real) 1024, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
        congr 1
      have hhalf : (1 / 2 : Real) * (2 : Real) ^ (971 : Nat) =
          (2 : Real) ^ (970 : Nat) := by
        rw [show (1 / 2 : Real) = (2 : Real) ^ (-1 : Int) by norm_num [zpow_neg]]
        rw [← zpow_natCast (2 : Real) 971, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
        norm_num [zpow_natCast]
        rfl
      calc
        ((2 : Real) ^ (53 : Nat) - 1 / 2) * (2 : Real) ^ (971 : Nat) =
            (2 : Real) ^ (53 : Nat) * (2 : Real) ^ (971 : Nat) -
              (1 / 2 : Real) * (2 : Real) ^ (971 : Nat) := by ring
        _ = (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) := by rw [htop, hhalf]
    have hfiniteBelow (he : e <= 1022) :
        X < (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) := by
      have hTcast : (2 ^ 53 : Nat) = (2 : Real) ^ (53 : Nat) := by norm_num [Nat.cast_pow]
      have hTscale : (2 ^ 53 : Nat) * (2 : Real) ^ (e - 52) =
          (2 : Real) ^ (e + 1) := by
        rw [hTcast]
        calc
          (2 : Real) ^ (53 : Nat) * (2 : Real) ^ (e - 52) =
              2 * ((2 ^ 52 : Nat) * (2 : Real) ^ (e - 52)) := by
                have hp : (2 : Real) ^ (53 : Nat) = 2 * (2 : Real) ^ (52 : Nat) := by
                  rw [show (53 : Nat) = 52 + 1 by norm_num, pow_succ]
                  ring
                rw [hp, hpow52cast]
                ring
          _ = 2 * (2 : Real) ^ (e : Int) := by rw [hscalePow]
          _ = (2 : Real) ^ (e + 1) := by
            rw [zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
            ring
      have htBound : X < (2 ^ 53 : Nat) * (2 : Real) ^ (e - 52) := by
        have hc := mul_lt_mul_of_pos_right htT hs
        calc
          X = t * (2 : Real) ^ (e - 52) := hXscale
          _ < (2 : Real) ^ (53 : Nat) * (2 : Real) ^ (e - 52) := hc
          _ = (2 ^ 53 : Nat) * (2 : Real) ^ (e - 52) := by rw [hTcast]
      have hp : (2 : Real) ^ (e + 1) <= (2 : Real) ^ (1023 : Int) :=
        zpow_le_zpow_right₀ (by norm_num : (1 : Real) <= 2) (show e + 1 <= 1023 by omega)
      calc
        X < (2 ^ 53 : Nat) * (2 : Real) ^ (e - 52) := htBound
        _ = (2 : Real) ^ (e + 1) := hTscale
        _ <= (2 : Real) ^ (1023 : Int) := hp
        _ < (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) := hthreshold
    refine ⟨hXnonneg, ?_⟩
    by_cases hextop : 1024 <= e
    · left
      constructor
      · simp [raw, hextop]
      · have hnormval := hnormal (by omega)
        have hnormreal : (2 ^ 52 : Real) <= t := by exact_mod_cast hnormval
        have hpowMono : (2 : Real) ^ (1024 : Int) <= (2 : Real) ^ (e : Int) :=
          zpow_le_zpow_right₀ (by norm_num : (1 : Real) <= 2) (by omega)
        have hmulse : (2 ^ 52 : Real) * (2 : Real) ^ (e - 52) <= X := by
          rw [hXscale]
          exact mul_le_mul_of_nonneg_right hnormreal (le_of_lt hs)
        have hepow : (2 : Real) ^ (e : Int) <=
            (2 ^ 52 : Real) * (2 : Real) ^ (e - 52) := by
          rw [hpow52cast] at hscalePow
          exact le_of_eq hscalePow.symm
        exact le_trans (le_trans (le_trans htopH hpowMono) hepow) hmulse
    · by_cases hcarrym : 2 ^ 53 <= m
      · have hm : m = 2 ^ 53 := Nat.le_antisymm hmupper hcarrym
        have hm4 : m4 = 2 ^ 52 := by
          have hc := hcarry hcarrym
          rcases hc with ⟨h4lo, h4hi, _⟩
          have h4upper : m4 <= 2 ^ 52 := by
            have ht2 : t / 2 < (2 : Real) ^ (52 : Nat) := by nlinarith [htT]
            by_contra hn
            have hn' : 2 ^ 52 + 1 <= m4 := by omega
            have hr : (2 ^ 52 : Real) + 1 <= (m4 : Real) := by exact_mod_cast hn'
            nlinarith
          have h4lower : 2 ^ 52 <= m4 := by
            have htlo : (2 : Real) ^ (52 : Nat) - 1 / 4 <= t / 2 := by
              rw [hm] at hmlower
              push_cast at hmlower
              nlinarith
            by_contra hn
            have hn' : m4 + 1 <= 2 ^ 52 := by omega
            have hr : (m4 : Real) + 1 <= (2 ^ 52 : Real) := by exact_mod_cast hn'
            nlinarith [h4lo]
          exact Nat.le_antisymm h4upper h4lower
        by_cases hecarryTop : 1023 <= e
        · have he : e = 1023 := by omega
          subst e
          have htlo : (2 ^ 53 : Real) - 1 / 2 <= t := by
            rw [hm] at hmlower
            push_cast at hmlower
            linarith
          have hXL : (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) <= X := by
            have hrel : ((2 : Real) ^ (53 : Nat) - 1 / 2) * (2 : Real) ^ (971 : Nat) =
                (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) := by
              have htop : (2 : Real) ^ (53 : Nat) * (2 : Real) ^ (971 : Nat) =
                  (2 : Real) ^ (1024 : Nat) := by
                rw [← zpow_natCast (2 : Real) 53, ← zpow_natCast (2 : Real) 971,
                  ← zpow_natCast (2 : Real) 1024, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
                congr 1
              have hhalf : (1 / 2 : Real) * (2 : Real) ^ (971 : Nat) =
                  (2 : Real) ^ (970 : Nat) := by
                rw [show (1 / 2 : Real) = (2 : Real) ^ (-1 : Int) by norm_num [zpow_neg]]
                rw [← zpow_natCast (2 : Real) 971, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
                norm_num [zpow_natCast]
                rfl
              calc
                ((2 : Real) ^ (53 : Nat) - 1 / 2) * (2 : Real) ^ (971 : Nat) =
                    (2 : Real) ^ (53 : Nat) * (2 : Real) ^ (971 : Nat) -
                      (1 / 2 : Real) * (2 : Real) ^ (971 : Nat) := by ring
                _ = (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) := by rw [htop, hhalf]
            have hmul := mul_le_mul_of_nonneg_right htlo
              (le_of_lt (by positivity : 0 < (2 : Real) ^ (971 : Nat)))
            calc
              (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) =
                  ((2 : Real) ^ (53 : Nat) - 1 / 2) * (2 : Real) ^ (971 : Nat) := hrel.symm
              _ <= t * (2 : Real) ^ (971 : Nat) := hmul
              _ = X := by
                rw [hXscale]
                rw [show (1023 : Int) - 52 = 971 by norm_num]
                rfl
          left
          constructor
          · norm_num [raw, hextop, hcarrym, hm, hm4]
          · exact hXL
        · have hecarryFin : e <= 1022 := by omega
          have hrawC : raw = (e + 1024).toNat * (2 ^ 52) := by
            simp [raw, hextop, hm, hm4, show ¬1023 <= e by omega]
          by_cases helow : e = -1022
          · have hseq : (2 : Real) ^ (e - 52) = (2 : Real) ^ (-1074 : Int) := by
              rw [helow]
              norm_num
            have hdelta : 0 < (2 : Real) ^ (-1074 : Int) := zpow_pos (by norm_num) _
            have hgrid := hroundGrid ((2 : Real) ^ (-1074 : Int)) hdelta hseq
            have hcap := hcapGrid ((2 : Real) ^ (-1074 : Int)) hdelta hseq
            have htie := htieGrid ((2 : Real) ^ (-1074 : Int)) hdelta hseq
            have hrawEq : raw = 2 ^ 53 := by
              rw [hrawC]
              have hc : (e + 1024).toNat = 2 := by rw [helow]; norm_num; rfl
              rw [hc]
              norm_num
            have hhalf : (2 : Real) ^ (-1074 : Int) / 2 =
                (2 : Real) ^ (-1075 : Int) := by
              rw [div_eq_mul_inv,
                show (2 : Real)⁻¹ = (2 : Real) ^ (-1 : Int) by norm_num [zpow_neg],
                ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
              norm_num
            have hhalfInv : (2 ^ 1074 : Real)⁻¹ / 2 = (2 ^ 1075 : Real)⁻¹ := by
              simpa [zpow_neg] using hhalf
            have hgridUp : X <= (m : Real) * (2 : Real) ^ (-1074 : Int) +
                (2 ^ 1075 : Real)⁻¹ := by
              have h' : X <= (m : Real) * (2 : Real) ^ (-1074 : Int) +
                  (2 ^ 1074 : Real)⁻¹ / 2 := by simpa [zpow_neg] using hgrid.1
              rw [← hhalfInv]
              exact h'
            have hgridLo : (m : Real) * (2 : Real) ^ (-1074 : Int) <=
                X + (2 ^ 1075 : Real)⁻¹ := by
              have h' : (m : Real) * (2 : Real) ^ (-1074 : Int) <= X +
                  (2 ^ 1074 : Real)⁻¹ / 2 := by simpa [zpow_neg] using hgrid.2
              rw [← hhalfInv]
              exact h'
            right
            left
            refine ⟨2 ^ 53, by norm_num, ?_⟩
            refine ⟨hrawEq, ?_, ?_, ?_, ?_⟩
            · exact hcap
            · simpa [hm, zpow_neg] using hgridUp
            · simpa [hm, zpow_neg] using hgridLo
            · simpa [hm] using htie
          · have heNorm : (-1022 : Int) < e := by
              set_option maxHeartbeats 1000000 in
                exact lt_of_le_of_ne (a := (-1022 : Int)) (b := e) hexpLo (Ne.symm helow)
            let p : Nat := (e + 1023).toNat
            have hpval : (p : Int) = e + 1023 := by
              dsimp [p]
              omega
            have hcoeff : (e + 1024).toNat = p + 1 := by
              dsimp [p]
              omega
            have hseq : (2 : Real) ^ (e - 52) =
                (2 : Real) ^ ((p : Int) - 1075) := by
              rw [hpval]
              have hexp : e - 52 = (e + 1023) - 1075 := by omega
              exact congrArg (fun k : Int => (2 : Real) ^ k) hexp
            set_option maxHeartbeats 1000000 in
              have hdelta : 0 < (2 : Real) ^ ((p : Int) - 1075) := by
                rw [← hseq]
                exact hs
            have hgrid := hroundGrid ((2 : Real) ^ ((p : Int) - 1075)) hdelta hseq
            have hcap := hcapGrid ((2 : Real) ^ ((p : Int) - 1075)) hdelta hseq
            have htie := htieGrid ((2 : Real) ^ ((p : Int) - 1075)) hdelta hseq
            have hrawEq : raw = p * (2 ^ 52) + (2 ^ 53 - 2 ^ 52) := by
              calc
                raw = (e + 1024).toNat * (2 ^ 52) := hrawC
                _ = p * (2 ^ 52) + (2 ^ 53 - 2 ^ 52) := by
                  rw [hcoeff]
                  norm_num
                  ring
            have hnormval := hnormal heNorm
            have hnormreal : (2 ^ 52 : Real) <= t := by exact_mod_cast hnormval
            have hXlo : (2 ^ 52 : Real) * (2 : Real) ^ ((p : Int) - 1075) <= X := by
              have hmul := mul_le_mul_of_nonneg_right hnormreal (le_of_lt hdelta)
              calc
                (2 ^ 52 : Real) * (2 : Real) ^ ((p : Int) - 1075) <=
                    t * (2 : Real) ^ ((p : Int) - 1075) := hmul
                _ = X := by rw [hXscale, hseq]
            have hXbelow := hfiniteBelow hecarryFin
            have hXloCast : (2 ^ 52 : Nat) * (2 : Real) ^ ((p : Int) - 1075) <= X := by
              rw [hpow52cast]
              exact hXlo
            right
            right
            have hpLo : 2 <= p := by
              have hi : (2 : Int) <= (p : Int) := by rw [hpval]; omega
              omega
            have hpHi : p <= 2046 := by
              have hi : (p : Int) <= 2045 := by rw [hpval]; omega
              omega
            refine ⟨p, 2 ^ 53, hpLo, hpHi, by norm_num, by norm_num,
              Or.inl (by omega), hrawEq, hXloCast, hcap, hXbelow, ?_, ?_, ?_⟩
            · simpa [hm] using hgrid.1
            · simpa [hm] using hgrid.2
            · simpa [hm] using htie
      · have hmLt : m < 2 ^ 53 := by omega
        have hrawN : raw =
            (e + 1023 - (if m < 2 ^ 52 then 1 else 0)).toNat * (2 ^ 52) + m % (2 ^ 52) := by
          dsimp only [raw]
          rw [if_neg hextop]
          by_cases hc : 2 ^ 53 <= m
          · exact False.elim (hcarrym hc)
          · rw [if_neg hc]
        by_cases htopFinite : 1023 <= e
        · have he : e = 1023 := by omega
          have hnormalValue := hnormal (by omega)
          have hnormalReal : (2 ^ 52 : Real) <= t := by exact_mod_cast hnormalValue
          have hmlo := hmLower (by omega)
          have htStrict : t < (2 ^ 53 : Real) - 1 / 2 := by
            by_contra hn
            have htLarge : (2 ^ 53 : Real) - 1 / 2 <= t := by linarith
            have hmTop : m + 1 <= 2 ^ 53 := by omega
            have hmTopR : (m : Real) + 1 <= (2 ^ 53 : Real) := by exact_mod_cast hmTop
            have hmHalf : (m : Real) + 1 / 2 <= (2 ^ 53 : Real) - 1 / 2 := by linarith
            have hmiddleLower : (m : Real) + 1 / 2 <= t := hmHalf.trans htLarge
            have htEq : t = (m : Real) + 1 / 2 := le_antisymm htupper hmiddleLower
            have hmid : (2 ^ 53 : Real) - 1 / 2 <= (m : Real) + 1 / 2 := by
              calc
                (2 ^ 53 : Real) - 1 / 2 <= t := htLarge
                _ = (m : Real) + 1 / 2 := htEq
            have hmidEq : (m : Real) + 1 / 2 = (2 ^ 53 : Real) - 1 / 2 :=
              le_antisymm hmHalf hmid
            have hmEqM : (m : Real) = (2 ^ 53 : Real) - 1 :=
              hhalfShift (m : Real) (2 ^ 53 : Real) hmidEq
            have hmEqR : (m : Real) + 1 = (2 ^ 53 : Real) :=
              hminusOneSucc (m : Real) (2 ^ 53 : Real) hmEqM
            have hmEq : m + 1 = 2 ^ 53 := by exact_mod_cast hmEqR
            have hmSuccDouble : m + 1 = 2 * 2 ^ 52 := hmEq.trans hdouble53
            have hmOdd := hsuccEvenOdd hmSuccDouble
            have heven := htie (Or.inl htEq)
            omega
          have hseq : (2 : Real) ^ (e - 52) = (2 : Real) ^ (971 : Nat) := by
            rw [he]
            norm_num
            rfl
          have hdelta : 0 < (2 : Real) ^ (971 : Nat) := by positivity
          have hgrid := hroundGrid ((2 : Real) ^ (971 : Nat)) hdelta hseq
          have hcap := hcapGrid ((2 : Real) ^ (971 : Nat)) hdelta hseq
          have htie := htieGrid ((2 : Real) ^ (971 : Nat)) hdelta hseq
          have hrawEq : raw = 2046 * (2 ^ 52) + (m - 2 ^ 52) := by
            rw [hrawN, he]
            have hrem : m % (2 ^ 52) = m - 2 ^ 52 := by
              have hbound : m < 2 * 2 ^ 52 := by
                calc
                  m < 2 ^ 53 := hmLt
                  _ = 2 * 2 ^ 52 := hdouble53
              have hsum : m < 2 ^ 52 + 2 ^ 52 := by
                calc
                  m < 2 * 2 ^ 52 := hbound
                  _ = 2 ^ 52 + 2 ^ 52 := Nat.two_mul _
              have hsub : m - 2 ^ 52 < 2 ^ 52 := by
                have := Nat.sub_lt_sub_right hmlo hsum
                simpa [Nat.add_sub_cancel_left] using this
              have hmod : (m - 2 ^ 52) % (2 ^ 52) = m - 2 ^ 52 :=
                Nat.mod_eq_of_lt hsub
              calc
                m % (2 ^ 52) = (2 ^ 52 + (m - 2 ^ 52)) % (2 ^ 52) := by
                  rw [Nat.add_sub_cancel' hmlo]
                _ = (m - 2 ^ 52) % (2 ^ 52) := by
                  rw [Nat.add_mod, Nat.mod_self, Nat.zero_add, Nat.mod_mod]
                _ = m - 2 ^ 52 := hmod
            simp only [if_neg (show ¬m < 2 ^ 52 by omega), hrem]
            have hcoeff : (1023 + 1023 - (0 : Int)).toNat = 2046 := by
              have hc : (((1023 + 1023 - (0 : Int)).toNat : Nat) : Int) = 2046 := by
                rw [Int.toNat_of_nonneg (by norm_num : (0 : Int) <= 1023 + 1023 - 0)]
                norm_num
              exact_mod_cast hc
            rw [hcoeff]
          have hUle : (2 ^ 52 : Real) <= t := by exact_mod_cast hnormalValue
          have hXlo : (2 ^ 52 : Real) * (2 : Real) ^ (971 : Nat) <= X := by
            have hmul := mul_le_mul_of_nonneg_right hUle (le_of_lt hdelta)
            calc
              (2 ^ 52 : Real) * (2 : Real) ^ (971 : Nat) <= t * (2 : Real) ^ (971 : Nat) := hmul
              _ = X := by rw [hXscale, hseq]
          have hXloCast : (2 ^ 52 : Nat) * (2 : Real) ^ (971 : Nat) <= X := by
            rw [hpow52cast]
            exact hXlo
          have hXbelow : X < (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) := by
            have hmul := mul_lt_mul_of_pos_right htStrict hdelta
            calc
              X = t * (2 : Real) ^ (e - 52) := hXscale
              _ < ((2 : Real) ^ (53 : Nat) - 1 / 2) * (2 : Real) ^ (971 : Nat) := by
                rw [hseq]
                exact hmul
              _ = (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) := htopRelation
          right
          right
          refine ⟨2046, m, by norm_num, by norm_num, hmlo, hmLt.le,
            Or.inr hmLt, hrawEq, hXloCast, hcap, hXbelow, ?_, ?_, ?_⟩
          · simpa using hgrid.1
          · simpa using hgrid.2
          · exact htie
        · have hfinite : e <= 1022 := Int.lt_add_one_iff.mp (lt_of_not_ge htopFinite)
          by_cases helow : e = -1022
          · have hseq : (2 : Real) ^ (e - 52) = (2 : Real) ^ (-1074 : Int) := by
              rw [helow]
              norm_num
            have hdelta : 0 < (2 : Real) ^ (-1074 : Int) := zpow_pos (by norm_num) _
            have hgrid := hroundGrid ((2 : Real) ^ (-1074 : Int)) hdelta hseq
            have hcap := hcapGrid ((2 : Real) ^ (-1074 : Int)) hdelta hseq
            have htie := htieGrid ((2 : Real) ^ (-1074 : Int)) hdelta hseq
            have hrawEq : raw = m := by
              rw [hrawN, helow]
              by_cases hmu : m < 2 ^ 52
              · simp only [if_pos hmu, Nat.mod_eq_of_lt hmu]
                norm_num
              · have hmlo : 2 ^ 52 <= m := le_of_not_gt hmu
                let U : Nat := 2 ^ 52
                set_option maxHeartbeats 1000000 in
                  have hrem : m % U = m - U := by
                    have hbound : m < 2 * U := by
                      calc
                        m < 2 ^ 53 := hmLt
                        _ = 2 * 2 ^ 52 := hdouble53
                        _ = 2 * U := by rfl
                    have hsum : m < U + U := by
                      calc
                        m < 2 * U := hbound
                        _ = U + U := Nat.two_mul U
                    have hmloU : U <= m := by exact hmlo
                    have hsub : m - U < U := (Nat.sub_lt_iff_lt_add hmloU).2 hsum
                    have hmod : (m - U) % U = m - U := hmodOfLt hsub
                    calc
                      m % U = (U + (m - U)) % U := by rw [Nat.add_sub_cancel' hmloU]
                      _ = (m - U) % U := by
                        rw [Nat.add_mod, Nat.mod_self, Nat.zero_add, Nat.mod_mod]
                      _ = m - U := hmod
                have hremConcrete : m % (2 ^ 52) = m - 2 ^ 52 := by simpa [U] using hrem
                simp only [if_neg hmu, hremConcrete]
                exact Nat.add_sub_cancel' hmlo
            have hhalf : (2 : Real) ^ (-1074 : Int) / 2 =
                (2 : Real) ^ (-1075 : Int) := by
              rw [div_eq_mul_inv, ← zpow_neg_one,
                ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
              norm_num
            have hgridUp : X <= (m : Real) * (2 : Real) ^ (-1074 : Int) +
                (2 : Real) ^ (-1075 : Int) := by
              rw [← hhalf]
              exact hgrid.1
            have hgridLo : (m : Real) * (2 : Real) ^ (-1074 : Int) <=
                X + (2 : Real) ^ (-1075 : Int) := by
              rw [← hhalf]
              exact hgrid.2
            right
            left
            refine ⟨m, hmLt.le, hrawEq, hcap, ?_, ?_, ?_⟩
            · exact hgridUp
            · exact hgridLo
            · exact htie
          · have heNorm : (-1022 : Int) < e :=
              lt_of_le_of_ne (a := (-1022 : Int)) (b := e) hexpLo (Ne.symm helow)
            have hmlo := hmLower heNorm
            have hpval := hpvalGlobal heNorm
            have hseq : (2 : Real) ^ (e - 52) =
                (2 : Real) ^ ((p : Int) - 1075) := by
              rw [hpval]
              have hexp : e - 52 = (e + 1023) - 1075 := by omega
              exact congrArg (fun k : Int => (2 : Real) ^ k) hexp
            set_option maxHeartbeats 1000000 in
              have hdelta : 0 < (2 : Real) ^ ((p : Int) - 1075) :=
                zpow_pos (by norm_num : (0 : Real) < 2) ((p : Int) - 1075)
            have hgrid := hroundGrid ((2 : Real) ^ ((p : Int) - 1075)) hdelta hseq
            have hcap := hcapGrid ((2 : Real) ^ ((p : Int) - 1075)) hdelta hseq
            have htie := htieGrid ((2 : Real) ^ ((p : Int) - 1075)) hdelta hseq
            set_option maxHeartbeats 1000000 in
              have hrem : m % (2 ^ 52) = m - 2 ^ 52 := by
                let U : Nat := 2 ^ 52
                have hbound : m < 2 * U := by
                  calc
                    m < 2 ^ 53 := hmLt
                    _ = 2 * 2 ^ 52 := hdouble53
                    _ = 2 * U := by rfl
                have hsum : m < U + U := by
                  calc
                    m < 2 * U := hbound
                    _ = U + U := Nat.two_mul U
                have hmloU : U <= m := by exact hmlo
                have hsub : m - U < U := (Nat.sub_lt_iff_lt_add hmloU).2 hsum
                have hmod : (m - U) % U = m - U := hmodOfLt hsub
                calc
                  m % (2 ^ 52) = m % U := by rfl
                  _ = (U + (m - U)) % U := by rw [Nat.add_sub_cancel' hmloU]
                  _ = (m - U) % U := by
                    rw [Nat.add_mod, Nat.mod_self, Nat.zero_add, Nat.mod_mod]
                  _ = m - U := hmod
            have hrawEq : raw = p * (2 ^ 52) + (m - 2 ^ 52) := by
              rw [hrawN, if_neg (show ¬m < 2 ^ 52 by omega), hrem]
              simp [p, Int.sub_zero]
            have hnormval := hnormal heNorm
            have hnormreal : (2 ^ 52 : Real) <= t := by exact_mod_cast hnormval
            have hXlo : (2 ^ 52 : Real) * (2 : Real) ^ ((p : Int) - 1075) <= X := by
              have hmul := mul_le_mul_of_nonneg_right hnormreal (le_of_lt hdelta)
              calc
                (2 ^ 52 : Real) * (2 : Real) ^ ((p : Int) - 1075) <=
                    t * (2 : Real) ^ ((p : Int) - 1075) := hmul
                _ = X := by rw [hXscale, hseq]
            have hXloCast : (2 ^ 52 : Nat) * (2 : Real) ^ ((p : Int) - 1075) <= X := by
              rw [hpow52cast]
              exact hXlo
            have hXbelow := hfiniteBelow hfinite
            have hpLo : 2 <= p := by
              have hi : (2 : Int) <= (p : Int) := by rw [hpval]; omega
              omega
            have hpHi : p <= 2046 := by
              have hi : (p : Int) <= 2045 := by rw [hpval]; omega
              omega
            right
            right
            refine ⟨p, m, hpLo, hpHi, hmlo, hmLt.le, Or.inl (by omega),
              hrawEq, hXloCast, hcap, hXbelow, ?_, ?_, ?_⟩
            · exact hgrid.1
            · exact hgrid.2
            · exact htie
  )

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
Technical checking records
Local closure summary
{
  "node": "P529_T1_EncodingArithmetic",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "2121557ab9772b773c6026869d8bb30b048442fa5eb42bd6dada1c0073e9b679",
  "active_statement_hash": "d64c54d6261d8816a03a099f117ab671afd4fc175b55d3bc886fc3bec12b0826",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "576e6bf9480cfd32f53c309c7ac8fb115c1ba6e413f299ed5ff9fa7c647e6769",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-168",
  "boundary_theorems": {},
  "strict_theorem_deps": {},
  "strict_definition_deps": {}
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "matches current fingerprint",
  "assessment_fingerprint": "sound-v2:517090fb80f6185dfe7f55b5fe8ff9a2d2b20ce24cd6d889a6f6a7b239196b44",
  "current_fingerprint": "sound-v2:517090fb80f6185dfe7f55b5fe8ff9a2d2b20ce24cd6d889a6f6a7b239196b44",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}