fix(core): return error instead of panicking on negative power edge cases 💥#171
Merged
Merged
Conversation
…ases 💥 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Context
While fuzzing the pipeline for panics, I found that the integer-power (
^) path aborts the whole process on several negative-exponent inputs. These are recoverable runtime errors that should never crash the interpreter — same class of bug as the recent integer division-by-zero fix (#168).The crashes
2 ^ (1/-1)right hand side must not be negative(int.rs:117)0 ^ -1denominator == 0(num-rational)0 ^ (2/-1)right hand side must not be negativeRoot cause —
Number::powhad two integer-exponent branches:Int ^ Inthandled negative exponents as the reciprocal1/(base^|exp|), but with base0that buildsBigRational::new(1, 0), which panics.Int ^ Rational(integer-valued rational, e.g.1/-1) didn't handle negatives at all — it calledInt::powdirectly, which panics unconditionally on a negative RHS.The existing
MAX_EXPONENT_BITSguard only checks magnitude, so it never caught these.Changes
Number::int_pow(base, exponent)helper that both integer-exponent branches route through. It returns adivision by zeroerror for0 ^ negative, computes the reciprocal rational for any other negative exponent, and otherwise returns the plain integer power. This also removes the previously duplicated negative-exponent logic.900_bugs/bug0025_pow_negative_rational_exponent.ndc—2 ^ (1/-1)→1/2001_math/030_pow_zero_negative_exponent.ndc—0 ^ -1→division by zero001_math/031_pow_zero_negative_rational_exponent.ndc—0 ^ (2/-1)→division by zeroValid cases verified unchanged:
2 ^ -1→1/2,3 ^ (-4/2)→1/9,2 ^ 3→8,(1/2) ^ -3→8.🤖