Repository navigation
fix(compat): round/ceil/floor should return Infinity for infinite values with precision - #1764
Merged
raon0211 merged 1 commit intoJun 11, 2026
Conversation
…ues with precision
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
es-toolkit/compataims for 100% behavioral parity with lodash, butround,ceil, andfloordiverge when called onInfinity/-Infinitytogether with a non-zeroprecision: they returnNaNinstead of the infinite value.Root cause
All three functions delegate to the internal
decimalAdjusthelper. Whenprecisionis non-zero it shifts the value using exponential string notation:For
Infinity,number.toString()is"Infinity", so the constructed string becomes"Infinitye2", which is not a valid number —Number("Infinitye2")isNaN. Theprecision === 0path was unaffected because it skips this branch and callsMath[type](number)directly.lodash guards this exact case in
createRoundby additionally checking that the number is finite before doing the exponential shift (lodash.js#L5481):Fix
Mirror lodash by only applying the exponential-shift logic when the value is finite; otherwise fall through to
Math[type](number), which returns the infinity unchanged.The
-0handling is preserved (numbermay be the string'-0'at this point;Number('-0')is-0, which is finite), andNaNinput still yieldsNaNvia the fall-through, matching lodash.Verification
should return Infinity for infinite values regardless of precisiontests toround,ceil, andfloorspec files. They fail onmain(NaN) and pass with this fix.Infinityand-Infinity.es-toolkit/compattest suite passes (3091 tests); a fuzz comparison of the string/number compat functions against lodash now reports zero discrepancies.decimalAdjustis used only byround/ceil/floor, so the change is fully scoped to these three functions.