`Float.scaleB` / `Float32.scaleB` return +0.0 for a negative bignum exponent, dropping Inf, NaN and the sign of `x`
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 35/100
Direzione di ricerca
Read src/runtime/object.cpp around lean_float_scaleb and lean_float32_scaleb, then compare the bignum exponent branches with the machine-int scalbn path. The issue reports an existing pull request, #14690, that fixes both functions and adds regression tests in tests/elab/floatScaleB.lean; that pull request is already linked to the work. Done means the bignum path preserves Inf, NaN, and the sign of zero as shown by the reproductions.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Prerequisites
- Check that your issue is not already filed:
https://github.com/leanprover/lean4/issues - Reduce the issue to a minimal, self-contained, reproducible test case.
Avoid dependencies to Mathlib or Batteries. - Test your test case against the latest nightly release, for example on
https://live.lean-lang.org/#project=lean-nightly
(You can also use the settings there to switch to “Lean nightly”) - When reporting a perceived soundness bug: Does it affect
lake check --paranoid?
If in doubt, please reach out on [email protected] for responsible disclosure.
(Not a soundness bug.)
Description
Note on existing PRs: open PR #14690 ("fix: preserve non-finite values in
Float.scaleB", not merged as of 2026-11-27) would fix this issue for bothFloat.scaleBandFloat32.scaleBand adds regression tests intests/elab/floatScaleB.lean. There is no issue tracking the bug itself, so this issue documents it, with a reproduction on nightly-2026-11-26, as the issue PR #14690 can close.
lean_float_scaleb and lean_float32_scaleb share the same body. For a heap (bignum) exponent, the branch
} else if (a == 0 || mpz_value(b).is_neg()) {
return 0;
(src/runtime/object.cpp:2019-2020 for Float, 2071-2072 for Float32) returns the literal +0.0 whenever the exponent is negative or a == 0, discarding a. So +Inf, -Inf and NaN all become +0.0, and -0.0 and negative finite values lose their sign, where IEEE x * 2^i gives Inf/NaN/-0.0. The machine-int path (scalbn) is correct, so the bignum path disagrees with it and with the docstring "computes x * 2^i". Exponents beyond the Int scalar range are rare.
Documented behavior it contradicts (docstring of Float.scaleB, src/Init/Data/Float/Float.lean:601-606, and identically Float32.scaleB, src/Init/Data/Float/Float32.lean:603-609):
Efficiently computes
x * 2^i.This function does not reduce in the kernel.
Context
- Affected functions:
lean_float_scaleb,lean_float32_scaleb(src/runtime/object.cpp) - Upstream: no existing issue found. Open PR #14690 fixes both
lean_float_scalebandlean_float32_scaleb(see the note above). The cited code is unchanged in nightly-2026-11-26.
Steps to Reproduce
Float.scaleB (lean_float_scaleb)
- Save the following as
input.lean:
def main : IO Unit := do
let big : Int := -(2^70)
IO.println s!"{(Float.scaleB (1.0/0.0) big).toBits}"
IO.println s!"{(Float.scaleB (-1.0/0.0) big).toBits}"
IO.println s!"{(Float.scaleB (0.0/0.0) big).toBits}"
IO.println s!"{(Float.scaleB (-0.0) big).toBits}"
IO.println s!"{(Float.scaleB (-2.0) big).toBits}"
- Compile:
lean -c input.c input.lean && leanc -o prog input.c - Run:
./prog
Expected behavior: a * 2^i for a = +Infinity, -Infinity, NaN, -0.0, -2.0, i.e. +Infinity, -Infinity, NaN, -0.0, -0.0:
9218868437227405312
18442240474082181120
9221120237041090560
9223372036854775808
9223372036854775808
Actual behavior:
0
0
0
0
0
The same five calls with the machine-word exponent -3 print 9218868437227405312, 18442240474082181120, 9221120237041090560, 9223372036854775808, 13821547256400052224 (-0.25), so the difference is only the bignum exponent path.
Float32.scaleB (lean_float32_scaleb)
- Save the following as
input.lean:
def main : IO Unit := do
let big : Int := -(2^70)
let pinf : Float32 := Float32.ofBits 2139095040
let ninf : Float32 := Float32.ofBits 4286578688
let nan : Float32 := Float32.ofBits 2143289344
let nz : Float32 := Float32.ofBits 2147483648
let m2 : Float32 := Float32.ofBits 3221225472
IO.println s!"{(Float32.scaleB pinf big).toBits}"
IO.println s!"{(Float32.scaleB ninf big).toBits}"
IO.println s!"{(Float32.scaleB nan big).toBits}"
IO.println s!"{(Float32.scaleB nz big).toBits}"
IO.println s!"{(Float32.scaleB m2 big).toBits}"
- Compile and run as above.
Expected behavior: +Infinity, -Infinity, NaN, -0.0, -0.0:
2139095040
4286578688
2143289344
2147483648
2147483648
Actual behavior:
0
0
0
0
0
Versions
Lean (version 4.38.0-nightly-2026-11-26, arm64-apple-darwin24.6.0, commit e1228b8b45918ece1a00d68e5fc5e6a685ca59ee, Release)
macOS 26.6.2 (arm64)
Additional Information
Root cause analysis. The exponent -(2^70) is a boxed mpz because Lean small Ints stop at the scalar range, which is exactly what a compiled Lean caller passes. lean_is_scalar correctly reports false and mpz::is_neg correctly reports true, so the wrong literal 0 is produced by the function's own branch at object.cpp:2019-2020 / 2071-2072, which discards a.
Suggested fix: a correct fast path must keep infinities and NaN and carry the sign of a into the underflowed zero, e.g. return a when !isfinite(a) or a == 0, and copysign(0.0, a) otherwise. (The positive-bignum branch has a related corner: a == 0 returns +0.0 there too, dropping the sign of -0.0.)
Impact
Only Float.scaleB / Float32.scaleB calls with an exponent outside the scalar Int range together with an infinite, NaN or negative value return a wrong +0.0.
Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.
- Lingua principale
- Lean
- Stelle
- 10.3k
- Fork
- 1.1k
- Merge medio
- 16h 45m
- PR unite (30g)
- 361
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di leanprover/lean4
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
leanprover/lean4#16876 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
leanprover/lean4#16842 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
leanprover/lean4#16841 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 82/100
leanprover/lean4#16840 ·
I maintainer di solito rispondono entro 1 giorno
-
doc: stray "public" in Bool and HEq documentationForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 72/100
leanprover/lean4#16825 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di leanprover/lean4
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
Thai project names become 'project', 'project-2'… (slug keeps ASCII only)Forse già presa @mastel2 l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
storytold/craft-anything#2538 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
compiler-explorer/compiler-explorer#9738 ·
I maintainer di solito rispondono entro 2 giorni