View on GitHub


An arbitrary-precision arithmetic library for Rust.


Malachite is an arbitrary-precision arithmetic library for Rust. It achieves high performance in part by using algorithms derived from GMP and FLINT.

use malachite::num::arithmetic::traits::Factorial;
use malachite::Natural;

fn main() {
    println!("{}", Natural::factorial(100));

The code above outputs the following:


You have to scroll to see the entire output.

Here’s a more complex example, calculating the negative-one-millionth power of 3 and displaying the result with 30 digits of precision.

use malachite::num::arithmetic::traits::Pow;
use malachite::num::conversion::string::options::ToSciOptions;
use malachite::num::conversion::traits::ToSci;
use malachite::Rational;

fn main() {
    let mut options = ToSciOptions::default();
    println!("{}", Rational::from(3).pow(-1_000_000i64).to_sci_with_options(options));

The output is this:


Every digit is correct, except that the least-significant digit was rounded up from 2. The default rounding mode, Nearest, uses bankers’ rounding, but you may specify different rounding behavior via the options parameter.

Malachite is designed to work with very large numbers efficiently. See here for a performance comparison against other libraries.

To use Malachite, add the following to your project’s Cargo.toml file:

version = "0.3.0"

By default, all of Malachite’s features are included, but you can opt out of some of them. For example, if you want to use Natural and Integer but not Rational, you can instead use

version = "0.3.0"
default-features = false
features = [ "naturals_and_integers" ]

The malachite crate re-exports three sub-crates.

Malachite is under active development, with many more types and features planned for the future. Nonetheless, it is extensively tested and documented, and ready for use today. Just be aware that its API is not stable yet, and that it is licensed under LGPL 3.0.


How is “Malachite” pronounced, and what does it mean? “Malachite” is pronounced MA-luh-kite, or /ˈmæl.əˌkaɪt/. It is the name of a green gemstone. Unfortunately, malachite does not contain iron, which would have made it a particularly good namesake for a Rust library.

Malachite’s logo is an image of a snub cube.

When does Malachite allocate memory? Any Natural less than \(2^{64}\) is represented inline, without allocating memory. Any Integer whose absolute value is less than \(2^{64}\) doesn’t allocate either, and neither does any Rational whose absolute numerator and denominator are both less than \(2^{64}\). If you’re using a build with --features 32_bit_limbs, then the threshold is \(2^{32}\) instead.

Can I build Malachite for WebAssembly? Yes. If, in the future, Malachite includes code incompatible with Wasm (for example, code that uses rayon), it will be possible to disable that code with cargo flags.

Blog Posts

Copyright © 2022 Mikhail Hogrefe