Merge some numeric traits with Real and don't re-export RealExt

The methods contained in `std::num::{Algebraic, Trigonometric, Exponential, Hyperbolic}` have now been moved into `std::num::Real`. This is part of an ongoing effort to simplify `std::num` (see issue #10387).

`std::num::RealExt` has also been removed from the prelude because it is not a commonly used trait.
This commit is contained in:
Brendan Zabarauskas 2014-01-09 15:29:09 +11:00
parent 9dece60391
commit 0232fed174
10 changed files with 425 additions and 469 deletions

View File

@ -11,7 +11,6 @@
//! Complex numbers.
use std::num::{Zero,One,ToStrRadix};
// FIXME #1284: handle complex NaN & infinity etc. This
@ -78,7 +77,7 @@ impl<T: Clone + Num> Cmplx<T> {
}
}
impl<T: Clone + Algebraic + Num> Cmplx<T> {
impl<T: Clone + Real> Cmplx<T> {
/// Calculate |self|
#[inline]
pub fn norm(&self) -> T {
@ -86,7 +85,7 @@ impl<T: Clone + Algebraic + Num> Cmplx<T> {
}
}
impl<T: Clone + Trigonometric + Algebraic + Num> Cmplx<T> {
impl<T: Clone + Real> Cmplx<T> {
/// Calculate the principal Arg of self.
#[inline]
pub fn arg(&self) -> T {

View File

@ -105,6 +105,12 @@ impl<T: Clone + Integer + Ord>
ret.reduce();
ret
}
/// Return the reciprocal
#[inline]
pub fn recip(&self) -> Ratio<T> {
Ratio::new_raw(self.denom.clone(), self.numer.clone())
}
}
impl Ratio<BigInt> {
@ -288,13 +294,6 @@ impl<T: Clone + Integer + Ord>
}
}
impl<T: Clone + Integer + Ord> Fractional for Ratio<T> {
#[inline]
fn recip(&self) -> Ratio<T> {
Ratio::new_raw(self.denom.clone(), self.numer.clone())
}
}
/* String conversions */
impl<T: ToStr> ToStr for Ratio<T> {
/// Renders as `numer/denom`.

View File

@ -347,146 +347,6 @@ impl Round for f32 {
fn fract(&self) -> f32 { *self - self.trunc() }
}
impl Fractional for f32 {
/// The reciprocal (multiplicative inverse) of the number
#[inline]
fn recip(&self) -> f32 { 1.0 / *self }
}
impl Algebraic for f32 {
#[inline]
fn pow(&self, n: &f32) -> f32 { pow(*self, *n) }
#[inline]
fn sqrt(&self) -> f32 { sqrt(*self) }
#[inline]
fn rsqrt(&self) -> f32 { self.sqrt().recip() }
#[inline]
fn cbrt(&self) -> f32 { cbrt(*self) }
#[inline]
fn hypot(&self, other: &f32) -> f32 { hypot(*self, *other) }
}
impl Trigonometric for f32 {
#[inline]
fn sin(&self) -> f32 { sin(*self) }
#[inline]
fn cos(&self) -> f32 { cos(*self) }
#[inline]
fn tan(&self) -> f32 { tan(*self) }
#[inline]
fn asin(&self) -> f32 { asin(*self) }
#[inline]
fn acos(&self) -> f32 { acos(*self) }
#[inline]
fn atan(&self) -> f32 { atan(*self) }
#[inline]
fn atan2(&self, other: &f32) -> f32 { atan2(*self, *other) }
/// Simultaneously computes the sine and cosine of the number
#[inline]
fn sin_cos(&self) -> (f32, f32) {
(self.sin(), self.cos())
}
}
impl Exponential for f32 {
/// Returns the exponential of the number
#[inline]
fn exp(&self) -> f32 { exp(*self) }
/// Returns 2 raised to the power of the number
#[inline]
fn exp2(&self) -> f32 { exp2(*self) }
/// Returns the natural logarithm of the number
#[inline]
fn ln(&self) -> f32 { ln(*self) }
/// Returns the logarithm of the number with respect to an arbitrary base
#[inline]
fn log(&self, base: &f32) -> f32 { self.ln() / base.ln() }
/// Returns the base 2 logarithm of the number
#[inline]
fn log2(&self) -> f32 { log2(*self) }
/// Returns the base 10 logarithm of the number
#[inline]
fn log10(&self) -> f32 { log10(*self) }
}
impl Hyperbolic for f32 {
#[inline]
fn sinh(&self) -> f32 { sinh(*self) }
#[inline]
fn cosh(&self) -> f32 { cosh(*self) }
#[inline]
fn tanh(&self) -> f32 { tanh(*self) }
///
/// Inverse hyperbolic sine
///
/// # Returns
///
/// - on success, the inverse hyperbolic sine of `self` will be returned
/// - `self` if `self` is `0.0`, `-0.0`, `INFINITY`, or `NEG_INFINITY`
/// - `NAN` if `self` is `NAN`
///
#[inline]
fn asinh(&self) -> f32 {
match *self {
NEG_INFINITY => NEG_INFINITY,
x => (x + ((x * x) + 1.0).sqrt()).ln(),
}
}
///
/// Inverse hyperbolic cosine
///
/// # Returns
///
/// - on success, the inverse hyperbolic cosine of `self` will be returned
/// - `INFINITY` if `self` is `INFINITY`
/// - `NAN` if `self` is `NAN` or `self < 1.0` (including `NEG_INFINITY`)
///
#[inline]
fn acosh(&self) -> f32 {
match *self {
x if x < 1.0 => Float::nan(),
x => (x + ((x * x) - 1.0).sqrt()).ln(),
}
}
///
/// Inverse hyperbolic tangent
///
/// # Returns
///
/// - on success, the inverse hyperbolic tangent of `self` will be returned
/// - `self` if `self` is `0.0` or `-0.0`
/// - `INFINITY` if `self` is `1.0`
/// - `NEG_INFINITY` if `self` is `-1.0`
/// - `NAN` if the `self` is `NAN` or outside the domain of `-1.0 <= self <= 1.0`
/// (including `INFINITY` and `NEG_INFINITY`)
///
#[inline]
fn atanh(&self) -> f32 {
0.5 * ((2.0 * *self) / (1.0 - *self)).ln_1p()
}
}
impl Real for f32 {
/// Archimedes' constant
#[inline]
@ -556,6 +416,136 @@ impl Real for f32 {
#[inline]
fn ln_10() -> f32 { 2.30258509299404568401799145468436421 }
/// The reciprocal (multiplicative inverse) of the number
#[inline]
fn recip(&self) -> f32 { 1.0 / *self }
#[inline]
fn pow(&self, n: &f32) -> f32 { pow(*self, *n) }
#[inline]
fn sqrt(&self) -> f32 { sqrt(*self) }
#[inline]
fn rsqrt(&self) -> f32 { self.sqrt().recip() }
#[inline]
fn cbrt(&self) -> f32 { cbrt(*self) }
#[inline]
fn hypot(&self, other: &f32) -> f32 { hypot(*self, *other) }
#[inline]
fn sin(&self) -> f32 { sin(*self) }
#[inline]
fn cos(&self) -> f32 { cos(*self) }
#[inline]
fn tan(&self) -> f32 { tan(*self) }
#[inline]
fn asin(&self) -> f32 { asin(*self) }
#[inline]
fn acos(&self) -> f32 { acos(*self) }
#[inline]
fn atan(&self) -> f32 { atan(*self) }
#[inline]
fn atan2(&self, other: &f32) -> f32 { atan2(*self, *other) }
/// Simultaneously computes the sine and cosine of the number
#[inline]
fn sin_cos(&self) -> (f32, f32) {
(self.sin(), self.cos())
}
/// Returns the exponential of the number
#[inline]
fn exp(&self) -> f32 { exp(*self) }
/// Returns 2 raised to the power of the number
#[inline]
fn exp2(&self) -> f32 { exp2(*self) }
/// Returns the natural logarithm of the number
#[inline]
fn ln(&self) -> f32 { ln(*self) }
/// Returns the logarithm of the number with respect to an arbitrary base
#[inline]
fn log(&self, base: &f32) -> f32 { self.ln() / base.ln() }
/// Returns the base 2 logarithm of the number
#[inline]
fn log2(&self) -> f32 { log2(*self) }
/// Returns the base 10 logarithm of the number
#[inline]
fn log10(&self) -> f32 { log10(*self) }
#[inline]
fn sinh(&self) -> f32 { sinh(*self) }
#[inline]
fn cosh(&self) -> f32 { cosh(*self) }
#[inline]
fn tanh(&self) -> f32 { tanh(*self) }
///
/// Inverse hyperbolic sine
///
/// # Returns
///
/// - on success, the inverse hyperbolic sine of `self` will be returned
/// - `self` if `self` is `0.0`, `-0.0`, `INFINITY`, or `NEG_INFINITY`
/// - `NAN` if `self` is `NAN`
///
#[inline]
fn asinh(&self) -> f32 {
match *self {
NEG_INFINITY => NEG_INFINITY,
x => (x + ((x * x) + 1.0).sqrt()).ln(),
}
}
///
/// Inverse hyperbolic cosine
///
/// # Returns
///
/// - on success, the inverse hyperbolic cosine of `self` will be returned
/// - `INFINITY` if `self` is `INFINITY`
/// - `NAN` if `self` is `NAN` or `self < 1.0` (including `NEG_INFINITY`)
///
#[inline]
fn acosh(&self) -> f32 {
match *self {
x if x < 1.0 => Float::nan(),
x => (x + ((x * x) - 1.0).sqrt()).ln(),
}
}
///
/// Inverse hyperbolic tangent
///
/// # Returns
///
/// - on success, the inverse hyperbolic tangent of `self` will be returned
/// - `self` if `self` is `0.0` or `-0.0`
/// - `INFINITY` if `self` is `1.0`
/// - `NEG_INFINITY` if `self` is `-1.0`
/// - `NAN` if the `self` is `NAN` or outside the domain of `-1.0 <= self <= 1.0`
/// (including `INFINITY` and `NEG_INFINITY`)
///
#[inline]
fn atanh(&self) -> f32 {
0.5 * ((2.0 * *self) / (1.0 - *self)).ln_1p()
}
/// Converts to degrees, assuming the number is in radians
#[inline]
fn to_degrees(&self) -> f32 { *self * (180.0f32 / Real::pi()) }

View File

@ -18,7 +18,7 @@ use cmath::c_double_utils;
use default::Default;
use libc::{c_double, c_int};
use num::{FPCategory, FPNaN, FPInfinite , FPZero, FPSubnormal, FPNormal};
use num::{Zero, One, strconv};
use num::{Zero, One, RealExt, strconv};
use num;
use to_str;
use unstable::intrinsics;
@ -365,146 +365,6 @@ impl Round for f64 {
fn fract(&self) -> f64 { *self - self.trunc() }
}
impl Fractional for f64 {
/// The reciprocal (multiplicative inverse) of the number
#[inline]
fn recip(&self) -> f64 { 1.0 / *self }
}
impl Algebraic for f64 {
#[inline]
fn pow(&self, n: &f64) -> f64 { pow(*self, *n) }
#[inline]
fn sqrt(&self) -> f64 { sqrt(*self) }
#[inline]
fn rsqrt(&self) -> f64 { self.sqrt().recip() }
#[inline]
fn cbrt(&self) -> f64 { cbrt(*self) }
#[inline]
fn hypot(&self, other: &f64) -> f64 { hypot(*self, *other) }
}
impl Trigonometric for f64 {
#[inline]
fn sin(&self) -> f64 { sin(*self) }
#[inline]
fn cos(&self) -> f64 { cos(*self) }
#[inline]
fn tan(&self) -> f64 { tan(*self) }
#[inline]
fn asin(&self) -> f64 { asin(*self) }
#[inline]
fn acos(&self) -> f64 { acos(*self) }
#[inline]
fn atan(&self) -> f64 { atan(*self) }
#[inline]
fn atan2(&self, other: &f64) -> f64 { atan2(*self, *other) }
/// Simultaneously computes the sine and cosine of the number
#[inline]
fn sin_cos(&self) -> (f64, f64) {
(self.sin(), self.cos())
}
}
impl Exponential for f64 {
/// Returns the exponential of the number
#[inline]
fn exp(&self) -> f64 { exp(*self) }
/// Returns 2 raised to the power of the number
#[inline]
fn exp2(&self) -> f64 { exp2(*self) }
/// Returns the natural logarithm of the number
#[inline]
fn ln(&self) -> f64 { ln(*self) }
/// Returns the logarithm of the number with respect to an arbitrary base
#[inline]
fn log(&self, base: &f64) -> f64 { self.ln() / base.ln() }
/// Returns the base 2 logarithm of the number
#[inline]
fn log2(&self) -> f64 { log2(*self) }
/// Returns the base 10 logarithm of the number
#[inline]
fn log10(&self) -> f64 { log10(*self) }
}
impl Hyperbolic for f64 {
#[inline]
fn sinh(&self) -> f64 { sinh(*self) }
#[inline]
fn cosh(&self) -> f64 { cosh(*self) }
#[inline]
fn tanh(&self) -> f64 { tanh(*self) }
///
/// Inverse hyperbolic sine
///
/// # Returns
///
/// - on success, the inverse hyperbolic sine of `self` will be returned
/// - `self` if `self` is `0.0`, `-0.0`, `INFINITY`, or `NEG_INFINITY`
/// - `NAN` if `self` is `NAN`
///
#[inline]
fn asinh(&self) -> f64 {
match *self {
NEG_INFINITY => NEG_INFINITY,
x => (x + ((x * x) + 1.0).sqrt()).ln(),
}
}
///
/// Inverse hyperbolic cosine
///
/// # Returns
///
/// - on success, the inverse hyperbolic cosine of `self` will be returned
/// - `INFINITY` if `self` is `INFINITY`
/// - `NAN` if `self` is `NAN` or `self < 1.0` (including `NEG_INFINITY`)
///
#[inline]
fn acosh(&self) -> f64 {
match *self {
x if x < 1.0 => Float::nan(),
x => (x + ((x * x) - 1.0).sqrt()).ln(),
}
}
///
/// Inverse hyperbolic tangent
///
/// # Returns
///
/// - on success, the inverse hyperbolic tangent of `self` will be returned
/// - `self` if `self` is `0.0` or `-0.0`
/// - `INFINITY` if `self` is `1.0`
/// - `NEG_INFINITY` if `self` is `-1.0`
/// - `NAN` if the `self` is `NAN` or outside the domain of `-1.0 <= self <= 1.0`
/// (including `INFINITY` and `NEG_INFINITY`)
///
#[inline]
fn atanh(&self) -> f64 {
0.5 * ((2.0 * *self) / (1.0 - *self)).ln_1p()
}
}
impl Real for f64 {
/// Archimedes' constant
#[inline]
@ -574,6 +434,136 @@ impl Real for f64 {
#[inline]
fn ln_10() -> f64 { 2.30258509299404568401799145468436421 }
/// The reciprocal (multiplicative inverse) of the number
#[inline]
fn recip(&self) -> f64 { 1.0 / *self }
#[inline]
fn pow(&self, n: &f64) -> f64 { pow(*self, *n) }
#[inline]
fn sqrt(&self) -> f64 { sqrt(*self) }
#[inline]
fn rsqrt(&self) -> f64 { self.sqrt().recip() }
#[inline]
fn cbrt(&self) -> f64 { cbrt(*self) }
#[inline]
fn hypot(&self, other: &f64) -> f64 { hypot(*self, *other) }
#[inline]
fn sin(&self) -> f64 { sin(*self) }
#[inline]
fn cos(&self) -> f64 { cos(*self) }
#[inline]
fn tan(&self) -> f64 { tan(*self) }
#[inline]
fn asin(&self) -> f64 { asin(*self) }
#[inline]
fn acos(&self) -> f64 { acos(*self) }
#[inline]
fn atan(&self) -> f64 { atan(*self) }
#[inline]
fn atan2(&self, other: &f64) -> f64 { atan2(*self, *other) }
/// Simultaneously computes the sine and cosine of the number
#[inline]
fn sin_cos(&self) -> (f64, f64) {
(self.sin(), self.cos())
}
/// Returns the exponential of the number
#[inline]
fn exp(&self) -> f64 { exp(*self) }
/// Returns 2 raised to the power of the number
#[inline]
fn exp2(&self) -> f64 { exp2(*self) }
/// Returns the natural logarithm of the number
#[inline]
fn ln(&self) -> f64 { ln(*self) }
/// Returns the logarithm of the number with respect to an arbitrary base
#[inline]
fn log(&self, base: &f64) -> f64 { self.ln() / base.ln() }
/// Returns the base 2 logarithm of the number
#[inline]
fn log2(&self) -> f64 { log2(*self) }
/// Returns the base 10 logarithm of the number
#[inline]
fn log10(&self) -> f64 { log10(*self) }
#[inline]
fn sinh(&self) -> f64 { sinh(*self) }
#[inline]
fn cosh(&self) -> f64 { cosh(*self) }
#[inline]
fn tanh(&self) -> f64 { tanh(*self) }
///
/// Inverse hyperbolic sine
///
/// # Returns
///
/// - on success, the inverse hyperbolic sine of `self` will be returned
/// - `self` if `self` is `0.0`, `-0.0`, `INFINITY`, or `NEG_INFINITY`
/// - `NAN` if `self` is `NAN`
///
#[inline]
fn asinh(&self) -> f64 {
match *self {
NEG_INFINITY => NEG_INFINITY,
x => (x + ((x * x) + 1.0).sqrt()).ln(),
}
}
///
/// Inverse hyperbolic cosine
///
/// # Returns
///
/// - on success, the inverse hyperbolic cosine of `self` will be returned
/// - `INFINITY` if `self` is `INFINITY`
/// - `NAN` if `self` is `NAN` or `self < 1.0` (including `NEG_INFINITY`)
///
#[inline]
fn acosh(&self) -> f64 {
match *self {
x if x < 1.0 => Float::nan(),
x => (x + ((x * x) - 1.0).sqrt()).ln(),
}
}
///
/// Inverse hyperbolic tangent
///
/// # Returns
///
/// - on success, the inverse hyperbolic tangent of `self` will be returned
/// - `self` if `self` is `0.0` or `-0.0`
/// - `INFINITY` if `self` is `1.0`
/// - `NEG_INFINITY` if `self` is `-1.0`
/// - `NAN` if the `self` is `NAN` or outside the domain of `-1.0 <= self <= 1.0`
/// (including `INFINITY` and `NEG_INFINITY`)
///
#[inline]
fn atanh(&self) -> f64 {
0.5 * ((2.0 * *self) / (1.0 - *self)).ln_1p()
}
/// Converts to degrees, assuming the number is in radians
#[inline]
fn to_degrees(&self) -> f64 { *self * (180.0f64 / Real::pi()) }

View File

@ -195,179 +195,11 @@ pub trait Round {
fn fract(&self) -> Self;
}
/// Trait for common fractional operations.
pub trait Fractional: Num
+ Orderable
+ Round
+ Div<Self,Self> {
/// Take the reciprocal (inverse) of a number, `1/x`.
fn recip(&self) -> Self;
}
/// A collection of algebraic operations.
pub trait Algebraic {
/// Raise a number to a power.
fn pow(&self, n: &Self) -> Self;
/// Take the square root of a number.
fn sqrt(&self) -> Self;
/// Take the reciprocal (inverse) square root of a number, `1/sqrt(x)`.
fn rsqrt(&self) -> Self;
/// Take the cubic root of a number.
fn cbrt(&self) -> Self;
/// Calculate the length of the hypotenuse of a right-angle triangle given
/// legs of length `x` and `y`.
fn hypot(&self, other: &Self) -> Self;
}
/// Raise a number to a power.
///
/// # Example
///
/// ```rust
/// use std::num;
///
/// let sixteen: f64 = num::pow(2.0, 4.0);
/// assert_eq!(sixteen, 16.0);
/// ```
#[inline(always)] pub fn pow<T: Algebraic>(value: T, n: T) -> T { value.pow(&n) }
/// Take the square root of a number.
#[inline(always)] pub fn sqrt<T: Algebraic>(value: T) -> T { value.sqrt() }
/// Take the reciprocal (inverse) square root of a number, `1/sqrt(x)`.
#[inline(always)] pub fn rsqrt<T: Algebraic>(value: T) -> T { value.rsqrt() }
/// Take the cubic root of a number.
#[inline(always)] pub fn cbrt<T: Algebraic>(value: T) -> T { value.cbrt() }
/// Calculate the length of the hypotenuse of a right-angle triangle given legs of length `x` and
/// `y`.
#[inline(always)] pub fn hypot<T: Algebraic>(x: T, y: T) -> T { x.hypot(&y) }
/// A trait for trigonometric functions.
pub trait Trigonometric {
/// Computes the sine of a number (in radians).
fn sin(&self) -> Self;
/// Computes the cosine of a number (in radians).
fn cos(&self) -> Self;
/// Computes the tangent of a number (in radians).
fn tan(&self) -> Self;
/// Computes the arcsine of a number. Return value is in radians in
/// the range [-pi/2, pi/2] or NaN if the number is outside the range
/// [-1, 1].
fn asin(&self) -> Self;
/// Computes the arccosine of a number. Return value is in radians in
/// the range [0, pi] or NaN if the number is outside the range
/// [-1, 1].
fn acos(&self) -> Self;
/// Computes the arctangent of a number. Return value is in radians in the
/// range [-pi/2, pi/2];
fn atan(&self) -> Self;
/// Computes the four quadrant arctangent of a number, `y`, and another
/// number `x`. Return value is in radians in the range [-pi, pi];
///
/// # Example
///
/// ```rust
/// use std::f32;
///
/// let y = 3f32.sqrt();
/// let x = 1f32;
/// assert_approx_eq!(y.atan2(&x), f32::consts::PI / 3f32);
/// assert_approx_eq!((-y).atan2(&(-x)), - 2f32 * f32::consts::PI / 3f32);
/// ```
fn atan2(&self, other: &Self) -> Self;
/// Simultaneously computes the sine and cosine of the number, `x`. Returns
/// `(sin(x), cos(x))`.
fn sin_cos(&self) -> (Self, Self);
}
/// Sine function.
#[inline(always)] pub fn sin<T: Trigonometric>(value: T) -> T { value.sin() }
/// Cosine function.
#[inline(always)] pub fn cos<T: Trigonometric>(value: T) -> T { value.cos() }
/// Tangent function.
#[inline(always)] pub fn tan<T: Trigonometric>(value: T) -> T { value.tan() }
/// Compute the arcsine of the number.
#[inline(always)] pub fn asin<T: Trigonometric>(value: T) -> T { value.asin() }
/// Compute the arccosine of the number.
#[inline(always)] pub fn acos<T: Trigonometric>(value: T) -> T { value.acos() }
/// Compute the arctangent of the number.
#[inline(always)] pub fn atan<T: Trigonometric>(value: T) -> T { value.atan() }
/// Compute the arctangent with 2 arguments.
#[inline(always)] pub fn atan2<T: Trigonometric>(x: T, y: T) -> T { x.atan2(&y) }
/// Simultaneously computes the sine and cosine of the number.
#[inline(always)] pub fn sin_cos<T: Trigonometric>(value: T) -> (T, T) { value.sin_cos() }
/// A trait exponential functions.
pub trait Exponential {
/// Returns `e^(self)`, (the exponential function).
fn exp(&self) -> Self;
/// Returns 2 raised to the power of the number, `2^(self)`.
fn exp2(&self) -> Self;
/// Returns the natural logarithm of the number.
fn ln(&self) -> Self;
/// Returns the logarithm of the number with respect to an arbitrary base.
fn log(&self, base: &Self) -> Self;
/// Returns the base 2 logarithm of the number.
fn log2(&self) -> Self;
/// Returns the base 10 logarithm of the number.
fn log10(&self) -> Self;
}
/// Returns `e^(value)`, (the exponential function).
#[inline(always)] pub fn exp<T: Exponential>(value: T) -> T { value.exp() }
/// Returns 2 raised to the power of the number, `2^(value)`.
#[inline(always)] pub fn exp2<T: Exponential>(value: T) -> T { value.exp2() }
/// Returns the natural logarithm of the number.
#[inline(always)] pub fn ln<T: Exponential>(value: T) -> T { value.ln() }
/// Returns the logarithm of the number with respect to an arbitrary base.
#[inline(always)] pub fn log<T: Exponential>(value: T, base: T) -> T { value.log(&base) }
/// Returns the base 2 logarithm of the number.
#[inline(always)] pub fn log2<T: Exponential>(value: T) -> T { value.log2() }
/// Returns the base 10 logarithm of the number.
#[inline(always)] pub fn log10<T: Exponential>(value: T) -> T { value.log10() }
/// A trait hyperbolic functions.
pub trait Hyperbolic: Exponential {
/// Hyperbolic sine function.
fn sinh(&self) -> Self;
/// Hyperbolic cosine function.
fn cosh(&self) -> Self;
/// Hyperbolic tangent function.
fn tanh(&self) -> Self;
/// Inverse hyperbolic sine function.
fn asinh(&self) -> Self;
/// Inverse hyperbolic cosine function.
fn acosh(&self) -> Self;
/// Inverse hyperbolic tangent function.
fn atanh(&self) -> Self;
}
/// Hyperbolic sine function.
#[inline(always)] pub fn sinh<T: Hyperbolic>(value: T) -> T { value.sinh() }
/// Hyperbolic cosine function.
#[inline(always)] pub fn cosh<T: Hyperbolic>(value: T) -> T { value.cosh() }
/// Hyperbolic tangent function.
#[inline(always)] pub fn tanh<T: Hyperbolic>(value: T) -> T { value.tanh() }
/// Inverse hyperbolic sine function.
#[inline(always)] pub fn asinh<T: Hyperbolic>(value: T) -> T { value.asinh() }
/// Inverse hyperbolic cosine function.
#[inline(always)] pub fn acosh<T: Hyperbolic>(value: T) -> T { value.acosh() }
/// Inverse hyperbolic tangent function.
#[inline(always)] pub fn atanh<T: Hyperbolic>(value: T) -> T { value.atanh() }
/// Defines constants and methods common to real numbers
pub trait Real: Signed
+ Fractional
+ Algebraic
+ Trigonometric
+ Hyperbolic {
+ Orderable
+ Round
+ Div<Self,Self> {
// Common Constants
// FIXME (#5527): These should be associated constants
fn pi() -> Self;
@ -388,6 +220,93 @@ pub trait Real: Signed
fn ln_2() -> Self;
fn ln_10() -> Self;
// Fractional functions
/// Take the reciprocal (inverse) of a number, `1/x`.
fn recip(&self) -> Self;
// Algebraic functions
/// Raise a number to a power.
fn pow(&self, n: &Self) -> Self;
/// Take the square root of a number.
fn sqrt(&self) -> Self;
/// Take the reciprocal (inverse) square root of a number, `1/sqrt(x)`.
fn rsqrt(&self) -> Self;
/// Take the cubic root of a number.
fn cbrt(&self) -> Self;
/// Calculate the length of the hypotenuse of a right-angle triangle given
/// legs of length `x` and `y`.
fn hypot(&self, other: &Self) -> Self;
// Trigonometric functions
/// Computes the sine of a number (in radians).
fn sin(&self) -> Self;
/// Computes the cosine of a number (in radians).
fn cos(&self) -> Self;
/// Computes the tangent of a number (in radians).
fn tan(&self) -> Self;
/// Computes the arcsine of a number. Return value is in radians in
/// the range [-pi/2, pi/2] or NaN if the number is outside the range
/// [-1, 1].
fn asin(&self) -> Self;
/// Computes the arccosine of a number. Return value is in radians in
/// the range [0, pi] or NaN if the number is outside the range
/// [-1, 1].
fn acos(&self) -> Self;
/// Computes the arctangent of a number. Return value is in radians in the
/// range [-pi/2, pi/2];
fn atan(&self) -> Self;
/// Computes the four quadrant arctangent of a number, `y`, and another
/// number `x`. Return value is in radians in the range [-pi, pi];
///
/// # Example
///
/// ```rust
/// use std::f32;
///
/// let y = 3f32.sqrt();
/// let x = 1f32;
/// assert_approx_eq!(y.atan2(&x), f32::consts::PI / 3f32);
/// assert_approx_eq!((-y).atan2(&(-x)), - 2f32 * f32::consts::PI / 3f32);
/// ```
fn atan2(&self, other: &Self) -> Self;
/// Simultaneously computes the sine and cosine of the number, `x`. Returns
/// `(sin(x), cos(x))`.
fn sin_cos(&self) -> (Self, Self);
// Exponential functions
/// Returns `e^(self)`, (the exponential function).
fn exp(&self) -> Self;
/// Returns 2 raised to the power of the number, `2^(self)`.
fn exp2(&self) -> Self;
/// Returns the natural logarithm of the number.
fn ln(&self) -> Self;
/// Returns the logarithm of the number with respect to an arbitrary base.
fn log(&self, base: &Self) -> Self;
/// Returns the base 2 logarithm of the number.
fn log2(&self) -> Self;
/// Returns the base 10 logarithm of the number.
fn log10(&self) -> Self;
// Hyperbolic functions
/// Hyperbolic sine function.
fn sinh(&self) -> Self;
/// Hyperbolic cosine function.
fn cosh(&self) -> Self;
/// Hyperbolic tangent function.
fn tanh(&self) -> Self;
/// Inverse hyperbolic sine function.
fn asinh(&self) -> Self;
/// Inverse hyperbolic cosine function.
fn acosh(&self) -> Self;
/// Inverse hyperbolic tangent function.
fn atanh(&self) -> Self;
// Angular conversions
/// Convert radians to degrees.
@ -396,6 +315,67 @@ pub trait Real: Signed
fn to_radians(&self) -> Self;
}
/// Raise a number to a power.
///
/// # Example
///
/// ```rust
/// use std::num;
///
/// let sixteen: f64 = num::pow(2.0, 4.0);
/// assert_eq!(sixteen, 16.0);
/// ```
#[inline(always)] pub fn pow<T: Real>(value: T, n: T) -> T { value.pow(&n) }
/// Take the square root of a number.
#[inline(always)] pub fn sqrt<T: Real>(value: T) -> T { value.sqrt() }
/// Take the reciprocal (inverse) square root of a number, `1/sqrt(x)`.
#[inline(always)] pub fn rsqrt<T: Real>(value: T) -> T { value.rsqrt() }
/// Take the cubic root of a number.
#[inline(always)] pub fn cbrt<T: Real>(value: T) -> T { value.cbrt() }
/// Calculate the length of the hypotenuse of a right-angle triangle given legs of length `x` and
/// `y`.
#[inline(always)] pub fn hypot<T: Real>(x: T, y: T) -> T { x.hypot(&y) }
/// Sine function.
#[inline(always)] pub fn sin<T: Real>(value: T) -> T { value.sin() }
/// Cosine function.
#[inline(always)] pub fn cos<T: Real>(value: T) -> T { value.cos() }
/// Tangent function.
#[inline(always)] pub fn tan<T: Real>(value: T) -> T { value.tan() }
/// Compute the arcsine of the number.
#[inline(always)] pub fn asin<T: Real>(value: T) -> T { value.asin() }
/// Compute the arccosine of the number.
#[inline(always)] pub fn acos<T: Real>(value: T) -> T { value.acos() }
/// Compute the arctangent of the number.
#[inline(always)] pub fn atan<T: Real>(value: T) -> T { value.atan() }
/// Compute the arctangent with 2 arguments.
#[inline(always)] pub fn atan2<T: Real>(x: T, y: T) -> T { x.atan2(&y) }
/// Simultaneously computes the sine and cosine of the number.
#[inline(always)] pub fn sin_cos<T: Real>(value: T) -> (T, T) { value.sin_cos() }
/// Returns `e^(value)`, (the exponential function).
#[inline(always)] pub fn exp<T: Real>(value: T) -> T { value.exp() }
/// Returns 2 raised to the power of the number, `2^(value)`.
#[inline(always)] pub fn exp2<T: Real>(value: T) -> T { value.exp2() }
/// Returns the natural logarithm of the number.
#[inline(always)] pub fn ln<T: Real>(value: T) -> T { value.ln() }
/// Returns the logarithm of the number with respect to an arbitrary base.
#[inline(always)] pub fn log<T: Real>(value: T, base: T) -> T { value.log(&base) }
/// Returns the base 2 logarithm of the number.
#[inline(always)] pub fn log2<T: Real>(value: T) -> T { value.log2() }
/// Returns the base 10 logarithm of the number.
#[inline(always)] pub fn log10<T: Real>(value: T) -> T { value.log10() }
/// Hyperbolic sine function.
#[inline(always)] pub fn sinh<T: Real>(value: T) -> T { value.sinh() }
/// Hyperbolic cosine function.
#[inline(always)] pub fn cosh<T: Real>(value: T) -> T { value.cosh() }
/// Hyperbolic tangent function.
#[inline(always)] pub fn tanh<T: Real>(value: T) -> T { value.tanh() }
/// Inverse hyperbolic sine function.
#[inline(always)] pub fn asinh<T: Real>(value: T) -> T { value.asinh() }
/// Inverse hyperbolic cosine function.
#[inline(always)] pub fn acosh<T: Real>(value: T) -> T { value.acosh() }
/// Inverse hyperbolic tangent function.
#[inline(always)] pub fn atanh<T: Real>(value: T) -> T { value.atanh() }
/// Methods that are harder to implement and not commonly used.
pub trait RealExt: Real {
// FIXME (#5527): usages of `int` should be replaced with an associated

View File

@ -59,9 +59,7 @@ pub use iter::{FromIterator, Extendable};
pub use iter::{Iterator, DoubleEndedIterator, RandomAccessIterator, CloneableIterator};
pub use iter::{OrdIterator, MutableDoubleEndedIterator, ExactSize};
pub use num::Times;
pub use num::{Algebraic, Trigonometric, Exponential, Hyperbolic};
pub use num::{Bitwise, BitCount, Bounded};
pub use num::{Integer, Fractional, Real, RealExt};
pub use num::{Bitwise, BitCount, Bounded, Integer, Real};
pub use num::{Num, NumCast, CheckedAdd, CheckedSub, CheckedMul};
pub use num::{Orderable, Signed, Unsigned, Round};
pub use num::{Primitive, Int, Float, ToStrRadix, ToPrimitive, FromPrimitive};

View File

@ -10,7 +10,7 @@
//! The exponential distribution.
use num::Exponential;
use num::Real;
use rand::{Rng, Rand};
use rand::distributions::{ziggurat, ziggurat_tables, Sample, IndependentSample};

View File

@ -10,7 +10,7 @@
//! The Gamma and derived distributions.
use num::Algebraic;
use num::Real;
use num;
use rand::{Rng, Open01};
use super::normal::StandardNormal;

View File

@ -10,7 +10,7 @@
//! The normal and derived distributions.
use num::Exponential;
use num::Real;
use rand::{Rng, Rand, Open01};
use rand::distributions::{ziggurat, ziggurat_tables, Sample, IndependentSample};

View File

@ -31,7 +31,7 @@
use clone::Clone;
use kinds::Send;
use num::{Exponential,Algebraic,Round};
use num::{Real, Round};
use option::{Option, Some, None};
use sync::arc::UnsafeArc;
use sync::atomics::{AtomicUint,Relaxed,Release,Acquire};