Repository navigation
Fix #11148: BigInt to float/double conversion at 64-bit word boundaries and near the maximum - #11149
Merged
Merged
Conversation
…ndaries and near the maximum When the top 64-bit word has its high bit set, toFloat shifted the next word right by 64, which is undefined; on x86-64 the whole word was ORed into the mantissa. The overflow checks also compared the bit length plus one with T.max_exp, so the top two binades, including float.max and double.max, became infinity.
thewilsonator
approved these changes
Nov 11, 2026
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.
Rationale
Two fixes in
BigInt.toFloat, which backscast(float),cast(double)and comparisons with floating-point values:w2 >>> w1BitCountshifts aulongby 64. That is undefined, and on x86-64 the count wraps to 0, so the whole next word is ORed into the mantissa. No bits of the next word are needed in that case, so they are now skipped.exponentis the bit length plus one, so the unbiased exponent isexponent - 2, which is what the biased exponent below is built from. Comparingexponentitself withT.max_expsent the top two binades, includingfloat.maxanddouble.max, to infinity.The unittest covers both cases, the rounding carry at
float.maxanddouble.max, negative values, and therealcomparison path. I also compared 20,000 random BigInts against Python'sfloat(int): the only differences left are exact ties, which round away from zero as documented.Feature request or issue tracking
Closes #11148.
Pre-review checklist
No changelog entry, since this fixes a reported issue.