Skip to main content

Crate malachite

Crate malachite 

Source
Expand description

Malachite is an arbitrary-precision arithmetic library for Rust, with efficient algorithms partially derived from GMP, FLINT, and MPFR.

This is the main Malachite crate, and the recommended way to use the library. It re-exports the contents of the more specialized crates that make up Malachite, so that depending on malachite alone is enough:

  • Natural and Integer, arbitrary-precision unsigned and signed integers, are re-exported from malachite-nz.
  • Rational, arbitrary-precision rational numbers, is re-exported from malachite-q. (Requires the rationals feature, which is enabled by default.)
  • Float, arbitrary-precision floating-point numbers, is re-exported from malachite-float. (Requires the floats feature; floats are currently experimental.)
  • The base module re-exports malachite-base, which provides the numeric traits (and their implementations for primitive types) used throughout Malachite, along with tools for generating values for tests and benchmarks.

Most conversion and arithmetic operations are expressed as traits that live in base, so a common pattern is to import them from there, for example use malachite::base::num::arithmetic::traits::Pow;.

§Examples

use malachite::base::num::arithmetic::traits::Pow;
use malachite::{Integer, Natural, Rational};

// Arbitrary-precision unsigned integers, far larger than any primitive type:
assert_eq!(Natural::from(2u32).pow(128), Natural::from(1u32) << 128u64);

// Signed integers:
assert_eq!(-Integer::from(5) * Integer::from(5), Integer::from(-25));

// Exact rational arithmetic: 1/3 + 1/6 = 1/2.
assert_eq!(
    Rational::from_unsigneds(1u32, 3u32) + Rational::from_unsigneds(1u32, 6u32),
    Rational::from_unsigneds(1u32, 2u32)
);

For documentation of individual items, follow the re-export links below.

Modules§

base
This module contains various functions that support the other crates. This includes many numeric traits and their implementation for primitive numeric types, as well as many functions for exhaustively and randomly generating values of many types.
integer
Integer, a type representing integers with arbitrarily large absolute values.
natural
Natural, a type representing arbitrarily large non-negative integers.
platform
Various types and constants dependent on whether Malachite is built using 32-bit limbs or 64-bit limbs. Limb is the type such that Vecs of limbs are used to represent the bits of a Natural.
rational
Rational, a type representing rational numbers with arbitrarily large numerators and denominators.

Structs§

Integer
An integer.
Natural
A natural (non-negative) integer.
Rational
A rational number.