From e30198d9d4d4d016949625062bde002bc33aa574 Mon Sep 17 00:00:00 2001 From: Corey Richardson Date: Sat, 5 Apr 2014 01:08:01 -0400 Subject: [PATCH] num: expand crate documentation + add example --- src/libnum/lib.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/libnum/lib.rs b/src/libnum/lib.rs index 20d694d0d093..54652c9dca33 100644 --- a/src/libnum/lib.rs +++ b/src/libnum/lib.rs @@ -8,6 +8,40 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +//! Simple numerics. +//! +//! This crate contains arbitrary-sized integer, rational, and complex types. +//! +//! ## Example +//! +//! This example uses the BigRational type and [Newton's method][newt] 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 = 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 +//! } +//! ``` +//! +//! [newt]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method + #![feature(macro_rules)] #![crate_id = "num#0.11.0-pre"]