2016-02-03 05:36:01 +00:00
|
|
|
// Copyright 2014-2016 The Rust Project Developers. See the COPYRIGHT
|
2014-09-16 17:35:35 +00:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-11-22 00:39:00 +00:00
|
|
|
//! A collection of numeric types and traits for Rust.
|
2014-09-16 17:35:35 +00:00
|
|
|
//!
|
2015-11-22 00:39:00 +00:00
|
|
|
//! This includes new types for big integers, rationals, and complex numbers,
|
|
|
|
//! new traits for generic programming on numeric properties like `Integer`,
|
|
|
|
//! and generic range iterators.
|
2014-09-16 17:35:35 +00:00
|
|
|
//!
|
|
|
|
//! ## Example
|
|
|
|
//!
|
|
|
|
//! This example uses the BigRational type and [Newton's method][newt] to
|
|
|
|
//! approximate a square root to arbitrary precision:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! extern crate num;
|
2015-06-02 12:34:22 +00:00
|
|
|
//! # #[cfg(all(feature = "bigint", feature="rational"))]
|
2015-12-21 20:21:47 +00:00
|
|
|
//! # mod test {
|
2014-09-16 17:35:35 +00:00
|
|
|
//!
|
2015-04-03 17:26:37 +00:00
|
|
|
//! use num::FromPrimitive;
|
2014-09-16 17:35:35 +00:00
|
|
|
//! use num::bigint::BigInt;
|
|
|
|
//! use num::rational::{Ratio, BigRational};
|
|
|
|
//!
|
2015-12-21 20:21:47 +00:00
|
|
|
//! # pub
|
2015-01-09 11:36:03 +00:00
|
|
|
//! fn approx_sqrt(number: u64, iterations: usize) -> BigRational {
|
2014-09-16 17:35:35 +00:00
|
|
|
//! let start: Ratio<BigInt> = Ratio::from_integer(FromPrimitive::from_u64(number).unwrap());
|
|
|
|
//! let mut approx = start.clone();
|
|
|
|
//!
|
2015-03-22 17:43:11 +00:00
|
|
|
//! for _ in 0..iterations {
|
2014-12-16 14:38:35 +00:00
|
|
|
//! approx = (&approx + (&start / &approx)) /
|
2014-09-16 17:35:35 +00:00
|
|
|
//! Ratio::from_integer(FromPrimitive::from_u64(2).unwrap());
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! approx
|
|
|
|
//! }
|
2015-06-02 12:34:22 +00:00
|
|
|
//! # }
|
|
|
|
//! # #[cfg(not(all(feature = "bigint", feature="rational")))]
|
2015-12-21 20:21:47 +00:00
|
|
|
//! # mod test { pub fn approx_sqrt(n: u64, _: usize) -> u64 { n } }
|
|
|
|
//! # use test::approx_sqrt;
|
2014-09-16 17:35:35 +00:00
|
|
|
//!
|
|
|
|
//! fn main() {
|
|
|
|
//! println!("{}", approx_sqrt(10, 4)); // prints 4057691201/1283082416
|
|
|
|
//! }
|
2015-06-02 12:34:22 +00:00
|
|
|
//!
|
2014-09-16 17:35:35 +00:00
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! [newt]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method
|
2015-11-13 02:40:13 +00:00
|
|
|
#![doc(html_logo_url = "http://rust-num.github.io/num/rust-logo-128x128-blk-v2.png",
|
|
|
|
html_favicon_url = "http://rust-num.github.io/num/favicon.ico",
|
|
|
|
html_root_url = "http://rust-num.github.io/num/",
|
2014-09-16 17:35:35 +00:00
|
|
|
html_playground_url = "http://play.rust-lang.org/")]
|
|
|
|
|
2016-03-11 00:05:40 +00:00
|
|
|
pub extern crate num_traits;
|
|
|
|
pub extern crate num_integer;
|
|
|
|
pub extern crate num_iter;
|
|
|
|
#[cfg(feature = "num-complex")]
|
|
|
|
pub extern crate num_complex;
|
2016-03-04 11:32:44 +00:00
|
|
|
#[cfg(feature = "num-bigint")]
|
2016-03-11 00:05:40 +00:00
|
|
|
pub extern crate num_bigint;
|
2016-03-04 11:32:44 +00:00
|
|
|
#[cfg(feature = "num-rational")]
|
2016-03-11 00:05:40 +00:00
|
|
|
pub extern crate num_rational;
|
2016-02-15 23:19:23 +00:00
|
|
|
|
2015-06-02 12:34:22 +00:00
|
|
|
#[cfg(feature = "rustc-serialize")]
|
2015-03-26 00:48:52 +00:00
|
|
|
extern crate rustc_serialize;
|
2016-01-08 02:30:50 +00:00
|
|
|
|
|
|
|
// Some of the tests of non-RNG-based functionality are randomized using the
|
|
|
|
// RNG-based functionality, so the RNG-based functionality needs to be enabled
|
|
|
|
// for tests.
|
|
|
|
#[cfg(any(feature = "rand", all(feature = "bigint", test)))]
|
2015-02-05 16:20:16 +00:00
|
|
|
extern crate rand;
|
2014-09-16 17:35:35 +00:00
|
|
|
|
2016-02-27 08:07:32 +00:00
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
extern crate serde;
|
|
|
|
|
2016-03-04 11:32:44 +00:00
|
|
|
#[cfg(feature = "num-bigint")]
|
2016-03-25 23:22:02 +00:00
|
|
|
pub use num_bigint::{BigInt, BigUint};
|
2016-03-04 11:32:44 +00:00
|
|
|
#[cfg(feature = "num-rational")]
|
2016-03-25 23:22:02 +00:00
|
|
|
pub use num_rational::Rational;
|
2016-03-04 11:32:44 +00:00
|
|
|
#[cfg(all(feature = "num-rational", feature="num-bigint"))]
|
2016-03-25 23:22:02 +00:00
|
|
|
pub use num_rational::BigRational;
|
2016-03-11 00:05:40 +00:00
|
|
|
#[cfg(feature = "num-complex")]
|
2016-03-25 23:22:02 +00:00
|
|
|
pub use num_complex::Complex;
|
|
|
|
pub use num_integer::Integer;
|
|
|
|
pub use num_iter::{range, range_inclusive, range_step, range_step_inclusive};
|
|
|
|
pub use num_traits::{Num, Zero, One, Signed, Unsigned, Bounded,
|
|
|
|
Saturating, CheckedAdd, CheckedSub, CheckedMul, CheckedDiv,
|
|
|
|
PrimInt, Float, ToPrimitive, FromPrimitive, NumCast, cast};
|
2014-09-16 17:35:35 +00:00
|
|
|
|
2015-01-03 18:30:05 +00:00
|
|
|
use std::ops::{Mul};
|
|
|
|
|
2016-03-04 11:32:44 +00:00
|
|
|
#[cfg(feature = "num-bigint")]
|
|
|
|
pub use num_bigint as bigint;
|
2016-03-10 23:55:53 +00:00
|
|
|
pub use num_complex as complex;
|
2016-03-11 00:05:40 +00:00
|
|
|
pub use num_integer as integer;
|
|
|
|
pub use num_iter as iter;
|
2016-03-04 11:32:44 +00:00
|
|
|
pub use num_traits as traits;
|
|
|
|
#[cfg(feature = "num-rational")]
|
|
|
|
pub use num_rational as rational;
|
2014-11-15 03:30:48 +00:00
|
|
|
|
|
|
|
/// Returns the additive identity, `0`.
|
|
|
|
#[inline(always)] pub fn zero<T: Zero>() -> T { Zero::zero() }
|
|
|
|
|
|
|
|
/// Returns the multiplicative identity, `1`.
|
|
|
|
#[inline(always)] pub fn one<T: One>() -> T { One::one() }
|
|
|
|
|
|
|
|
/// Computes the absolute value.
|
|
|
|
///
|
|
|
|
/// For `f32` and `f64`, `NaN` will be returned if the number is `NaN`
|
|
|
|
///
|
|
|
|
/// For signed integers, `::MIN` will be returned if the number is `::MIN`.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn abs<T: Signed>(value: T) -> T {
|
|
|
|
value.abs()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The positive difference of two numbers.
|
|
|
|
///
|
|
|
|
/// Returns zero if `x` is less than or equal to `y`, otherwise the difference
|
|
|
|
/// between `x` and `y` is returned.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn abs_sub<T: Signed>(x: T, y: T) -> T {
|
|
|
|
x.abs_sub(&y)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the sign of the number.
|
|
|
|
///
|
|
|
|
/// For `f32` and `f64`:
|
|
|
|
///
|
|
|
|
/// * `1.0` if the number is positive, `+0.0` or `INFINITY`
|
|
|
|
/// * `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
|
|
|
|
/// * `NaN` if the number is `NaN`
|
|
|
|
///
|
|
|
|
/// For signed integers:
|
|
|
|
///
|
|
|
|
/// * `0` if the number is zero
|
|
|
|
/// * `1` if the number is positive
|
|
|
|
/// * `-1` if the number is negative
|
|
|
|
#[inline(always)] pub fn signum<T: Signed>(value: T) -> T { value.signum() }
|
|
|
|
|
|
|
|
/// Raises a value to the power of exp, using exponentiation by squaring.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use num;
|
|
|
|
///
|
2015-03-07 22:12:50 +00:00
|
|
|
/// assert_eq!(num::pow(2i8, 4), 16);
|
|
|
|
/// assert_eq!(num::pow(6u8, 3), 216);
|
2014-11-15 03:30:48 +00:00
|
|
|
/// ```
|
|
|
|
#[inline]
|
2015-01-09 11:36:03 +00:00
|
|
|
pub fn pow<T: Clone + One + Mul<T, Output = T>>(mut base: T, mut exp: usize) -> T {
|
2016-01-07 02:27:50 +00:00
|
|
|
if exp == 0 { return T::one() }
|
2015-03-07 22:12:50 +00:00
|
|
|
|
2016-01-07 02:27:50 +00:00
|
|
|
while exp & 1 == 0 {
|
|
|
|
base = base.clone() * base;
|
|
|
|
exp >>= 1;
|
|
|
|
}
|
|
|
|
if exp == 1 { return base }
|
|
|
|
|
|
|
|
let mut acc = base.clone();
|
|
|
|
while exp > 1 {
|
|
|
|
exp >>= 1;
|
|
|
|
base = base.clone() * base;
|
|
|
|
if exp & 1 == 1 {
|
|
|
|
acc = acc * base.clone();
|
2014-11-15 03:30:48 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-07 02:27:50 +00:00
|
|
|
acc
|
2014-11-15 03:30:48 +00:00
|
|
|
}
|
2015-01-09 11:36:03 +00:00
|
|
|
|
2016-02-06 00:04:34 +00:00
|
|
|
/// Raises a value to the power of exp, returning `None` if an overflow occurred.
|
2016-02-03 05:36:01 +00:00
|
|
|
///
|
|
|
|
/// Otherwise same as the `pow` function.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use num;
|
|
|
|
///
|
|
|
|
/// assert_eq!(num::checked_pow(2i8, 4), Some(16));
|
|
|
|
/// assert_eq!(num::checked_pow(7i8, 8), None);
|
|
|
|
/// assert_eq!(num::checked_pow(7u32, 8), Some(5_764_801));
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn checked_pow<T: Clone + One + CheckedMul>(mut base: T, mut exp: usize) -> Option<T> {
|
|
|
|
if exp == 0 { return Some(T::one()) }
|
|
|
|
|
|
|
|
macro_rules! optry {
|
|
|
|
( $ expr : expr ) => {
|
|
|
|
if let Some(val) = $expr { val } else { return None }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while exp & 1 == 0 {
|
|
|
|
base = optry!(base.checked_mul(&base));
|
|
|
|
exp >>= 1;
|
|
|
|
}
|
|
|
|
if exp == 1 { return Some(base) }
|
|
|
|
|
|
|
|
let mut acc = base.clone();
|
|
|
|
while exp > 1 {
|
|
|
|
exp >>= 1;
|
|
|
|
base = optry!(base.checked_mul(&base));
|
|
|
|
if exp & 1 == 1 {
|
|
|
|
acc = optry!(acc.checked_mul(&base));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(acc)
|
|
|
|
}
|