num/
lib.rs

1// Copyright 2014-2016 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11//! A collection of numeric types and traits for Rust.
12//!
13//! This includes new types for big integers, rationals, and complex numbers,
14//! new traits for generic programming on numeric properties like `Integer`,
15//! and generic range iterators.
16//!
17//! ## Example
18//!
19//! This example uses the BigRational type and [Newton's method][newt] to
20//! approximate a square root to arbitrary precision:
21//!
22//! ```
23//! extern crate num;
24//! # #[cfg(feature = "std")]
25//! # mod test {
26//!
27//! use num::FromPrimitive;
28//! use num::bigint::BigInt;
29//! use num::rational::{Ratio, BigRational};
30//!
31//! # pub
32//! fn approx_sqrt(number: u64, iterations: usize) -> BigRational {
33//!     let start: Ratio<BigInt> = Ratio::from_integer(FromPrimitive::from_u64(number).unwrap());
34//!     let mut approx = start.clone();
35//!
36//!     for _ in 0..iterations {
37//!         approx = (&approx + (&start / &approx)) /
38//!             Ratio::from_integer(FromPrimitive::from_u64(2).unwrap());
39//!     }
40//!
41//!     approx
42//! }
43//! # }
44//! # #[cfg(not(feature = "std"))]
45//! # mod test { pub fn approx_sqrt(n: u64, _: usize) -> u64 { n } }
46//! # use test::approx_sqrt;
47//!
48//! fn main() {
49//!     println!("{}", approx_sqrt(10, 4)); // prints 4057691201/1283082416
50//! }
51//!
52//! ```
53//!
54//! [newt]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method
55//!
56//! ## Compatibility
57//!
58//! The `num` crate is tested for rustc 1.15 and greater.
59
60#![doc(html_root_url = "https://docs.rs/num/0.2")]
61#![no_std]
62
63#[cfg(feature = "std")]
64extern crate num_bigint;
65extern crate num_complex;
66extern crate num_integer;
67extern crate num_iter;
68extern crate num_rational;
69extern crate num_traits;
70
71#[cfg(feature = "std")]
72pub use num_bigint::{BigInt, BigUint};
73
74pub use num_complex::Complex;
75
76#[cfg(feature = "std")]
77pub use num_rational::BigRational;
78pub use num_rational::Rational;
79
80pub use num_integer::Integer;
81
82pub use num_iter::{range, range_inclusive, range_step, range_step_inclusive};
83
84#[cfg(feature = "std")]
85pub use num_traits::Float;
86pub use num_traits::{
87    abs, abs_sub, cast, checked_pow, clamp, one, pow, signum, zero, Bounded, CheckedAdd,
88    CheckedDiv, CheckedMul, CheckedSub, FromPrimitive, Num, NumCast, One, PrimInt, Saturating,
89    Signed, ToPrimitive, Unsigned, Zero,
90};
91
92#[cfg(feature = "std")]
93pub mod bigint {
94    pub use num_bigint::*;
95}
96
97pub mod complex {
98    pub use num_complex::*;
99}
100
101pub mod integer {
102    pub use num_integer::*;
103}
104
105pub mod iter {
106    pub use num_iter::*;
107}
108
109pub mod traits {
110    pub use num_traits::*;
111}
112
113pub mod rational {
114    pub use num_rational::*;
115}