2012-09-24 02:54:24 +00:00
|
|
|
use cmp::Ord;
|
2012-09-08 05:54:31 +00:00
|
|
|
|
2012-09-10 02:55:15 +00:00
|
|
|
//
|
|
|
|
// Abs
|
|
|
|
//
|
2012-09-08 05:54:31 +00:00
|
|
|
trait Abs {
|
|
|
|
pure fn abs() -> self;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl int: Abs {
|
2012-09-24 02:54:24 +00:00
|
|
|
#[inline]
|
2012-09-08 05:54:31 +00:00
|
|
|
pure fn abs() -> int {
|
|
|
|
if self >= 0 { self }
|
|
|
|
else {-self }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl float: Abs {
|
2012-09-24 02:54:24 +00:00
|
|
|
#[inline]
|
2012-09-08 05:54:31 +00:00
|
|
|
pure fn abs() -> float {
|
|
|
|
if self >= 0f { self }
|
|
|
|
else {-self }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl f32: Abs {
|
2012-09-24 02:54:24 +00:00
|
|
|
#[inline]
|
2012-09-08 05:54:31 +00:00
|
|
|
pure fn abs() -> f32 {
|
|
|
|
if self >= 0f32 { self }
|
|
|
|
else {-self }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl f64: Abs {
|
2012-09-24 02:54:24 +00:00
|
|
|
#[inline]
|
2012-09-08 05:54:31 +00:00
|
|
|
pure fn abs() -> f64 {
|
|
|
|
if self >= 0f64 { self }
|
|
|
|
else {-self }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-10 02:55:15 +00:00
|
|
|
//
|
|
|
|
// Min
|
|
|
|
//
|
2012-09-24 02:54:24 +00:00
|
|
|
#[inline]
|
2012-09-10 02:55:15 +00:00
|
|
|
pure fn min<T:Copy Ord>(&&a:T, &&b:T) -> T {
|
2012-09-08 05:54:31 +00:00
|
|
|
if a < b { a }
|
|
|
|
else { b }
|
|
|
|
}
|
|
|
|
|
2012-09-10 02:55:15 +00:00
|
|
|
//
|
|
|
|
// Max
|
|
|
|
//
|
2012-09-24 02:54:24 +00:00
|
|
|
#[inline]
|
2012-09-10 02:55:15 +00:00
|
|
|
pure fn max<T:Copy Ord>(&&a:T, &&b:T) -> T {
|
2012-09-08 05:54:31 +00:00
|
|
|
if a > b { a }
|
|
|
|
else { b }
|
2012-09-10 02:55:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Sqrt
|
|
|
|
//
|
|
|
|
trait Sqrt {
|
|
|
|
pure fn sqrt() -> self;
|
|
|
|
}
|
|
|
|
|
|
|
|
// impl int: Sqrt {
|
2012-09-24 02:54:24 +00:00
|
|
|
// #[inline]
|
2012-09-10 02:55:15 +00:00
|
|
|
// pure fn sqrt() -> int {
|
|
|
|
// sqrt(self)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
impl float: Sqrt {
|
2012-09-24 02:54:24 +00:00
|
|
|
#[inline]
|
2012-09-10 02:55:15 +00:00
|
|
|
pure fn sqrt() -> float {
|
|
|
|
float::sqrt(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl f32: Sqrt {
|
2012-09-24 02:54:24 +00:00
|
|
|
#[inline]
|
2012-09-10 02:55:15 +00:00
|
|
|
pure fn sqrt() -> f32 {
|
|
|
|
f32::sqrt(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl f64: Sqrt {
|
2012-09-24 02:54:24 +00:00
|
|
|
#[inline]
|
2012-09-10 02:55:15 +00:00
|
|
|
pure fn sqrt() -> f64 {
|
|
|
|
f64::sqrt(self)
|
|
|
|
}
|
2012-09-08 05:54:31 +00:00
|
|
|
}
|