Skip to content

Commit e30198d

Browse files
committed
num: expand crate documentation + add example
1 parent 91fa8e5 commit e30198d

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/libnum/lib.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,40 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
//! Simple numerics.
12+
//!
13+
//! This crate contains arbitrary-sized integer, rational, and complex types.
14+
//!
15+
//! ## Example
16+
//!
17+
//! This example uses the BigRational type and [Newton's method][newt] to
18+
//! approximate a square root to arbitrary precision:
19+
//!
20+
//! ```
21+
//! extern crate num;
22+
//!
23+
//! use num::bigint::BigInt;
24+
//! use num::rational::{Ratio, BigRational}:
25+
//!
26+
//! fn approx_sqrt(number: u64, iterations: uint) -> BigRational {
27+
//! let start: Ratio<BigInt> = Ratio::from_integer(FromPrimitive::from_u64(number).unwrap());
28+
//! let mut approx = start.clone();
29+
//!
30+
//! for _ in range(0, iterations) {
31+
//! approx = (approx + (start / approx)) /
32+
//! Ratio::from_integer(FromPrimitive::from_u64(2).unwrap());
33+
//! }
34+
//!
35+
//! approx
36+
//! }
37+
//!
38+
//! fn main() {
39+
//! println!("{}", approx_sqrt(10, 4)); // prints 4057691201/1283082416
40+
//! }
41+
//! ```
42+
//!
43+
//! [newt]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method
44+
1145
#![feature(macro_rules)]
1246

1347
#![crate_id = "num#0.11.0-pre"]

0 commit comments

Comments
 (0)