diff options
Diffstat (limited to 'src/libstd/num/mod.rs')
| -rw-r--r-- | src/libstd/num/mod.rs | 206 |
1 files changed, 93 insertions, 113 deletions
diff --git a/src/libstd/num/mod.rs b/src/libstd/num/mod.rs index d66d13657fc..7959ee79016 100644 --- a/src/libstd/num/mod.rs +++ b/src/libstd/num/mod.rs @@ -195,17 +195,38 @@ pub trait Round { fn fract(&self) -> Self; } -/// Trait for common fractional operations. -pub trait Fractional: Num - + Orderable - + Round - + Div<Self,Self> { +/// Defines constants and methods common to real numbers +pub trait Real: Signed + + Orderable + + Round + + Div<Self,Self> { + // Common Constants + // FIXME (#5527): These should be associated constants + fn pi() -> Self; + fn two_pi() -> Self; + fn frac_pi_2() -> Self; + fn frac_pi_3() -> Self; + fn frac_pi_4() -> Self; + fn frac_pi_6() -> Self; + fn frac_pi_8() -> Self; + fn frac_1_pi() -> Self; + fn frac_2_pi() -> Self; + fn frac_2_sqrtpi() -> Self; + fn sqrt2() -> Self; + fn frac_1_sqrt2() -> Self; + fn e() -> Self; + fn log2_e() -> Self; + fn log10_e() -> Self; + fn ln_2() -> Self; + fn ln_10() -> Self; + + // Fractional functions + /// Take the reciprocal (inverse) of a number, `1/x`. fn recip(&self) -> Self; -} -/// A collection of algebraic operations. -pub trait Algebraic { + // Algebraic functions + /// Raise a number to a power. fn pow(&self, n: &Self) -> Self; /// Take the square root of a number. @@ -217,31 +238,9 @@ pub trait Algebraic { /// 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) } + // Trigonometric functions -/// 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). @@ -260,7 +259,6 @@ pub trait Trigonometric { /// 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]; /// @@ -275,38 +273,16 @@ pub trait Trigonometric { /// 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() } + // Exponential functions -/// 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. @@ -315,78 +291,21 @@ pub trait Exponential { 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() } + // Hyperbolic functions -/// 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 { - // Common Constants - // FIXME (#5527): These should be associated constants - fn pi() -> Self; - fn two_pi() -> Self; - fn frac_pi_2() -> Self; - fn frac_pi_3() -> Self; - fn frac_pi_4() -> Self; - fn frac_pi_6() -> Self; - fn frac_pi_8() -> Self; - fn frac_1_pi() -> Self; - fn frac_2_pi() -> Self; - fn frac_2_sqrtpi() -> Self; - fn sqrt2() -> Self; - fn frac_1_sqrt2() -> Self; - fn e() -> Self; - fn log2_e() -> Self; - fn log10_e() -> Self; - fn ln_2() -> Self; - fn ln_10() -> Self; // Angular conversions @@ -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 |
