RustPython / RustPython/pymath

Add fractions module: Rational number arithmetic

未关闭
#16 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
Rust
星标
12
派生
1
平均合并
8 分钟
30 天内合并 PR
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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先阅读 CPython 的 Lib/fractions.py 以及项目现有的 _bigint feature 模式,包括 complex feature 背后的 cmath。在定义分阶段的 API 和行为之前,先查看 pyo3 的 proptest 模式。完成的标准是 fractions feature 支持针对 fractions.Fraction 列出的构造、算术、转换、格式化和边界情况测试。

由索引模型根据 Issue 内容生成。

评估

技术栈
python, rust
领域
backend
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。