Repository navigation
Take an odd root of an integer MinValue without recursing forever [patch] - #472
Merged
Merged
Conversation
…tch] -MinValue wraps back to MinValue, so RootN called itself with the same arguments until the stack overflowed, which ends the process. It now roots the magnitude one below and adds one where MinValue's magnitude is an exact nth power, checked by division so nothing overflows. Fixes #464 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CcmGrWCwm3uDHcCpVZVcJU
This was referenced Oct 20, 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.



Fixes #464
What was wrong
StorageMath.RootNhandled a negative value with an odd degree by returning-RootN(-value, n). For a two's-complement integer'sMinValue,-valuewraps back toMinValue.RootNtherefore called itself with the same arguments until the stack overflowed, which ends the process. This hitCbrt(sbyte.MinValue),RootN(int.MinValue, 5), and theMinValueofshort,longandInt128.Change
RootNroots-(value + 1), the magnitude one below. The floors of the two roots differ only when|MinValue|is itself an exact nth power, and then by exactly one.IsExactPowerhelper checks that case by dividingvalueby the candidate rootntimes, to see whether it leaves exactly-1. Division keeps everything in range, so nothing overflows.Cbrt(long.MinValue)is-2097152(2^21) andRootN(sbyte.MinValue, 7)is-2.if.Tests
New test:
OddRootOfIntegerMinValueIsTheExactFloor. It coverssbyte,short,int,longandInt128withCbrtand oddRootN, including the exact-power cases. The expected values were cross-checked with arbitrary-precision integer arithmetic.StorageMath.cschange reverted, the test host dies withStack overflow.Generated by Claude Code