Skip to content

fix: preserve non-finite values in Float.scaleB - #14690

Open
felix314159 wants to merge 2 commits into
leanprover:masterfrom
felix314159:master
Open

fix: preserve non-finite values in Float.scaleB#14690
felix314159 wants to merge 2 commits into
leanprover:masterfrom
felix314159:master

Conversation

@felix314159

@felix314159 felix314159 commented Aug 5, 2026

Copy link
Copy Markdown

This PR makes Float.scaleB and Float32.scaleB preserve infinities, NaN, and signed zero when scaling by extremely large exponents.

Previously, the boxed-exponent path returned positive zero for every huge negative exponent, including non-finite inputs and negative finite values. Handle non-finite inputs and zero directly, and preserve the sign of underflowed finite values.

The PR also adds regression coverage for both precisions in tests/elab/floatScaleB.lean.

@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 Aug 5, 2026
@mathlib-lean-pr-testing

mathlib-lean-pr-testing Bot commented Aug 5, 2026

Copy link
Copy Markdown

Mathlib CI status (docs):

  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 4c29de6f2cb93ffdabd2838c1eed6f55061bb605 --onto f2bcf2e8660ab2d16cf3cb50c8e127de0439a337. You can force Mathlib CI using the force-mathlib-ci label. (2026-08-05 17:20:13)
  • ✅ Mathlib branch lean-pr-testing-14690 has successfully built against this PR. (2026-08-05 18:57:27) View Log
  • ✅ Mathlib branch lean-pr-testing-14690 has successfully built against this PR. (2026-08-05 22:02:21) View Log

@leanprover-bot

leanprover-bot commented Aug 5, 2026

Copy link
Copy Markdown
Collaborator

Reference manual CI status:

  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase 4c29de6f2cb93ffdabd2838c1eed6f55061bb605 --onto 23393b959b33e3a8d15796b2397f8a04c315b9f4. You can force reference manual CI using the force-manual-ci label. (2026-08-05 17:20:15)
  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2026-08-05 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-08-05 18:00:38)

This PR makes `Float.scaleB` and `Float32.scaleB` preserve infinities, NaN, and signed zero when scaling by exponents too large for the native `scalbn` interface.

Handle boxed exponents using the limiting IEEE behavior and add regression coverage for both floating-point precisions.
@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 Aug 5, 2026
@mathlib-lean-pr-testing mathlib-lean-pr-testing Bot added the builds-mathlib CI has verified that Mathlib builds against this PR label Aug 5, 2026
@Rob23oba

Rob23oba commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

You can add labels by writing a comment that just contains the name of the label (you can also delete the comment afterwards). You might need to edit your PR description back and forth for the check to notice the label change though.
Also, don't worry about reference manual CI, it's not a blocker for merging (neither is mathlib CI but it's nice to have confirmation).

Comment thread tests/compile/floatScaleB.lean Outdated
Comment on lines +12 to +22
public def main : IO Unit := do
IO.println ((Float.inf.scaleB hugeNegativeExponent).isInf)
IO.println (((-Float.inf).scaleB hugeNegativeExponent).isInf)
IO.println ((Float.nan.scaleB hugeNegativeExponent).isNaN)
IO.println (((-1.0 : Float).scaleB hugeNegativeExponent).toBits == (-0.0 : Float).toBits)
IO.println (((-0.0 : Float).scaleB hugePositiveExponent).toBits == (-0.0 : Float).toBits)
IO.println ((Float32.inf.scaleB hugeNegativeExponent).isInf)
IO.println (((-Float32.inf).scaleB hugeNegativeExponent).isInf)
IO.println ((Float32.nan.scaleB hugeNegativeExponent).isNaN)
IO.println (((-1.0 : Float32).scaleB hugeNegativeExponent).toBits == (-0.0 : Float32).toBits)
IO.println (((-0.0 : Float32).scaleB hugePositiveExponent).toBits == (-0.0 : Float32).toBits)

@Rob23oba Rob23oba Aug 5, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the nature of this test, this should probably live in tests/elab instead and use #guard for testing (instead of IO.println).
Edit: Also, it might be more appropriate to use equality here for testing (which essentially compares toBits), i.e.

#guard Float.inf.scaleB hugeNegativeExponent = Float.inf
#guard (-Float.inf).scaleB hugeNegativeExponent = -Float.inf
#guard Float.nan.scaleB hugeNegativeExponent = Float.nan
#guard (-1.0 : Float).scaleB hugeNegativeExponent = -0.0
#guard (-0.0 : Float).scaleB hugeNegativeExponent = -0.0

Comment thread tests/compile/floatScaleB.lean Outdated
@@ -0,0 +1,22 @@
module

import Init.Data.Float.Float32

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need this import, declarations from Init are imported by default.

@github-actions github-actions Bot added the changelog-compiler Compiler, runtime, and FFI label Aug 5, 2026
@felix314159

Copy link
Copy Markdown
Author

Thanks for the feedback, changes have been implemented and label is applied

mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Aug 5, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

builds-mathlib CI has verified that Mathlib builds against this PR changelog-compiler Compiler, runtime, and FFI 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.

3 participants