RustPython / RustPython/pymath

Add fractions module: Rational number arithmetic

オープン
#16 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
Rust
スター
12
フォーク
1
平均マージ
8分
マージ済み PR(30日)
1

説明

Summary

Port Python's fractions.Fraction to Rust — arbitrary-precision rational number arithmetic.

In CPython, this is a pure Python implementation (Lib/fractions.py) that internally stores a (numerator: int, denominator: int) pair, GCD-normalized with a positive denominator.

Design

Type representation
pub struct Fraction<I> {
    numerator: I,
    denominator: I,  // always positive, coprime with numerator
}

Follow the existing _bigint feature pattern to support both num-bigint and malachite-bigint. Implement as generic over integer type or via trait bounds.

Feature flag
[features]
fractions = ["_bigint"]  # requires BigInt

Same pattern as cmath behind the complex feature.

Implementation plan

Phase 1: Core struct & construction
  • Fraction struct (numerator, denominator)
  • GCD normalization (sign normalization included: denominator always positive)
  • new(numerator, denominator) — primary constructor
  • from_integer(n) — construct from integer
  • from_float(f64) — using f64::integer_decode() or as_integer_ratio logic
  • from_coprime_ints(n, d) — direct construction from pre-normalized values (internal use)
  • String parsing: "3/4", "1.5", "1e-2", etc.
Phase 2: Arithmetic operations
  • Add, Sub, Mul, Div (Rust traits)
  • Neg, Abs
  • Floor division, modulo, divmod
  • Pow — integer exponent returns Fraction / non-integer returns f64

Arithmetic optimization: Knuth TAOCP 4.5.1 approach (pre-compute GCD in multiplication to avoid unnecessary blowup)

Phase 3: Comparison & conversion
  • Eq, Ord (cross-multiply comparison)
  • as_f64() — float conversion
  • trunc(), floor(), ceil(), round() — integer conversions
  • is_integer() — checks denominator == 1
  • as_integer_ratio() — returns (numerator, denominator) tuple
Phase 4: Special methods
  • limit_denominator(max_denominator) — best rational approximation via continued fraction algorithm
  • Hash: compatible with Python's _hash_algorithm (modular inverse based)
  • Display: "{numerator}/{denominator}" or "{numerator}" when integer
  • Format: float-style formatting (.2f, e, g, etc.)
Phase 5: Testing
  • Use the existing pyo3 proptest pattern from the project
  • Verify bit-identical results against fractions.Fraction
  • Edge cases: zero, negatives, very large numbers, float precision boundary values

Out of scope

  • Python numbers.Rational ABC (replaced by Rust traits)
  • Direct conversion with Decimal type (separate module scope)
  • Pickle/copy (Python runtime dependent)

References

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず CPython の Lib/fractions.py と、プロジェクトに既存の _bigint feature パターン(complex feature の背後にある cmath を含む)を読んでください。段階的な API と動作を定義する前に、pyo3 の proptest パターンを確認してください。fractions feature が、fractions.Fraction に対する、列挙された構築、算術、変換、フォーマット、およびエッジケースのテストをサポートすれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python, rust
領域
backend
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。