Skip to content

Align cpython#18

Merged
youknowone merged 3 commits intomainfrom
align-cpython
Mar 14, 2026
Merged

Align cpython#18
youknowone merged 3 commits intomainfrom
align-cpython

Conversation

@youknowone
Copy link
Member

@youknowone youknowone commented Mar 14, 2026

Close #15

Summary by CodeRabbit

  • New Features

    • Explicit error returns for previously-panicking math ops to surface invalid inputs as errors.
  • Bug Fixes

    • Corrected edge-case rounding for large-number logs and fixed zero-denominator / length-mismatch error signaling.
  • Performance

    • Faster stepped behavior for nextafter with improved saturation and crossing-zero handling.
  • Documentation

    • Expanded docs clarifying math semantics and error behavior across platforms.
  • Tests

    • Added extensive regression and cross-language tests for edge cases and numeric parity.

Replace the O(n) loop in nextafter(x, y, steps) with O(1) IEEE 754
bit manipulation matching math_nextafter_impl in mathmodule.c.
Handles sign-crossing, saturation, and all edge cases.

Add pyo3 proptest and edge/extreme-step tests for nextafter.
cmath: Rewrite log(z, base) to match cmath_log_impl errno
semantics. c_log always returns a value and separately reports
EDOM for zero. c_quot returns EDOM and (0,0) for zero
denominator instead of NaN.

bigint: Add sticky bit to frexp_bigint for correct IEEE
round-half-to-even, matching _PyLong_Frexp. Use mul_add for
log/log2/log10 bigint to match loghelper fma usage.

Add regression tests for remainder tie-to-even and signed zero.
@coderabbitai
Copy link

coderabbitai bot commented Mar 14, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0b61d888-fd23-4236-87a9-3ef253daf8a6

📥 Commits

Reviewing files that changed from the base of the PR and between ba2bad5 and 57f01ee.

📒 Files selected for processing (6)
  • Cargo.toml
  • src/math.rs
  • src/math/aggregate.rs
  • src/math/bigint.rs
  • src/math/exponential.rs
  • src/math/misc.rs

📝 Walkthrough

Walkthrough

Bumps crate version to 0.2.0 and adds error-aware complex math helpers, changes sumprod to return Result with EDOM on length mismatch, introduces sticky-bit rounding for BigInt→f64, implements O(1) multi-step nextafter bit-path, and expands documentation and tests.

Changes

Cohort / File(s) Summary
Version
Cargo.toml
Package version changed from 0.1.5 to 0.2.0.
Complex math (cmath)
src/cmath/exponential.rs
Added c_log(z) -> (Complex64, Option<Error>); changed c_quot to return (Complex64, Option<Error>); log now propagates EDOM-style errors; new regression tests for zero-denominator and log error behavior.
Aggregation math
src/math/aggregate.rs
pub fn sumprod(p, q) signature changed to crate::Result<f64>; returns EDOM on length mismatch; implements two-path accumulation (fast TripleLength path with fallback); tests updated/added.
BigInt → float precision
src/math/bigint.rs
Introduced sticky-bit rounding in frexp_bigint and adjusted conversions; replaced manual adds with fused mul_add for log/log2/log10; added bit-exact regression tests vs. Python.
nextafter & remainder
src/math/misc.rs
Added O(1) bit-manipulation path for nextafter(x,y,Some(steps)), handling saturation, zero-crossing, and sign cases; retained fallback for steps=None; added many edge/proptest tests and docs.
Documentation & comments
src/math.rs, src/math/exponential.rs
Expanded comments clarifying errno/EDOM behavior and CPython alignment (no runtime logic changes).
Tests / formatting
src/test.rs, src/math/integer.rs
Minor formatting/comment adjustments; test entries reflowed; no behavioral changes.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • _bigint helpers #11: Modifies src/math/bigint.rs with BigInt conversion/rounding and log helpers — closely related to the sticky-bit and log bit-exact changes.
  • Implement full math and cmath modules #7: Changes in src/cmath/exponential.rs around complex log/quotient error handling overlapping with the new c_log/c_quot work.
  • Fix build for various targets #12: Also touches src/math/bigint.rs and version bumps; overlaps on BigInt conversion and precision test coverage.

Poem

🐰
Hops through bits with tiny paws,
I patch the logs and mind the laws.
Sticky tails keep rounding neat,
nextafter leaps in O(1) beat —
version two, precise and fleet!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'Align cpython' is vague and generic; it does not clearly describe the specific changes made to the codebase. Consider a more descriptive title that highlights the main change, such as 'Add error handling for complex logarithm and math functions' or 'Improve error handling and documentation for CPython alignment'.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch align-cpython
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

- math_1/math_2: document why errno handling differs from
  CPython (platform-specific unreliability, output checks
  sufficient, verified by proptest)
- math.log: document EDOM substitution for ZeroDivisionError
- math.remainder: document libm delegation rationale
- sumprod: return Result for length mismatch instead of panic,
  improve overflow fallback to continue from where the fast
  path stopped instead of restarting from scratch
@youknowone youknowone merged commit 5765480 into main Mar 14, 2026
5 checks passed
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.

Review pymath 100% aligned to CPython 3.14.3

1 participant