num-rational
Generic rational numbers (fractions) for Rust with exact arithmetic and automatic reduction.
Repository Health
Technical Analysis
num-rational provides generic rational number types — fractions represented as a numerator and denominator — for Rust. It is part of the well-established num family of numeric crates and offers exact, lossless arithmetic that automatically reduces fractions to lowest terms.
The Ratio<T> type is generic over any integer type, so you can build rationals over i32, i64, or arbitrary-precision BigInt (via the optional num-bigint feature) with convenient Rational32, Rational64, and BigRational aliases. It supports no_std, parsing from strings, conversions, and full operator overloading.
What You Get
- A generic Ratio<T> rational type with numerator/denominator storage
- Exact arithmetic that automatically reduces fractions to lowest terms
- Convenient Rational32, Rational64, and BigRational type aliases
- Optional arbitrary-precision rationals via num-bigint
- no_std support, string parsing, conversions, and full operator overloading
Common Use Cases
- Performing exact fractional arithmetic without floating-point error
- Representing precise ratios in math, physics, and financial calculations
- Working with arbitrary-precision fractions using BigRational
- Building higher-level numeric libraries on top of the num trait ecosystem
Under The Hood
Architecture - num-rational is a single library crate centered on Ratio<T> in lib.rs, with functionality split across focused modules: ops.rs/opassign.rs for arithmetic and assignment operators, cmp.rs for ordering, convert.rs for numeric conversions, pow.rs for exponentiation, str.rs for parsing/formatting, and iter.rs for sum/product. Every constructor normalizes by dividing out the GCD and canonicalizing the sign.
Tech Stack - Written in Rust (MSRV 1.60) building on num-integer and num-traits for GCD and generic numeric behavior, with optional num-bigint for BigRational and optional serde. The default std feature can be disabled for no_std targets.
Code Quality - The crate is mature and well-tested, with a dedicated tests.rs and tests/ directory covering arithmetic, parsing, and edge cases, plus CI configuration and RELEASES notes. As a core member of the num family it carries hundreds of millions of downloads and long-term stability.
API Design - The API mirrors Rust’s standard numeric idioms — operator overloading, From/Into conversions, and trait implementations from num-traits — so Ratio behaves like a built-in number type. Documentation on docs.rs and simple type aliases keep the learning curve low.