Skip to content

Float round/floor/ceil with |N| >= 20 give silently wrong results #132575

Closed

Description

@clickgapai

Describe what's wrong

  • floor, ceil and round on Float64 values with |N| >= 20 produce wrong output with no error.
  • Triggered by calls like floor(-1234.5678, -20) or floor(1.5e-20, 20), with constant or non-constant N.
  • Expected -100000000000000000000 but got -18446744073709552000; small values with large positive N collapse to 0 instead of the correct result.

Root cause: FloatRoundingImpl::apply and applyOne in FunctionsRound.h build the scale factor from intExp10, which saturates to UInt64 max for exponents above 19 (src/Common/intExp10.h), so the float is scaled by 1.8447e19 instead of the actual power of ten.

Analysis details (evidence, affected locations, impact)

Why we believe this is a bug: FunctionRounding::executeImpl -> Dispatcher::apply -> FloatRoundingImpl::apply (FunctionsRound.h:295) / applyOne (FunctionsRound.h:306) -> Op::prepare(intExp10(digits)); intExp10 returns std::numeric_limits<uint64_t>::max() for exponents above 19 (src/Common/intExp10.h:10-11).

Affected locations:

Impact: Silent wrong results when rounding floats to 20+ decimal places (small values collapse to 0) or to 10^20 and coarser (values snap to multiples of 1.8447e19). No error is raised.

Does it reproduce on most recent release?

Confirmed on master; not checked on a release.

How to reproduce

Run SELECT floor(-1234.5678, -20), ceil(1234.5678, -20), round(1.5e20, -20), floor(1.5e-20, 20) and compare with materialized N to confirm both constant and non-constant paths are affected.

▶ Run on ClickHouse Fiddle

-- Rounding a Float64 to |N| >= 20 digits must use the exact power of ten.
SELECT floor(-1234.5678, -20), ceil(1234.5678, -20), round(1.5e20, -20), floor(1.5e-20, 20);
SELECT floor(materialize(-1234.5678), materialize(-20)), floor(materialize(1.5e-20), materialize(20));

Verified: this block reproduces on current master (3634df08ebb7) when pasted as is.

Expected behavior

Rounding should scale by the exact power of ten (10^|N|) even when |N| >= 20.

Expected output of the reproducer above:

-100000000000000000000	100000000000000000000	200000000000000000000	1e-20
-100000000000000000000	1e-20

Error message and/or stacktrace

The scale factor saturates at 2^64-1, so results snap to multiples of that value or collapse to zero instead of the correct power-of-ten scaling.

Actual output of the reproducer above:

-18446744073709552000	18446744073709552000	147573952589676410000	0
-18446744073709552000	0
Suggested fix

Build the float scale from a floating-point power of ten (a double table up to 1e308, or exp10) when |N| > 19, keeping intExp10 for |N| <= 19 so the common path and its constant-divisor codegen stay unchanged. Return x when positive N exceeds the type's precision (avoids x * 10^N overflowing to inf). Trade-off: 10^N above 1e22 is not exact in double, so huge |N| is correct to double precision only.

Additional context

Same pattern as #132276 (found by: lexical, vector; vector: cosine distance 0.17 (agrees with the lexical leg)).

Open risks:

  • Float32 and BFloat16 share FloatRoundingImpl; only Float64 was run.

Found during automated review of PR #132299; whether that PR introduced it could not be established, so nobody is tagged. Severity P2 · Finding h_pr132299_002

Version info

Activity

  1. added
    comp-regular-function
    on Nov 18, 2026
  2. PedroTadim commented on Nov 18, 2026

    @PedroTadim
    Member

    @groeneai fix and I review

  3. groeneai commented on Nov 18, 2026

    @groeneai
    Collaborator

    This is fixed by #128321 (open, CI finished). On its head the reproducer above returns exactly the expected output, for constant and column N. Float32 takes the same path: floor(toFloat32(1.5e-20), 20) = 1e-20, floor(toFloat32(-1234.5678), -20) = -1e20. The PR now has Closes: lines for this issue and #132573, which it fixes too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ai_p2comp-regular-function

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions