Skip to content

Commit

Permalink
auto merge of #11412 : bjz/rust/num-cleanups, r=alexcrichton
Browse files Browse the repository at this point in the history
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.

r? @alexcrichton
  • Loading branch information
bors committed Jan 9, 2014
2 parents ab9ec6d + 0232fed commit 1b0f5b2
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 289 deletions.
5 changes: 2 additions & 3 deletions src/libextra/num/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

//! Complex numbers.

use std::num::{Zero,One,ToStrRadix};

// FIXME #1284: handle complex NaN & infinity etc. This
Expand Down Expand Up @@ -78,15 +77,15 @@ 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 {
self.re.hypot(&self.im)
}
}

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 {
Expand Down
13 changes: 6 additions & 7 deletions src/libextra/num/rational.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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`.
Expand Down
148 changes: 69 additions & 79 deletions src/libstd/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,79 @@ impl Round for f32 {
fn fract(&self) -> f32 { *self - self.trunc() }
}

impl Fractional for f32 {
impl Real for f32 {
/// Archimedes' constant
#[inline]
fn pi() -> f32 { 3.14159265358979323846264338327950288 }

/// 2.0 * pi
#[inline]
fn two_pi() -> f32 { 6.28318530717958647692528676655900576 }

/// pi / 2.0
#[inline]
fn frac_pi_2() -> f32 { 1.57079632679489661923132169163975144 }

/// pi / 3.0
#[inline]
fn frac_pi_3() -> f32 { 1.04719755119659774615421446109316763 }

/// pi / 4.0
#[inline]
fn frac_pi_4() -> f32 { 0.785398163397448309615660845819875721 }

/// pi / 6.0
#[inline]
fn frac_pi_6() -> f32 { 0.52359877559829887307710723054658381 }

/// pi / 8.0
#[inline]
fn frac_pi_8() -> f32 { 0.39269908169872415480783042290993786 }

/// 1 .0/ pi
#[inline]
fn frac_1_pi() -> f32 { 0.318309886183790671537767526745028724 }

/// 2.0 / pi
#[inline]
fn frac_2_pi() -> f32 { 0.636619772367581343075535053490057448 }

/// 2.0 / sqrt(pi)
#[inline]
fn frac_2_sqrtpi() -> f32 { 1.12837916709551257389615890312154517 }

/// sqrt(2.0)
#[inline]
fn sqrt2() -> f32 { 1.41421356237309504880168872420969808 }

/// 1.0 / sqrt(2.0)
#[inline]
fn frac_1_sqrt2() -> f32 { 0.707106781186547524400844362104849039 }

/// Euler's number
#[inline]
fn e() -> f32 { 2.71828182845904523536028747135266250 }

/// log2(e)
#[inline]
fn log2_e() -> f32 { 1.44269504088896340735992468100189214 }

/// log10(e)
#[inline]
fn log10_e() -> f32 { 0.434294481903251827651128918916605082 }

/// ln(2.0)
#[inline]
fn ln_2() -> f32 { 0.693147180559945309417232121458176568 }

/// ln(10.0)
#[inline]
fn ln_10() -> f32 { 2.30258509299404568401799145468436421 }

/// 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) }

Expand All @@ -352,9 +418,7 @@ impl Algebraic for f32 {

#[inline]
fn hypot(&self, other: &f32) -> f32 { hypot(*self, *other) }
}

impl Trigonometric for f32 {
#[inline]
fn sin(&self) -> f32 { sin(*self) }

Expand All @@ -381,9 +445,7 @@ impl Trigonometric for f32 {
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) }
Expand All @@ -407,9 +469,7 @@ impl Exponential for f32 {
/// 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) }

Expand Down Expand Up @@ -469,76 +529,6 @@ impl Hyperbolic for f32 {
fn atanh(&self) -> f32 {
0.5 * ((2.0 * *self) / (1.0 - *self)).ln_1p()
}
}

impl Real for f32 {
/// Archimedes' constant
#[inline]
fn pi() -> f32 { 3.14159265358979323846264338327950288 }

/// 2.0 * pi
#[inline]
fn two_pi() -> f32 { 6.28318530717958647692528676655900576 }

/// pi / 2.0
#[inline]
fn frac_pi_2() -> f32 { 1.57079632679489661923132169163975144 }

/// pi / 3.0
#[inline]
fn frac_pi_3() -> f32 { 1.04719755119659774615421446109316763 }

/// pi / 4.0
#[inline]
fn frac_pi_4() -> f32 { 0.785398163397448309615660845819875721 }

/// pi / 6.0
#[inline]
fn frac_pi_6() -> f32 { 0.52359877559829887307710723054658381 }

/// pi / 8.0
#[inline]
fn frac_pi_8() -> f32 { 0.39269908169872415480783042290993786 }

/// 1 .0/ pi
#[inline]
fn frac_1_pi() -> f32 { 0.318309886183790671537767526745028724 }

/// 2.0 / pi
#[inline]
fn frac_2_pi() -> f32 { 0.636619772367581343075535053490057448 }

/// 2.0 / sqrt(pi)
#[inline]
fn frac_2_sqrtpi() -> f32 { 1.12837916709551257389615890312154517 }

/// sqrt(2.0)
#[inline]
fn sqrt2() -> f32 { 1.41421356237309504880168872420969808 }

/// 1.0 / sqrt(2.0)
#[inline]
fn frac_1_sqrt2() -> f32 { 0.707106781186547524400844362104849039 }

/// Euler's number
#[inline]
fn e() -> f32 { 2.71828182845904523536028747135266250 }

/// log2(e)
#[inline]
fn log2_e() -> f32 { 1.44269504088896340735992468100189214 }

/// log10(e)
#[inline]
fn log10_e() -> f32 { 0.434294481903251827651128918916605082 }

/// ln(2.0)
#[inline]
fn ln_2() -> f32 { 0.693147180559945309417232121458176568 }

/// ln(10.0)
#[inline]
fn ln_10() -> f32 { 2.30258509299404568401799145468436421 }

/// Converts to degrees, assuming the number is in radians
#[inline]
Expand Down
Loading

0 comments on commit 1b0f5b2

Please sign in to comment.