Crate num [] [src]

Simple numerics.

This crate contains arbitrary-sized integer, rational, and complex types.

Example

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

extern crate num; #[cfg(all(feature = "bigint", feature="rational"))] pub mod test { 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 } } #[cfg(not(all(feature = "bigint", feature="rational")))] fn approx_sqrt(n: u64, _: usize) -> u64 { n } fn main() { println!("{}", approx_sqrt(10, 4)); // prints 4057691201/1283082416 }
extern crate num;

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
}

Reexports

pub use bigint::{BigInt, BigUint};
pub use rational::Rational;
pub use rational::BigRational;
pub use complex::Complex;
pub use integer::Integer;
pub use iter::{range, range_inclusive, range_step, range_step_inclusive};
pub use traits::{Num, Zero, One, Signed, Unsigned, Bounded, Saturating, CheckedAdd, CheckedSub, CheckedMul, CheckedDiv, PrimInt, Float, ToPrimitive, FromPrimitive, NumCast, cast};

Modules

bigint

A Big integer (signed version: BigInt, unsigned version: BigUint).

complex

Complex numbers.

integer

Integer trait and functions.

iter

External iterators for generic mathematics

rational

Rational numbers

traits

Numeric traits for generic mathematics

Functions

abs

Computes the absolute value.

abs_sub

The positive difference of two numbers.

one

Returns the multiplicative identity, 1.

pow

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

signum

Returns the sign of the number.

zero

Returns the additive identity, 0.