Expand description
puremp — arbitrary-precision arithmetic written entirely in Rust, depending
on no foreign code.
It provides a family of numeric types, built bottom-up:
- Integers — unsigned
Natand signedInt, the workhorse layer that carries the hard limb-level algorithms (multiplication, division, GCD, modular arithmetic, …). Enabled by theintfeature. - Rationals —
Rational, exactp/qfractions kept in lowest terms; plusInfRational, the same extended with±∞/NaN.rationalfeature. - Dyadics —
Dyadic, exactn·2^-kbinary fractions.dyadicfeature. - Floats —
Float, binary floating-point with a caller-chosen precision and directedRoundingMode, aiming at MPFR-class correct rounding, plusFixedFloat, a fixed-precision wrapper with operators.floatfeature. - Decimals —
Decimal, exact base-10 floating point (PythonDecimal-style), with directed rounding.decimalfeature.
Built on top of these are several derived structures, each generic or specialised as noted:
ModInt— modular integersℤ/mℤwith automatic reduction (int).Complex— generic complex numbers / Gaussian integers (complex).Poly— generic univariate polynomials (poly).Matrix— dense matrices with exact determinant/inverse/solve (matrix).Interval— outward-rounded interval arithmetic (interval).Quadratic/Algebraic— exact quadratic irrationalsℚ(√d)and general real algebraic numbers (algebraic).
Int/Rational also carry a number-theory toolkit (factorization,
sqrt_mod, Jacobi/Legendre, CRT, random_prime, combinatorics,
continued-fraction approximation), and an optional num-traits bridge slots
the types into generic numeric code.
puremp is usable as a Rust library, a C library (the ffi feature; see
include/puremp.h), and a standalone command-line calculator (the cli
feature; the puremp binary).
This is a clean-room implementation: it is MIT-licensed and its algorithms
are drawn from the open literature, never from GMP/MPFR source. See
ROADMAP.md for the design, the algorithm references, and the milestone
plan.
§Example
use puremp::{Int, Rational};
// Arbitrary-precision integers.
let big = Int::from(2).pow(128);
assert_eq!(big.to_string(), "340282366920938463463374607431768211456");
assert_eq!(Int::from(1071).gcd(&Int::from(462)).to_string(), "21");
assert_eq!(Int::from(2).modpow(&Int::from(10), &Int::from(1000)).to_string(), "24");
// Exact rationals, always in lowest terms.
let third = Rational::new(Int::from(1), Int::from(3));
let sum = &(&third + &third) + &third;
assert_eq!(sum.to_string(), "1");§no_std
The crate is #![no_std] at its core. Arbitrary-precision types are
heap-backed, so they need the alloc crate; the alloc feature (implied by
every type layer) pulls it in. The std feature (enabled by default) adds
the pieces that genuinely need the operating system — the CLI, std::error
integration, and system I/O. Build with --no-default-features for a bare
no_std target.
Re-exports§
pub use error::Error;pub use error::Result;pub use int::Int;pub use int::Sign;pub use nat::Nat;pub use nat::Reciprocal;pub use nat::u_gcd;pub use nat::u64_gcd;pub use random::RandomSource;pub use inf_rational::InfRational;pub use rational::Rational;pub use mod_int::ModInt;pub use dyadic::Dyadic;pub use decimal::Decimal;pub use decimal::Rounding;pub use complex::Complex;pub use poly::Poly;pub use matrix::Matrix;pub use algebraic::Algebraic;pub use quadratic::Quadratic;pub use fixed_float::FixedFloat;pub use float::Float;pub use float::RoundingMode;pub use interval::Interval;
Modules§
- algebraic
- General real algebraic numbers.
- complex
- Generic complex numbers
re + im·i. - decimal
- Arbitrary-precision base-10 floating point.
- dyadic
- Exact dyadic rationals — numbers of the form
n · 2^-k, the rationals whose denominator is a power of two. - error
- Error and result types shared across the crate.
- fixed_
float - Fixed-precision floating point — a
mpfx-style convenience wrapper overFloat. - float
- Arbitrary-precision binary floating-point numbers.
- inf_
rational - Extended rationals with infinities — an exact
Rationalaugmented with+∞,-∞, andNaN, following the usual IEEE-style arithmetic (1/0 = +∞,-1/0 = -∞,0/0 = NaN,∞ − ∞ = NaN,∞ · 0 = NaN, …). - int
- Arbitrary-precision signed integers with small-value inlining.
- interval
- Interval arithmetic with outward rounding (verified computing).
- matrix
- Generic dense matrices
Matrix<T>. - mod_int
- Modular integers — residue classes
ℤ/mℤfor a fixed modulus. - nat
- Arbitrary-precision natural numbers (unsigned integers).
- poly
- Generic dense univariate polynomials
Poly<T>. - quadratic
- Quadratic irrationals — exact arithmetic in a field
ℚ(√d). - random
- Random-number generation for
NatandInt. - rational
- Arbitrary-precision rational numbers (exact
p/qfractions).
Constants§
- VERSION
- The crate version string (
CARGO_PKG_VERSION), exposed for the C ABI and CLI.