Skip to content

feat: add Dyadic.divAtPrec for round-down dyadic division at given precision#13654

Open
kim-em wants to merge 5 commits into
leanprover:masterfrom
kim-em:issue-13653
Open

feat: add Dyadic.divAtPrec for round-down dyadic division at given precision#13654
kim-em wants to merge 5 commits into
leanprover:masterfrom
kim-em:issue-13653

Conversation

@kim-em
Copy link
Copy Markdown
Collaborator

@kim-em kim-em commented May 6, 2026

This PR adds Dyadic.divAtPrec a b prec, returning the greatest dyadic with precision at most prec which is less than or equal to a/b (and 0 when b = 0). Mirroring the existing invAtPrec, the characterising lemmas divAtPrec_mul_le and lt_divAtPrec_add_inc_mul are also provided.

Compared to composing invAtPrec with multiplication, divAtPrec rounds a/b down uniformly (no sign-dependent rounding flip), states the precision at the output rather than requiring callers to back-solve from ‖a‖, and fuses the bignum division with renormalisation. invAtPrec remains the right primitive when the reciprocal is reused.

Closes #13653.

🤖 Prepared with Claude Code

…ecision

This PR adds `Dyadic.divAtPrec a b prec`, returning the greatest dyadic with
precision at most `prec` which is less than or equal to `a/b` (and `0` when
`b = 0`). Mirroring the existing `invAtPrec`, the characterising lemmas
`divAtPrec_mul_le` and `lt_divAtPrec_add_inc_mul` are also provided.

Compared to composing `invAtPrec` with multiplication, `divAtPrec` rounds
`a/b` down uniformly (no sign-dependent rounding flip), states the precision
at the output rather than requiring callers to back-solve from `‖a‖`, and
fuses the bignum division with renormalisation.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@kim-em kim-em added the changelog-library Library label May 6, 2026
@github-actions github-actions Bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label May 6, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

mathlib-lean-pr-testing Bot commented May 6, 2026

Mathlib CI status (docs):

  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase ea6e76707835317858b7dd36c95322679c50aaac --onto 8ebd294673e9e0397c5ccf2ab9a65b0f3e937918. You can force Mathlib CI using the force-mathlib-ci label. (2026-05-06 07:29:40)
  • ✅ Mathlib branch lean-pr-testing-13654 has successfully built against this PR. (2026-05-07 00:52:57) View Log
  • ✅ Mathlib branch lean-pr-testing-13654 has successfully built against this PR. (2026-05-07 03:14:10) View Log
  • ✅ Mathlib branch lean-pr-testing-13654 has successfully built against this PR. (2026-05-08 14:14:09) View Log

@leanprover-bot
Copy link
Copy Markdown
Collaborator

leanprover-bot commented May 6, 2026

Reference manual CI status:

  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase ea6e76707835317858b7dd36c95322679c50aaac --onto 3fc99eef102549c743c6c63547983319bfae6f01. You can force reference manual CI using the force-manual-ci label. (2026-05-06 07:29:42)
  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2026-05-06 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-manual, reference manual CI should run now. You can force reference manual CI using the force-manual-ci label. (2026-05-06 23:57:56)
  • ✅ Reference manual branch lean-pr-testing-13654 has successfully built against this PR. (2026-05-07 02:24:09) View Log
  • 🟡 Reference manual branch lean-pr-testing-13654 build against this PR didn't complete normally. (2026-05-07 02:25:51) View Log
  • ✅ Reference manual branch lean-pr-testing-13654 has successfully built against this PR. (2026-05-08 13:16:08) View Log
  • 🟡 Reference manual branch lean-pr-testing-13654 build against this PR didn't complete normally. (2026-05-08 13:17:26) View Log
  • ✅ Reference manual branch lean-pr-testing-13654 has successfully built against this PR. (2026-05-11 14:47:12) View Log
  • 🟡 Reference manual branch lean-pr-testing-13654 build against this PR didn't complete normally. (2026-05-11 14:47:56) View Log

Discharges the existing TODOs by adding `eq_invAtPrec` and `eq_divAtPrec`,
factored through a public helper `eq_toDyadic_of_precision_le`: any dyadic
with precision at most `prec` lying in `[y.toRat, y.toRat + 2 ^ (-prec))` is
the floor of `q` at that precision. Adds a (non-public) import of
`Init.Data.Int.Order` for `Int.le_antisymm` and `Int.lt_add_one_iff`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@github-actions github-actions Bot added the mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN label May 6, 2026
@mathlib-lean-pr-testing mathlib-lean-pr-testing Bot added the builds-mathlib CI has verified that Mathlib builds against this PR label May 7, 2026
Hoist the `hxr`/`hbr` bridges from `0 < x` to `0 < x.toRat` to the top
of each proof and inline them at the use sites, replacing the repeated
`have h_pos`, `have h_le`, and inline `Rat.ne_of_gt`/`Rat.le_of_lt`
derivations. Also drops the trailing intermediate `have`s in favour of
direct `calc` steps. No semantic changes; ~38 fewer lines.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request May 7, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request May 7, 2026
@leanprover-bot leanprover-bot added the builds-manual CI has verified that the Lean Language Reference builds against this PR label May 7, 2026
…q_divAtPrec_one`

Add `invAtPrec_eq_divAtPrec_one`, the (theorem-level only) equality
`invAtPrec x prec = divAtPrec 1 x prec`. The definition of `invAtPrec`
is left untouched so its runtime path remains tight. The three
characterising lemmas about `invAtPrec` now follow as one-line
corollaries of their `divAtPrec` counterparts.

Also add two strict bounds relating `a * invAtPrec b prec` to
`divAtPrec a b prec` (which are *not* equal in general): each
isolates `a * invAtPrec b prec` on one side, with the two pinning
the gap to within `2 ^ (-prec)` and `a * 2 ^ (-prec)` respectively.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request May 8, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request May 8, 2026
leanprover-bot added a commit to leanprover/reference-manual that referenced this pull request May 8, 2026
…ic.Inv

The previous commit's docstrings overstated two things. `invAtPrec` is
now only propositionally equal to `divAtPrec 1`, not definitionally;
and the gap between `a * invAtPrec b prec` and `divAtPrec a b prec` is
asymmetric — bounded by `2 ^ (-prec)` on one side and by
`a * 2 ^ (-prec)` on the other, not by `a * 2 ^ (-prec)` in both
directions. Update the prose to match the theorems.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@kim-em kim-em added awaiting-review Waiting for someone to review the PR merge-ci Enable merge queue CI checks for PR. In particular, produce artifacts for all major platforms. and removed awaiting-review Waiting for someone to review the PR merge-ci Enable merge queue CI checks for PR. In particular, produce artifacts for all major platforms. labels May 11, 2026
@kim-em kim-em enabled auto-merge May 11, 2026 06:59
@kim-em kim-em disabled auto-merge May 11, 2026 13:46
mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request May 11, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request May 11, 2026
leanprover-bot added a commit to leanprover/reference-manual that referenced this pull request May 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

builds-manual CI has verified that the Lean Language Reference builds against this PR builds-mathlib CI has verified that Mathlib builds against this PR changelog-library Library mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: add Dyadic.divAtPrec for round-down dyadic division at given precision

2 participants