Skip to content

feat: fix and expose Float.ofScientific#14110

Merged
TwoFX merged 2 commits into
leanprover:masterfrom
TwoFX:julia/float-ofscientific
Jun 18, 2026
Merged

feat: fix and expose Float.ofScientific#14110
TwoFX merged 2 commits into
leanprover:masterfrom
TwoFX:julia/float-ofscientific

Conversation

@TwoFX

@TwoFX TwoFX commented Jun 18, 2026

Copy link
Copy Markdown
Member

This PR completely rewrites the implementation of Float.ofScientific.

The new implementation passes all 5+ million tests from the parse-number-fxx-test-data suite. This comes at the expense of being significantly slower than the previous implementation. Unfortunately, based on my investigations, the old routine involving Float.scaleB seems difficult to salvage because it suffers from double-rounding issues, at least if the result turns out to be a subnormal. The algorithm described at https://www.exploringbinary.com/correct-decimal-to-floating-point-using-big-integers/ can deal with these problems, but it is also very slow.

The routine implemented here is as follows: we have a fast path if both the significant and the power of ten can be represented exactly; this should happen most of the time in normal usage. Otherwise, we call out to a model implementation on UnbundledFloat which manages to avoid the double-rounding issues. This is quite slow according to my measurements: the slowdown in this path is between 10x and 20x.

The new implementation can be executed in the kernel, though at the moment the slow path requires either a bump to the recursion depth or the use of decide +kernel.

This means that

example : 0.1 + 0.2 != 0.3 := rfl

works now.

The full parse-number-fxx-test-data suite is more than 200MB in size, so we do not include it here; instead, in CI we only test 10k tests from that suite, and the full suite can be run locally if desired.

Closes #13982.

@TwoFX TwoFX requested a review from kim-em as a code owner June 18, 2026 15:46
@TwoFX TwoFX added the changelog-library Library label Jun 18, 2026
@TwoFX

TwoFX commented Jun 18, 2026

Copy link
Copy Markdown
Member Author

!radar

@leanprover-radar

leanprover-radar commented Jun 18, 2026

Copy link
Copy Markdown

Benchmark results for 9a21edb against 5822584 are in. No significant results found. @TwoFX

  • build//instructions: -4.2M (-0.00%)

Small changes (1✅, 1🟥)

  • 🟥 build/module/Init.Data.OfScientific//instructions: +208.0M (+32.75%) (reduced significance based on *//lines)
  • build/module/Lean.Elab.BuiltinDo//instructions: -12.9M (-2.04%)

@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 Jun 18, 2026
@mathlib-lean-pr-testing

Copy link
Copy Markdown

Mathlib CI status (docs):

  • ❗ Mathlib CI can not be attempted yet, as the nightly-testing-2026-06-18 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-mathlib, Mathlib CI should run now. You can force Mathlib CI using the force-mathlib-ci label. (2026-06-18 16:31:03)

@leanprover-bot

Copy link
Copy Markdown
Collaborator

Reference manual CI status:

  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2026-06-18 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-06-18 16:31:04)

@Rob23oba

Copy link
Copy Markdown
Contributor

example : 0.1 + 0.2 = 0.3 := rfl
Hmm........ I sense an upside down "i" missing in that example

@TwoFX TwoFX added this pull request to the merge queue Jun 18, 2026
Merged via the queue into leanprover:master with commit 44232a0 Jun 18, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog-library Library 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.

Decimal Float literals can round incorrectly by 1 ulp

4 participants