Skip to content

fixed #5260: return self from floor for values >= 2^52#5266

Merged
yegor256 merged 1 commit into
objectionary:masterfrom
arnabnandy7:fix/number-floor-overflow
Jun 26, 2026
Merged

fixed #5260: return self from floor for values >= 2^52#5266
yegor256 merged 1 commit into
objectionary:masterfrom
arnabnandy7:fix/number-floor-overflow

Conversation

@arnabnandy7

Copy link
Copy Markdown
Contributor

Description

This PR fixes a bug in number.floor where finite numbers of magnitude $\ge 2^{63}$ were incorrectly returning saturated values (about $\pm 9.22 \times 10^{18}$) due to narrowing conversion to i64.

Problem

number.floor was computing trunc = ^.as-i64.as-number. The conversion as-i64 is backed by Double.longValue(). For finite doubles whose magnitude is $\ge 2^{63}$, Double.longValue() saturates to Long.MAX_VALUE or Long.MIN_VALUE per Java Language Specification narrowing rules, causing floor of values like 1e20 or -1e20 to return the saturated boundary value.

Solution

Since double-precision floating point numbers have 52 mantissa bits, any double value with a magnitude $\ge 2^{52}$ cannot represent a fractional part and is already an exact mathematical integer.

We update the floor implementation to check if the magnitude is $\ge 2^{52}$ (specifically $\ge 4503599627370496$ or $\le -4503599627370496$), and if so, return the number itself unchanged. This bypasses the narrowing conversion path entirely for values where the result is guaranteed to be equal to the input.

Related Issues

Closes #5260

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Performance Analysis

All benchmarks are within the acceptable range. No critical degradation detected (threshold is 100%). Please refer to the detailed report for more information.

Click to see the detailed report
Test Base Score PR Score Change % Change Unit Mode
benchmarks.XmirBench.xmirToEO 12628.043 12583.618 -44.425 -0.35% ms/op Average Time

✅ Performance gain: benchmarks.XmirBench.xmirToEO is faster by 44.425 ms/op (0.35%)

@0crat

0crat commented Jun 26, 2026

Copy link
Copy Markdown

@arnabnandy7 It is not a good idea to name Git branches the way you named this one: "fix/number-floor-overflow". You've earned -6 points. Next time, better give your branch the same name as the number of the ticket that you are solving. In this case, a perfect name, for example, would be "5265". Your running score is -6; don't forget to check your Zerocracy account too).

@yegor256 yegor256 merged commit 7ce7a77 into objectionary:master Jun 26, 2026
26 checks passed
@sonarqubecloud

Copy link
Copy Markdown

@0crat

0crat commented Jun 26, 2026

Copy link
Copy Markdown

@arnabnandy7 Thanks for the contribution! You've earned +12 points for this: +16 as a basis; -4 for too few (18) hits-of-code. Please, keep them coming. Your running score is +6; don't forget to check your Zerocracy account too).

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.

number.floor incorrectly returns saturated value for finite numbers >= 2^63 due to as-i64 implementation

3 participants