https://github.com/declanvk/reckoner
Simple arbitrary precision integer and rational arithmetic library
https://github.com/declanvk/reckoner
arbitrary-precision arithmetic ffi-bindings
Last synced: 10 months ago
JSON representation
Simple arbitrary precision integer and rational arithmetic library
- Host: GitHub
- URL: https://github.com/declanvk/reckoner
- Owner: declanvk
- License: mit
- Created: 2019-10-27T22:40:09.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2024-12-09T18:23:46.000Z (about 1 year ago)
- Last Synced: 2025-03-11T01:57:21.889Z (11 months ago)
- Topics: arbitrary-precision, arithmetic, ffi-bindings
- Language: Rust
- Homepage: https://declanvk.github.io/reckoner
- Size: 5.11 MB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Reckoner
A high level arbitrary precision integer and rational arithmetic library wrapping [`imath`](https://github.com/creachadair/imath/).
## Example
The following example computes an approximation of pi using the [Newton / Euler Convergence Transformation](https://en.wikipedia.org/wiki/Approximations_of_%CF%80#Other_classical_formulae).
````rust
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::()
}
````
See [`examples/`](https://github.com/declanvk/reckoner/tree/main/examples) for more.
## Crates
The MSRV for both crates is `1.70.0`.
### `reckoner`
[](https://crates.io/crates/reckoner) [](https://docs.rs/reckoner)
A high level arbitrary precision arithmetic library supporting integer and rational numbers.
### `creachadair-imath-sys`
[](https://crates.io/crates/creachadair-imath-sys) [](https://docs.rs/creachadair-imath-sys)
FFI bindings for [`imath`](https://github.com/creachadair/imath/).
## Documentation
[Documentation for `reckoner` from `main` branch](https://declanvk.github.io/reckoner/reckoner/index.html)
[Documentation for `creachadair-imath-sys` from `main` branch](https://declanvk.github.io/reckoner/creachadair_imath_sys/index.html)
## Contributing
Download the crate using the command
```bash
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`.