num 0.1.36

A collection of numeric types and traits for Rust, including bigint, complex, rational, range iterators, generic integers, and more!
Documentation

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:

extern crate num;
# #[cfg(all(feature = "bigint", feature="rational"))]
# mod test {

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

# pub
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")))]
# mod test { pub fn approx_sqrt(n: u64, _: usize) -> u64 { n } }
# use test::approx_sqrt;

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