`Float.scaleB` / `Float32.scaleB` return +0.0 for a negative bignum exponent, dropping Inf, NaN and the sign of `x`

Aperta
#16,806 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

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
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
c
Ambito
backend, testing

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

bug
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 both Float.scaleB and Float32.scaleB and adds regression tests in tests/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_scaleb and lean_float32_scaleb (see the note above). The cited code is unchanged in nightly-2026-11-26.
Steps to Reproduce
Float.scaleB (lean_float_scaleb)
  1. 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}"
  1. Compile: lean -c input.c input.lean && leanc -o prog input.c
  2. 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)
  1. 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}"
  1. 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

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di leanprover/lean4

Tutte le issue di leanprover/lean4

Issue simili

Altre issue su Backend & API Design

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.