Skip to content

fix: float32 Exp and Log correctly rounded, proven over every float32 - #87

Merged
tannevaled merged 4 commits into
mainfrom
test/float32-exhaustive
Nov 1, 2026
Merged

tannevaled merged 4 commits into
mainfrom
test/float32-exhaustive

Conversation

@tannevaled

@tannevaled tannevaled commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

float32 Exp and Log are now correctly rounded: every result is the float32 nearest the exact value. This is proven over all 2³² inputs.

The proof: TestExp32CorrectlyRounded and TestLog32CorrectlyRounded run every bit pattern with NDARRAY_EXHAUSTIVE=1; CI samples every 65 537th. Each result is certified in one of three ways:

  • the float64 intermediate (0.504 ULP) is more than 2 float64 ULP from both float32 midpoints;
  • it is far outside float32's range;
  • or a 300-bit reference (refExp/refLog) decides the rounding.

Controls:

  • The certifier is tested on planted midpoints and on the overflow boundary. The overflow boundary was a real bug in the certifier, caught by that test before the run.
  • A kernel sabotaged one ULP up fails 76% of exp inputs and 50% of log inputs.

Fix: Log's double rounding was wrong for 5 inputs (e.g. log(9.47263622) = 0x400fe5e8, not 0x400fe5e7). The fix is a midpoint check (one integer comparison) plus a table of the 24 near-midpoint inputs with their 300-bit-reference roundings. It costs nothing measurable (−4%, p = 0.07, host at load 15).

Results, run on each real machine:

darwin/arm64 linux/amd64 (Zen 3) ppc64le (POWER9) s390x (IBM Z) riscv64 (X60)
Exp 0 wrong 0 wrong 0 wrong 0 wrong 0 wrong
Log 0 wrong 0 wrong 0 wrong 0 wrong 0 wrong

loong64 is covered by CI's sampled run only, because the Loongson host refuses our key.

NumPy's float32 kernels are documented at 2.52 ULP (exp) and 3.83 ULP (log) in their sources.

🤖 Generated with Claude Code

tannevaled and others added 4 commits November 1, 2026 11:56
An exhaustive test runs all 2^32 float32 inputs and certifies each
result in one of three ways: the float64 intermediate (0.504 ULP) is
more than 2 float64 ULP from both float32 midpoints, or it is far
outside float32's range, or a 300-bit reference decides the rounding.
The certifier is tested on planted midpoints and the overflow boundary.
That boundary case was a bug, caught by the test. A kernel sabotaged one
ULP up fails 76% / 50% of the inputs.

Exp: 0 incorrectly rounded (2 decided by the reference), on
darwin/arm64 and linux/amd64.
Log: 5 inputs double-rounded wrong, e.g. log(9.47263622) gave
0x400fe5e8, not 0x400fe5e7. Fixed with the usual exception table: a
result within 8 float64 ULP of a midpoint is looked up among the 24
inputs that come within 2 ULP, each with its 300-bit-reference
rounding. With the table, 0 incorrectly rounded on both architectures.
The check costs nothing measurable on Zen 3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…not reach them)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 5ce766c into main Nov 1, 2026
12 checks passed
@tannevaled
tannevaled deleted the test/float32-exhaustive branch November 1, 2026 11:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant