Dashu 0.3: Libraries of arbitrary precision integers, floats ... and now rationals!
Hi everyone, I'm excited to announce that the big number library suite dashu has released a new version - v0.3. This time a library for arbitrary precision rational numbers has been added, called dashu-ratio. For arbitrary precision integer and float numbers, please see the docs of dashu-int and dashu-float, or refer to my previous announcement.
dashu-ratio: Arbitrary precision rationals
dashu-ratio is a new library crated based on dashu-int, it has two major types RBig and Relaxed. The Gcd is not always removed from the numerator and denominator in latter type, which might lead to better performance in some cases.
One interesting feature of RBig is that it supports estimating a bigger RBig number using a smaller one with smaller denominator. The following example will demonstrate some usages:
use dashu_ratio::RBig;
let pi = 3.1415926f32;
// Get the exact representation of this float number
let exact = RBig::try_from(pi).unwrap(); // = 6588397 / 2097152 (lossless)
// Get a simplest form of pi considering the precision of f32
let est1 = RBig::simplest_from_f32(pi).unwrap(); // = 51808 / 16491
// Get a nearest fraction around pi, with denominator less than 10
let est2 = RBig::nearest(exact, 10u8.into()).value(); // = 22 / 7
// Get a nearest fraction less than pi, with denominator less than 10
let est3 = RBig::next_down(exact, 10u8.into()); // = 25 / 8
This library also has a good efficiency. The following result is a comparison of different library calculating the same sequence of rational numbers. (See here for the benchmark).
dashu/fib 2000 time: [10.735 ms 10.968 ms 11.256 ms]
num-rational/fib 2000 time: [76.441 ms 77.611 ms 78.921 ms]
malachite/fib 2000 time: [11.298 ms 11.471 ms 11.666 ms]
rug/fib 2000 time: [1.3117 ms 1.3436 ms 1.3803 ms]
Besides, rational numbers are also supported by dashu-macros. You can create rational numbers with literals using dashu-macro::rbig, even in a const context!.
Hope you enjoy this update, and for the next releases: v0.4 will mainly focus on improving docs & tests, creating a user guide, and complete benchmarks; v0.5 will hopefully bring the complex numbers into the suite, with some necessary changes in the dashu-float crate. Thank you!