Skip to content

Fix zero-exponent identity for unit and negative moduli - #2775

Open
pralav-25 wants to merge 1 commit into
keon:mainfrom
pralav-25:fix/modular-exponent-zero
Open

pralav-25 wants to merge 1 commit into
keon:mainfrom
pralav-25:fix/modular-exponent-zero

Conversation

@pralav-25

@pralav-25 pralav-25 commented Sep 25, 2026 •

Copy link
Copy Markdown

modular_exponential(7, 0, 1) returns 1, while Python's pow(7, 0, 1) returns 0. The zero-exponent identity also needs reduction for negative moduli, which the existing loop otherwise supports.

Initialize the result as 1 % mod and document the nonzero modulus domain. Add a built-in pow oracle across signed bases, zero/positive exponents, and positive/negative moduli, plus a zero-modulus regression.

Validation: 721 tests pass; 480 documentation examples pass (one existing skip); repository Ruff checks pass.

Prepared with AI assistance and verified locally against the stated oracles.

Summary by CodeRabbit

  • Bug Fixes

    • Modular exponentiation now matches Python’s pow behavior for positive and negative nonzero moduli, including when the exponent is zero.
    • Using a zero modulus with an exponent of zero continues to raise ZeroDivisionError.
  • Tests

    • Added coverage comparing results with Python’s pow across selected bases, exponents, and moduli.

@coderabbitai

coderabbitai Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 35a37b93-a2ec-4893-b8d6-7aa2853b828d

📥 Commits

Reviewing files that changed from the base of the PR and between bd1d152 and 840e0ea.

📒 Files selected for processing (2)
  • algorithms/math/modular_exponential.py
  • tests/test_modular_exponential_boundaries.py

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

The modular exponential function now initializes its result as 1 % mod and documents that the modulus must be nonzero. New tests compare selected cases with Python’s three-argument pow and check the zero-modulus exception.

Changes

Modular exponential behavior

Layer / File(s) Summary
Normalize results and test boundaries
algorithms/math/modular_exponential.py, tests/test_modular_exponential_boundaries.py
The function starts with 1 % mod, and its documentation specifies nonzero positive or negative moduli. Tests compare selected inputs with pow and check that modulus zero raises ZeroDivisionError.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 840e0

The change aligns the zero-exponent result with Python’s modular pow behavior for nonzero moduli and tests the zero-modulus exception. No concrete merge-blocking risk is evident.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: correcting the zero-exponent result for unit and negative moduli.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

Comment @coderabbitai help to get the list of available commands.

This branch has not been deployed

No deployments
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.

1 participant