Crate numExperimental[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; use num::bigint::BigInt; use num::rational::{Ratio, BigRational}; fn approx_sqrt(number: u64, iterations: uint) -> BigRational { let start: Ratio<BigInt> = Ratio::from_integer(FromPrimitive::from_u64(number).unwrap()); let mut approx = start.clone(); for _ in range(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 }
extern crate num;

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

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

    for _ in range(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, BigRational};
pub use complex::Complex;
pub use integer::Integer;

Modules

bigint

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

complex

Complex numbers.

integer

Integer trait and functions

rational

Rational numbers