Skip to content

Commit

Permalink
auto merge of #6110 : bjz/rust/numeric-traits, r=pcwalton
Browse files Browse the repository at this point in the history
As discussed on issue #4819, I have created four new traits: `Algebraic`, `Trigonometric`, `Exponential` and `Hyperbolic`, and moved the appropriate methods into them from `Real`.

~~~rust
pub trait Algebraic {
    fn pow(&self, n: Self) -> Self;
    fn sqrt(&self) -> Self;
    fn rsqrt(&self) -> Self;
    fn cbrt(&self) -> Self;
    fn hypot(&self, other: Self) -> Self;
}

pub trait Trigonometric {
    fn sin(&self) -> Self;
    fn cos(&self) -> Self;
    fn tan(&self) -> Self;
    fn asin(&self) -> Self;
    fn acos(&self) -> Self;
    fn atan(&self) -> Self;
    fn atan2(&self, other: Self) -> Self;
}

pub trait Exponential {
    fn exp(&self) -> Self;
    fn exp2(&self) -> Self;
    fn expm1(&self) -> Self;
    fn log(&self) -> Self;
    fn log2(&self) -> Self;
    fn log10(&self) -> Self;
}

pub trait Hyperbolic: Exponential {
    fn sinh(&self) -> Self;
    fn cosh(&self) -> Self;
    fn tanh(&self) -> Self;
}
~~~

There was some discussion over whether we should shorten the names, for example `Trig` and `Exp`. No abbreviations have been agreed on yet, but this could be considered in the future.

Additionally, `Integer::divisible_by` has been renamed to `Integer::is_multiple_of`.
  • Loading branch information
bors committed Apr 29, 2013
2 parents 76ec35a + 500078e commit dbcc3fe
Show file tree
Hide file tree
Showing 10 changed files with 380 additions and 258 deletions.
5 changes: 3 additions & 2 deletions src/libcore/core.rc
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ pub use old_iter::{ExtendedMutableIter};
pub use iter::Times;

pub use num::{Num, NumCast};
pub use num::{Orderable, Signed, Unsigned, Integer};
pub use num::{Round, Fractional, Real, RealExt};
pub use num::{Orderable, Signed, Unsigned, Round};
pub use num::{Algebraic, Trigonometric, Exponential, Hyperbolic};
pub use num::{Integer, Fractional, Real, RealExt};
pub use num::{Bitwise, BitCount, Bounded};
pub use num::{Primitive, Int, Float};

Expand Down
142 changes: 70 additions & 72 deletions src/libcore/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
//! Operations and constants for `f32`

use from_str;
use libc::c_int;
use num::{Zero, One, strconv};
use prelude::*;

Expand Down Expand Up @@ -102,8 +101,8 @@ delegate!(
fn sinh(n: c_float) -> c_float = c_float_utils::sinh,
fn tan(n: c_float) -> c_float = c_float_utils::tan,
fn tanh(n: c_float) -> c_float = c_float_utils::tanh,
fn tgamma(n: c_float) -> c_float = c_float_utils::tgamma)

fn tgamma(n: c_float) -> c_float = c_float_utils::tgamma
)

// These are not defined inside consts:: for consistency with
// the integer types
Expand Down Expand Up @@ -368,154 +367,153 @@ impl Fractional for f32 {
fn recip(&self) -> f32 { 1.0 / *self }
}

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

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

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

/// pi / 3.0
#[inline(always)]
fn frac_pi_3() -> f32 { 1.04719755119659774615421446109316763 }
fn pow(&self, n: f32) -> f32 { pow(*self, n) }

/// pi / 4.0
#[inline(always)]
fn frac_pi_4() -> f32 { 0.785398163397448309615660845819875721 }
fn sqrt(&self) -> f32 { sqrt(*self) }

/// pi / 6.0
#[inline(always)]
fn frac_pi_6() -> f32 { 0.52359877559829887307710723054658381 }
fn rsqrt(&self) -> f32 { self.sqrt().recip() }

/// pi / 8.0
#[inline(always)]
fn frac_pi_8() -> f32 { 0.39269908169872415480783042290993786 }
fn cbrt(&self) -> f32 { cbrt(*self) }

/// 1 .0/ pi
#[inline(always)]
fn frac_1_pi() -> f32 { 0.318309886183790671537767526745028724 }
fn hypot(&self, other: f32) -> f32 { hypot(*self, other) }
}

/// 2.0 / pi
impl Trigonometric for f32 {
#[inline(always)]
fn frac_2_pi() -> f32 { 0.636619772367581343075535053490057448 }
fn sin(&self) -> f32 { sin(*self) }

/// 2.0 / sqrt(pi)
#[inline(always)]
fn frac_2_sqrtpi() -> f32 { 1.12837916709551257389615890312154517 }
fn cos(&self) -> f32 { cos(*self) }

/// sqrt(2.0)
#[inline(always)]
fn sqrt2() -> f32 { 1.41421356237309504880168872420969808 }
fn tan(&self) -> f32 { tan(*self) }

/// 1.0 / sqrt(2.0)
#[inline(always)]
fn frac_1_sqrt2() -> f32 { 0.707106781186547524400844362104849039 }
fn asin(&self) -> f32 { asin(*self) }

/// Euler's number
#[inline(always)]
fn e() -> f32 { 2.71828182845904523536028747135266250 }
fn acos(&self) -> f32 { acos(*self) }

/// log2(e)
#[inline(always)]
fn log2_e() -> f32 { 1.44269504088896340735992468100189214 }
fn atan(&self) -> f32 { atan(*self) }

/// log10(e)
#[inline(always)]
fn log10_e() -> f32 { 0.434294481903251827651128918916605082 }
fn atan2(&self, other: f32) -> f32 { atan2(*self, other) }
}

/// log(2.0)
impl Exponential for f32 {
#[inline(always)]
fn log_2() -> f32 { 0.693147180559945309417232121458176568 }
fn exp(&self) -> f32 { exp(*self) }

/// log(10.0)
#[inline(always)]
fn log_10() -> f32 { 2.30258509299404568401799145468436421 }
fn exp2(&self) -> f32 { exp2(*self) }

#[inline(always)]
fn pow(&self, n: f32) -> f32 { pow(*self, n) }
fn expm1(&self) -> f32 { expm1(*self) }

#[inline(always)]
fn exp(&self) -> f32 { exp(*self) }
fn log(&self) -> f32 { ln(*self) }

#[inline(always)]
fn exp2(&self) -> f32 { exp2(*self) }
fn log2(&self) -> f32 { log2(*self) }

#[inline(always)]
fn expm1(&self) -> f32 { expm1(*self) }
fn log10(&self) -> f32 { log10(*self) }
}

impl Hyperbolic for f32 {
#[inline(always)]
fn ldexp(&self, n: int) -> f32 { ldexp(*self, n as c_int) }
fn sinh(&self) -> f32 { sinh(*self) }

#[inline(always)]
fn log(&self) -> f32 { ln(*self) }
fn cosh(&self) -> f32 { cosh(*self) }

#[inline(always)]
fn log2(&self) -> f32 { log2(*self) }
fn tanh(&self) -> f32 { tanh(*self) }
}

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

/// 2.0 * pi
#[inline(always)]
fn log_radix(&self) -> f32 { log_radix(*self) as f32 }
fn two_pi() -> f32 { 6.28318530717958647692528676655900576 }

/// pi / 2.0
#[inline(always)]
fn ilog_radix(&self) -> int { ilog_radix(*self) as int }
fn frac_pi_2() -> f32 { 1.57079632679489661923132169163975144 }

/// pi / 3.0
#[inline(always)]
fn sqrt(&self) -> f32 { sqrt(*self) }
fn frac_pi_3() -> f32 { 1.04719755119659774615421446109316763 }

/// pi / 4.0
#[inline(always)]
fn rsqrt(&self) -> f32 { self.sqrt().recip() }
fn frac_pi_4() -> f32 { 0.785398163397448309615660845819875721 }

/// pi / 6.0
#[inline(always)]
fn cbrt(&self) -> f32 { cbrt(*self) }
fn frac_pi_6() -> f32 { 0.52359877559829887307710723054658381 }

/// Converts to degrees, assuming the number is in radians
/// pi / 8.0
#[inline(always)]
fn to_degrees(&self) -> f32 { *self * (180.0 / Real::pi::<f32>()) }
fn frac_pi_8() -> f32 { 0.39269908169872415480783042290993786 }

/// Converts to radians, assuming the number is in degrees
/// 1 .0/ pi
#[inline(always)]
fn to_radians(&self) -> f32 { *self * (Real::pi::<f32>() / 180.0) }
fn frac_1_pi() -> f32 { 0.318309886183790671537767526745028724 }

/// 2.0 / pi
#[inline(always)]
fn hypot(&self, other: f32) -> f32 { hypot(*self, other) }
fn frac_2_pi() -> f32 { 0.636619772367581343075535053490057448 }

/// 2.0 / sqrt(pi)
#[inline(always)]
fn sin(&self) -> f32 { sin(*self) }
fn frac_2_sqrtpi() -> f32 { 1.12837916709551257389615890312154517 }

/// sqrt(2.0)
#[inline(always)]
fn cos(&self) -> f32 { cos(*self) }
fn sqrt2() -> f32 { 1.41421356237309504880168872420969808 }

/// 1.0 / sqrt(2.0)
#[inline(always)]
fn tan(&self) -> f32 { tan(*self) }
fn frac_1_sqrt2() -> f32 { 0.707106781186547524400844362104849039 }

/// Euler's number
#[inline(always)]
fn asin(&self) -> f32 { asin(*self) }
fn e() -> f32 { 2.71828182845904523536028747135266250 }

/// log2(e)
#[inline(always)]
fn acos(&self) -> f32 { acos(*self) }
fn log2_e() -> f32 { 1.44269504088896340735992468100189214 }

/// log10(e)
#[inline(always)]
fn atan(&self) -> f32 { atan(*self) }
fn log10_e() -> f32 { 0.434294481903251827651128918916605082 }

/// log(2.0)
#[inline(always)]
fn atan2(&self, other: f32) -> f32 { atan2(*self, other) }
fn log_2() -> f32 { 0.693147180559945309417232121458176568 }

/// log(10.0)
#[inline(always)]
fn sinh(&self) -> f32 { sinh(*self) }
fn log_10() -> f32 { 2.30258509299404568401799145468436421 }

/// Converts to degrees, assuming the number is in radians
#[inline(always)]
fn cosh(&self) -> f32 { cosh(*self) }
fn to_degrees(&self) -> f32 { *self * (180.0 / Real::pi::<f32>()) }

/// Converts to radians, assuming the number is in degrees
#[inline(always)]
fn tanh(&self) -> f32 { tanh(*self) }
fn to_radians(&self) -> f32 { *self * (Real::pi::<f32>() / 180.0) }
}

impl Bounded for f32 {
Expand Down
Loading

0 comments on commit dbcc3fe

Please sign in to comment.