Skip to main content

Crate puremp

Crate puremp 

Source
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:

  1. Integers — unsigned Nat and signed Int, the workhorse layer that carries the hard limb-level algorithms (multiplication, division, GCD, modular arithmetic, …). Enabled by the int feature.
  2. RationalsRational, exact p/q fractions kept in lowest terms; plus InfRational, the same extended with ±∞/NaN. rational feature.
  3. DyadicsDyadic, exact n·2^-k binary fractions. dyadic feature.
  4. FloatsFloat, binary floating-point with a caller-chosen precision and directed RoundingMode, aiming at MPFR-class correct rounding, plus FixedFloat, a fixed-precision wrapper with operators. float feature.
  5. DecimalsDecimal, exact base-10 floating point (Python Decimal-style), with directed rounding. decimal feature.

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 over Float.
float
Arbitrary-precision binary floating-point numbers.
inf_rational
Extended rationals with infinities — an exact Rational augmented with +∞, -∞, and NaN, 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 Nat and Int.
rational
Arbitrary-precision rational numbers (exact p/q fractions).

Constants§

VERSION
The crate version string (CARGO_PKG_VERSION), exposed for the C ABI and CLI.