Add some missing method wrappers to std::num

This commit is contained in:
Brendan Zabarauskas 2013-07-30 08:58:46 +10:00
parent 4cc3bbb83d
commit b6ea0538a9

View File

@ -45,18 +45,23 @@ pub trait Orderable: Ord {
fn clamp(&self, mn: &Self, mx: &Self) -> Self;
}
#[inline(always)] pub fn min<T: Orderable>(a: T, b: T) -> T { a.min(&b) }
#[inline(always)] pub fn max<T: Orderable>(a: T, b: T) -> T { a.max(&b) }
#[inline(always)] pub fn min<T: Orderable>(x: T, y: T) -> T { x.min(&y) }
#[inline(always)] pub fn max<T: Orderable>(x: T, y: T) -> T { x.max(&y) }
#[inline(always)] pub fn clamp<T: Orderable>(value: T, mn: T, mx: T) -> T { value.clamp(&mn, &mx) }
pub trait Zero {
fn zero() -> Self; // FIXME (#5527): This should be an associated constant
fn is_zero(&self) -> bool;
}
#[inline(always)] pub fn zero<T: Zero>() -> T { Zero::zero() }
pub trait One {
fn one() -> Self; // FIXME (#5527): This should be an associated constant
}
#[inline(always)] pub fn one<T: One>() -> T { One::one() }
pub trait Signed: Num
+ Neg<Self> {
fn abs(&self) -> Self;
@ -68,6 +73,7 @@ pub trait Signed: Num
}
#[inline(always)] pub fn abs<T: Signed>(value: T) -> T { value.abs() }
#[inline(always)] pub fn abs_sub<T: Signed>(x: T, y: T) -> T { x.abs_sub(&y) }
#[inline(always)] pub fn signum<T: Signed>(value: T) -> T { value.signum() }
pub trait Unsigned: Num {}
@ -90,6 +96,9 @@ pub trait Integer: Num
fn is_odd(&self) -> bool;
}
#[inline(always)] pub fn gcd<T: Integer>(x: T, y: T) -> T { x.gcd(&y) }
#[inline(always)] pub fn lcm<T: Integer>(x: T, y: T) -> T { x.lcm(&y) }
pub trait Round {
fn floor(&self) -> Self;
fn ceil(&self) -> Self;
@ -113,15 +122,21 @@ pub trait Algebraic {
fn hypot(&self, other: &Self) -> Self;
}
#[inline(always)] pub fn pow<T: Algebraic>(value: T, n: T) -> T { value.pow(&n) }
#[inline(always)] pub fn sqrt<T: Algebraic>(value: T) -> T { value.sqrt() }
#[inline(always)] pub fn rsqrt<T: Algebraic>(value: T) -> T { value.rsqrt() }
#[inline(always)] pub fn cbrt<T: Algebraic>(value: T) -> T { value.cbrt() }
#[inline(always)] pub fn hypot<T: Algebraic>(x: T, y: T) -> T { x.hypot(&y) }
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;
fn sin_cos(&self) -> (Self, Self);
}
@ -135,10 +150,12 @@ pub trait Trigonometric {
#[inline(always)] pub fn atan<T: Trigonometric>(value: T) -> T { value.atan() }
#[inline(always)] pub fn atan2<T: Trigonometric>(x: T, y: T) -> T { x.atan2(&y) }
#[inline(always)] pub fn sin_cos<T: Trigonometric>(value: T) -> (T, T) { value.sin_cos() }
pub trait Exponential {
fn exp(&self) -> Self;
fn exp2(&self) -> Self;
fn ln(&self) -> Self;
fn log(&self, base: &Self) -> Self;
fn log2(&self) -> Self;
@ -157,6 +174,7 @@ pub trait Hyperbolic: Exponential {
fn sinh(&self) -> Self;
fn cosh(&self) -> Self;
fn tanh(&self) -> Self;
fn asinh(&self) -> Self;
fn acosh(&self) -> Self;
fn atanh(&self) -> Self;
@ -326,6 +344,10 @@ pub trait Float: Real
}
///
#[inline(always)] pub fn exp_m1<T: Float>(value: T) -> T { value.exp_m1() }
#[inline(always)] pub fn ln_1p<T: Float>(value: T) -> T { value.ln_1p() }
#[inline(always)] pub fn mul_add<T: Float>(a: T, b: T, c: T) -> T { a.mul_add(b, c) }
/// Cast from one machine scalar to another
///
/// # Example