#pi #numerical

bin+lib compute-pi

A Rust library designed for computing pi to millions of digits

6 releases (1 stable)

1.0.0 Aug 24, 2024
0.2.5 Mar 21, 2024
0.1.3 Mar 15, 2024

#755 in Math

MIT and LGPL-3.0+

9KB
72 lines

compute-pi

Compute-pi calculates the value of pi to an arbitrary number of digits using the Gauss–Legendre algorithm. It computes 1 million digits within a couple of seconds on your PC.

Installation

You can include this crate in your Cargo.toml file as follows:

[dependencies]
compute-pi = "1.0"

Usage

To use the compute_pi_str function in your Rust code, add the following to your crate root:

use compute_pi::compute_pi_str;

fn main() {
    // Specify the number of digits of Pi you want to compute
    let digits = 100;

    // Compute Pi
    let pi = compute_pi_str(digits);

    // Print calculated decimal
    println!("Pi to {} decimal places: {}", digits, pi);
}

Command Line Usage

You can also use the compute-pi command from the command line. After installing the crate with

cargo install compute-pi

run the following command:

compute-pi <digits>

Replace <digits> with the number of digits of Pi you want to compute. For example:

compute-pi 100

This will print the value of Pi to 100 decimal places.

Performance

On a MacBook Air (Apple M1, 16 GB), pi to 1 million digits was computed in 1.5 seconds, and to 320 million digits in 24 minutes, but the calculation did not complete within 10 hours for 330 million digits. Similarly, on a Mac mini (Apple M1, 16 GB), pi to 320 million digits was computed, but the calculation did not complete within 10 hours for 330 million digits. It is presumed that the calculation is taking a long time due to memory swapping, as it does not end in a panic due to memory allocation failure but instead continues indefinitely. Since both machines yielded the same result, it is considered that 320 million digits is the maximum number of digits that can be computed using compute-pi with 16 GB of memory. Another limitation is that the digit cannot exceed 1,292,913,983 due to the precision of rug::Float being defined as u32. The computed result of pi to 320 million digits using compute-pi was confirmed to match exactly with the results obtained using y-cruncher and the Chudnovsky algorithm.

License

This crate is licensed under the MIT license.

Dependencies

~2.5MB
~47K SLoC