#mathematics #numerics #rational #arbitrary-precision #bigrat

no-std dashu-ratio

A big rational library with good performance

5 releases

0.4.1 Jan 12, 2024
0.4.0 Sep 12, 2023
0.3.2 Mar 13, 2023
0.3.1 Dec 8, 2022
0.3.0 Nov 10, 2022

#1075 in Math

Download history 345/week @ 2023-10-28 1117/week @ 2023-11-04 1095/week @ 2023-11-11 955/week @ 2023-11-18 929/week @ 2023-11-25 915/week @ 2023-12-02 983/week @ 2023-12-09 936/week @ 2023-12-16 333/week @ 2023-12-23 679/week @ 2023-12-30 693/week @ 2024-01-06 466/week @ 2024-01-13 762/week @ 2024-01-20 717/week @ 2024-01-27 697/week @ 2024-02-03 569/week @ 2024-02-10

2,825 downloads per month
Used in 11 crates (2 directly)

MIT/Apache

1MB
23K SLoC

dashu-ratio

Arbitrary precision rational implementation as a part of the dashu library. See Docs.rs for the full documentation.

Features

  • Supports no_std and written in pure Rust.
  • Support a relaxed verion of rational numbers for fast computation.
  • Support for Diophantine Approximation of floating point numbers.
  • Rational numbers with small numerators and denominators are inlined on stack.
  • Efficient integer parsing and printing with base 2~36.
  • Developer friendly debug printing for float numbers.

Optional dependencies

  • std (default): enable std support for dependencies.

Performance

Relevant benchmark will be implemented in the built-in benchmark.

License

See the top-level readme.


lib.rs:

A big rational library with good performance.

The library implements efficient arithmetic and conversion functions in pure Rust.

The two main rational types are [RBig] and [Relaxed]. Both of them represent the rational number as a pair of integers (numerator and denominator) and their APIs are mostly the same. However only with [RBig], the numerator and denominator are reduced so that they don't have common divisors other than one. Therefore, [Relaxed] sometimes can be much faster if you don't care about a reduced representation of the rational number. However, benchmarking is always recommended before choosing which representation to use.

To construct big rationals from literals, please use the dashu-macro crate for your convenience.

Examples

use dashu_int::{IBig, UBig};
use dashu_ratio::{RBig, Relaxed};

let a = RBig::from_parts((-12).into(), 34u8.into());
let b = RBig::from_str_radix("-azz/ep", 36).unwrap();
let c = RBig::try_from(3.1415926f32).unwrap(); // c = 6588397 / 2097152 (lossless)
let c2 = RBig::simplest_from_f32(3.1415926).unwrap(); // c2 = 51808 / 16491
assert_eq!(c2.numerator(), &IBig::from(51808));

assert_eq!(c.to_string(), "6588397/2097152");
let d = RBig::simplest_from_f32(22./7.).unwrap();
assert_eq!(d.to_string(), "22/7"); // round trip to the original literal

// for Relaxed, only the common divisor 2 is removed
let e: Relaxed = "-3228/1224".parse()?; // d = -807 / 306
assert_eq!(e.numerator(), &IBig::from(-807));
let f: RBig = e.clone().canonicalize(); // e = -269 / 102
assert_eq!(f.numerator(), &IBig::from(-269));

Dependencies

~280–580KB
~12K SLoC