about summary refs log tree commit diff
path: root/src/libnum
diff options
context:
space:
mode:
authorCorey Richardson <corey@octayn.net>2014-04-05 01:08:01 -0400
committerCorey Richardson <corey@octayn.net>2014-05-16 09:55:29 -0700
commite30198d9d4d4d016949625062bde002bc33aa574 (patch)
tree3e5cb96313f38b36fe7a1d7ada058705ff339976 /src/libnum
parent91fa8e5f2aaad4620928c8669218971da747e4da (diff)
downloadrust-e30198d9d4d4d016949625062bde002bc33aa574.tar.gz
rust-e30198d9d4d4d016949625062bde002bc33aa574.zip
num: expand crate documentation + add example
Diffstat (limited to 'src/libnum')
-rw-r--r--src/libnum/lib.rs34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/libnum/lib.rs b/src/libnum/lib.rs
index 20d694d0d09..54652c9dca3 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<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
+//! }
+//! ```
+//!
+//! [newt]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method
+
 #![feature(macro_rules)]
 
 #![crate_id = "num#0.11.0-pre"]