Repository navigation
Fix round, floor, ceil and trunc of floats returning inf on overflow or a wrong value for |N| >= 20 - #128321
Conversation
`round`, `roundBankers`, `floor`, `ceil` and `trunc` of `Float64`, `Float32` and `BFloat16` computed `round(x * 10^N) / 10^N` (or the negative-`N` mirror) with a scale from `intExp10`, which saturates to 2^64 - 1 for `|N| > 19`: - For a positive `N`, `x * 10^N` overflows to `inf` when `x` is large (`round(1e300, 10) = inf`). Such an `x` has no digits after the N-th decimal place, so the result is now `x`. - For `|N| > 19`, the scale was 2^64 instead of 10^N (`round(1.5e-20, 20) = 0`, `floor(1.23456789e25, -20)` wrong). The rounding functions now receive the exponent (as in the integer rounding fix). `|N| <= 19` keeps the vectorized computation in the argument type and only adds the overflow select for a positive `N`; every finite result there is unchanged. `|N| > 19` is computed in `Float64` for every float type, with the correctly rounded 10^N from `preciseExp10`: - results from 2^52 on are integers, so `x` is returned instead of a round trip through an inexact scale; - a scale above 10^308 is applied in two steps (positive `N`), or answered directly (negative `N`: every finite `x` is below 10^N in magnitude); - `ceil` of a tiny positive `x` and `floor` of a tiny negative `x`, whose quotient underflows to 0, give 10^N and -10^N; - a directed result is clamped to the valid side of `x`, which an inexact 10^N (`N > 22`) can otherwise cross (`ceil(6.385506180058851e-264, 278)`). Co-Authored-By: Claude Opus 5.5 <[email protected]>
… underflow For a negative `N` with `|N| <= 19`, `round(x / 10^N) * 10^N` overflowed to `inf` for `x` at the type max (`floor(toFloat32(3.4028235e38), -2)`, `round(1.7976931348623157e308, -6)`), although such an `x` has no digits below `10^N`, and `x / 10^N` underflowed to zero for a tiny `x`, so `ceil` and `floor` returned `0`/`-0` instead of `10^N`/`-10^N` (`ceil(5e-324, -1)`, `ceil(toFloat32(1e-30), -19)`). The negative branch now returns `x` when the result is not finite, and `ceil`/`floor` use `1`/`-1` when the quotient underflows, as the `|N| > 19` path already does. Also use `if constexpr` for the template-parameter branches of `FloatRoundingLargeScale::compute`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rop the |N| <= 19 underflow handling For |N| <= 19 with a negative N, `x / 10^N` underflows to zero for a tiny nonzero x, so `ceil(5e-324, -1)` is 0 instead of 10. Handling it inside the vectorized loop costs ~4% on the negative-N ceil/floor loops (release, one thread, numbers(1e9), three runs of 11 reps), even in the cheapest bit-identical form (substituting `val` for an underflowed quotient: one compare and one blend). That is above the 3% budget for this hot loop, and the case only concerns values below the type's normal range divided by up to 10^19, so it stays as on master. What remains for |N| <= 19 is the overflow select at the type max (`floor(toFloat32(3.4028235e38), -2)` returned inf), which costs at most ~2% on the same benchmark. The |N| > 19 path keeps its underflow handling (`ceil(1e-300, -30)` = 1e30). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; rounds 1-3: 0, 1 and 1 findings) and my own review (3 rounds).
Severity: ❌ blocker / |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20261030-222201 |
|
cc @Algunenano @rschu1ze, could you review this and add the |
|
Workflow [PR], commit [29454f5] Summary: ❌
AI ReviewSummaryThe PR fixes spurious floating-point overflow in Final Verdict✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes3 object files changed (+116.40 KiB total), 0 added.
711 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units10 translation units recompiled, 81 s compile time in total, 10 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.12 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
CI finish ledger - 8b8fe4aEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
The first runs of this head stopped at Session id: cron:our-pr-ci-monitor:20261031-140009 |
|
Fixing PR for the |
…large-scale Resolve the conflict in src/Functions/FunctionsRound.h with ClickHouse#128283: keep master's IntegerRoundingImpl, keep this branch's FloatRoundingImpl, whose FloatRoundingComputation::prepare takes the exponent itself. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - 99ac658Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261115-190012 |
|
🕵️ @groeneai looks good to me. I checked the new path independently:
One non-blocking note: some results are still The failed |
|
Thanks for the independent check. I added a sentence to the changelog entry: |
CI finish ledger - 3393765Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20261118-133026 |
AI review of a later commit does not approve the PR, see the AI Review comment.
CI finish ledger - 29454f5Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:clickhouse-premium-slot-24:20261118-203901 |
f98d6d5
Closes: #132573
Closes: #132575
Related: #128283
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixes
round,roundBankers,floor,ceilandtruncofFloat32,Float64andBFloat16values returninginfwhen the scaled value overflows (round(1e300, 10),floor(toFloat32(3.4028235e38), -2)) and a wrong value for|N| >= 20(round(1.5e-20, 20)returned0).infis still returned when the exact result is beyond the range of the type (ceil(1., -400),round(1.7e308, -308)). Closes #132573. Closes #132575.Description
On 26.3 and 26.8,
round(1e300, 10)returnsinf,floor(toFloat32(3.4028235e38), -2)returnsinfandround(1.5e-20, 20)returns0(alsoroundBankers,ceil,trunc,Float32,BFloat16, columnN).Two defects in the float path of
FunctionsRound.h:round(x * 10^N) / 10^Noverflows toinfwhenx * 10^Nexceeds the type, and so doesround(x / 10^N) * 10^Nforxat the type max, although such anxhas no digits at that precision;intExp10, which saturates to2^64 - 1for|N| > 19.For floats (
Dispatcherpasses the exponent since #128283):|N| <= 19: the existing vectorized loop, plus a select that returnsxwhen the result overflows. Every other result is unchanged.|N| > 19: computed inFloat64with the correctly rounded10^N(preciseExp10).xis returned once the scaled value reaches2^52;N > 308scales in two steps,N < -308is answered directly;ceilandfloorof anxwhose quotient underflows give10^Nand-10^N; directed results are clamped to the valid side ofx, which an inexact10^Ncan otherwise cross.Not changed, as on master, for
|N| <= 19: the computation stays in the argument type and can be an ulp off, andceil(5e-324, -1)is still0becausex / 10^Nunderflows (handling it costs 4% on theceilandfloorloops).Validation: the new test fails on master and passes 50/50 randomized runs. 46k random and adversarial cases match, bit for bit on the constant and column
Npaths, a model checked against exact rationals; for|N| <= 19results equal master's except where master returnedinf. Release build, one thread: constant-Nrounding within 3% of master, loops still vectorized.Workflow [PR]
Sync PR [sync-upstream/pr/128321]
Version info
26.11.1.2771-master(included in26.11and later)