mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-28 20:16:58 +03:00
auto merge of #6110 : bjz/rust/numeric-traits, r=pcwalton
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`.
This commit is contained in:
@@ -428,7 +428,7 @@ fn lcm(&self, other: &BigUint) -> BigUint { ((*self * *other) / self.gcd(other))
|
||||
|
||||
/// Returns `true` if the number can be divided by `other` without leaving a remainder
|
||||
#[inline(always)]
|
||||
fn divisible_by(&self, other: &BigUint) -> bool { (*self % *other).is_zero() }
|
||||
fn is_multiple_of(&self, other: &BigUint) -> bool { (*self % *other).is_zero() }
|
||||
|
||||
/// Returns `true` if the number is divisible by `2`
|
||||
#[inline(always)]
|
||||
@@ -973,7 +973,7 @@ fn lcm(&self, other: &BigInt) -> BigInt {
|
||||
|
||||
/// Returns `true` if the number can be divided by `other` without leaving a remainder
|
||||
#[inline(always)]
|
||||
fn divisible_by(&self, other: &BigInt) -> bool { self.data.divisible_by(&other.data) }
|
||||
fn is_multiple_of(&self, other: &BigInt) -> bool { self.data.is_multiple_of(&other.data) }
|
||||
|
||||
/// Returns `true` if the number is divisible by `2`
|
||||
#[inline(always)]
|
||||
|
||||
@@ -203,6 +203,9 @@ fn one() -> Ratio<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy + Num + Ord>
|
||||
Num for Ratio<T> {}
|
||||
|
||||
/* Utils */
|
||||
impl<T: Copy + Num + Ord>
|
||||
Round for Ratio<T> {
|
||||
@@ -242,6 +245,12 @@ fn fract(&self) -> Ratio<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy + Num + Ord> Fractional for Ratio<T> {
|
||||
#[inline]
|
||||
fn recip(&self) -> Ratio<T> {
|
||||
Ratio::new_raw(self.denom, self.numer)
|
||||
}
|
||||
}
|
||||
|
||||
/* String conversions */
|
||||
impl<T: ToStr> ToStr for Ratio<T> {
|
||||
@@ -446,6 +455,15 @@ fn test_fract() {
|
||||
assert_eq!(_3_2.fract(), _1_2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_recip() {
|
||||
assert_eq!(_1 * _1.recip(), _1);
|
||||
assert_eq!(_2 * _2.recip(), _1);
|
||||
assert_eq!(_1_2 * _1_2.recip(), _1);
|
||||
assert_eq!(_3_2 * _3_2.recip(), _1);
|
||||
assert_eq!(_neg1_2 * _neg1_2.recip(), _1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_from_str() {
|
||||
fn test(r: Rational, s: ~str) {
|
||||
|
||||
Reference in New Issue
Block a user