Crate num

source · []
Expand description

A collection of numeric types and traits for Rust.

This includes new types for big integers, rationals, and complex numbers, new traits for generic programming on numeric properties like Integer, and generic range iterators.

Example

This example uses the BigRational type and Newton’s method to approximate a square root to arbitrary precision:


use num::FromPrimitive;
use num::bigint::BigInt;
use num::rational::{Ratio, BigRational};

fn approx_sqrt(number: u64, iterations: usize) -> BigRational {
    let start: Ratio<BigInt> = Ratio::from_integer(FromPrimitive::from_u64(number).unwrap());
    let mut approx = start.clone();

    for _ in 0..iterations {
        approx = (&approx + (&start / &approx)) /
            Ratio::from_integer(FromPrimitive::from_u64(2).unwrap());
    }

    approx
}

fn main() {
    println!("{}", approx_sqrt(10, 4)); // prints 4057691201/1283082416
}

Compatibility

The num crate is tested for rustc 1.31 and greater.

Modules

Structs

A big signed integer type.

A big unsigned integer type.

A complex number in Cartesian form.

Traits

Numbers which have upper and lower bounds

Performs addition that returns None instead of wrapping around on overflow.

Performs division that returns None instead of panicking on division by zero and instead of wrapping around on underflow and overflow.

Performs multiplication that returns None instead of wrapping around on underflow or overflow.

Performs subtraction that returns None instead of wrapping around on underflow.

Generic trait for floating point numbers

A generic trait for converting a number to a value.

The base trait for numeric types, covering 0 and 1 values, comparisons, basic numeric operations, and string conversion.

An interface for casting between machine scalars.

Defines a multiplicative identity element for Self.

Generic trait for primitive integers.

Saturating math operations. Deprecated, use SaturatingAdd, SaturatingSub and SaturatingMul instead.

Useful functions for signed numbers (i.e. numbers that can be negative).

A generic trait for converting a value to a number.

A trait for values which cannot be negative

Defines an additive identity element for Self.

Functions

Computes the absolute value.

The positive difference of two numbers.

Cast from one machine scalar to another.

Raises a value to the power of exp, returning None if an overflow occurred.

A value bounded by a minimum and a maximum

Returns the multiplicative identity, 1.

Raises a value to the power of exp, using exponentiation by squaring.

Returns an iterator over the given range [start, stop) (that is, starting at start (inclusive), and ending at stop (exclusive)).

Return an iterator over the range [start, stop]

Return an iterator over the range [start, stop) by step. It handles overflow by stopping.

Return an iterator over the range [start, stop] by step. It handles overflow by stopping.

Returns the sign of the number.

Returns the additive identity, 0.

Type Definitions

Alias for arbitrary precision rationals.

RationalDeprecated

Alias for a Ratio of machine-sized integers.

Alias for a Ratio of 32-bit-sized integers.

Alias for a Ratio of 64-bit-sized integers.