| 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 | //! # #[cfg (any(feature = "alloc" , feature = "std" ))] |
| 24 | //! # mod test { |
| 25 | //! |
| 26 | //! use num::FromPrimitive; |
| 27 | //! use num::bigint::BigInt; |
| 28 | //! use num::rational::{Ratio, BigRational}; |
| 29 | //! |
| 30 | //! # pub |
| 31 | //! fn approx_sqrt(number: u64, iterations: usize) -> BigRational { |
| 32 | //! let start: Ratio<BigInt> = Ratio::from_integer(FromPrimitive::from_u64(number).unwrap()); |
| 33 | //! let mut approx = start.clone(); |
| 34 | //! |
| 35 | //! for _ in 0..iterations { |
| 36 | //! approx = (&approx + (&start / &approx)) / |
| 37 | //! Ratio::from_integer(FromPrimitive::from_u64(2).unwrap()); |
| 38 | //! } |
| 39 | //! |
| 40 | //! approx |
| 41 | //! } |
| 42 | //! # } |
| 43 | //! # #[cfg (not(any(feature = "alloc" , feature = "std" )))] |
| 44 | //! # mod test { pub fn approx_sqrt(n: u64, _: usize) -> u64 { n } } |
| 45 | //! # use crate::test::approx_sqrt; |
| 46 | //! |
| 47 | //! fn main() { |
| 48 | //! println!("{}" , approx_sqrt(10, 4)); // prints 4057691201/1283082416 |
| 49 | //! } |
| 50 | //! |
| 51 | //! ``` |
| 52 | //! |
| 53 | //! [newt]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method |
| 54 | //! |
| 55 | //! ## Compatibility |
| 56 | //! |
| 57 | //! The `num` crate is tested for rustc 1.60 and greater. |
| 58 | |
| 59 | #![doc (html_root_url = "https://docs.rs/num/0.4" )] |
| 60 | #![no_std ] |
| 61 | |
| 62 | #[cfg (any(feature = "alloc" , feature = "std" ))] |
| 63 | pub use num_bigint::{BigInt, BigUint}; |
| 64 | |
| 65 | pub use num_complex::Complex; |
| 66 | |
| 67 | #[cfg (any(feature = "alloc" , feature = "std" ))] |
| 68 | pub use num_rational::BigRational; |
| 69 | #[allow (deprecated)] |
| 70 | pub use num_rational::Rational; |
| 71 | pub use num_rational::{Rational32, Rational64}; |
| 72 | |
| 73 | pub use num_integer::Integer; |
| 74 | |
| 75 | pub use num_iter::{range, range_inclusive, range_step, range_step_inclusive}; |
| 76 | |
| 77 | #[cfg (any(feature = "libm" , feature = "std" ))] |
| 78 | pub use num_traits::Float; |
| 79 | pub use num_traits::{ |
| 80 | abs, abs_sub, cast, checked_pow, clamp, one, pow, signum, zero, Bounded, CheckedAdd, |
| 81 | CheckedDiv, CheckedMul, CheckedSub, FromPrimitive, Num, NumCast, One, PrimInt, Saturating, |
| 82 | Signed, ToPrimitive, Unsigned, Zero, |
| 83 | }; |
| 84 | |
| 85 | #[cfg (any(feature = "alloc" , feature = "std" ))] |
| 86 | pub mod bigint { |
| 87 | pub use num_bigint::*; |
| 88 | } |
| 89 | |
| 90 | pub mod complex { |
| 91 | pub use num_complex::*; |
| 92 | } |
| 93 | |
| 94 | pub mod integer { |
| 95 | pub use num_integer::*; |
| 96 | } |
| 97 | |
| 98 | pub mod iter { |
| 99 | pub use num_iter::*; |
| 100 | } |
| 101 | |
| 102 | pub mod traits { |
| 103 | pub use num_traits::*; |
| 104 | } |
| 105 | |
| 106 | pub mod rational { |
| 107 | pub use num_rational::*; |
| 108 | } |
| 109 | |