#rational-numbers #high-precision #integer-arithmetic #integer #rational #numbers

reckoner

A high level arbitrary precision arithmetic library supporting integer and rational numbers

5 unstable releases

0.3.0 Oct 7, 2024
0.2.0 Oct 25, 2022
0.1.2 Apr 18, 2021
0.1.1 Apr 18, 2021
0.1.0 Jan 14, 2020

#82 in Math

Download history 1/week @ 2024-09-18 2/week @ 2024-09-25 164/week @ 2024-10-02 44/week @ 2024-10-09 3/week @ 2024-10-16

211 downloads per month

MIT license

1.5MB
16K SLoC

Rust 10K SLoC // 0.2% comments C 5K SLoC // 0.2% comments Python 1K SLoC // 0.2% comments Scheme 134 SLoC // 0.1% comments Shell 78 SLoC // 0.1% comments

Reckoner

A high level arbitrary precision integer and rational arithmetic library wrapping imath.

Example

The following example computes an approximation of pi using the Newton / Euler Convergence Transformation.

use reckoner::{Integer, Rational};

fn factorial(v: &Integer) -> Integer {
    let mut accum = 1.into();
    let mut f = v.clone();

    while f > 0 {
        accum *= &f;
        f -= 1;
    }

    accum
}

// Product of all odd integer up to the given value.
fn odd_factorial(v: &Integer) -> Integer {
    let mut accum = 1.into();
    let mut f = if v % 2 == 0 { v - 1 } else { v.clone() };

    while f > 0 {
        accum *= &f;
        f -= 2;
    }

    accum
}

// ```
// \frac{\pi}{2}
//     = \sum_{k=0}^\infty\frac{k!}{(2k+1)!!}
//     = \sum_{k=0}^{\infty} \cfrac {2^k k!^2}{(2k + 1)!}
//     = 1+\frac{1}{3}\left(1+\frac{2}{5}\left(1+\frac{3}{7}\left(1+\cdots\right)\right)\right)
// ```
fn compute_pi_approx(iterations: u32) -> Rational {
    2 * (0..iterations)
        .map(Integer::from)
        .map(|n| {
            let numerator = factorial(&n);
            let denominator = odd_factorial(&(2 * n + 1));

            (numerator, denominator).into()
        })
        .sum::<Rational>()
}

See examples/ for more.

Crates

The MSRV for both crates is 1.70.0.

reckoner

crates.io docs.rs

A high level arbitrary precision arithmetic library supporting integer and rational numbers.

creachadair-imath-sys

crates.io docs.rs

FFI bindings for imath.

Documentation

Documentation for reckoner from main branch

Documentation for creachadair-imath-sys from main branch

Contributing

Download the crate using the command

git clone --recurse-submodules https://github.com/declanvk/reckoner

so that you also get the submodule sources, which are required to compile the creachadair-imath-sys crate. If you already cloned the project and forgot --recurse-submodules, you can combine the git submodule init and git submodule update steps by running git submodule update --init.

Dependencies

~0–2.6MB
~43K SLoC