2012-09-24 02:54:24 +00:00
|
|
|
use cmp::Ord;
|
2012-10-28 07:00:49 +00:00
|
|
|
use num::{Num, from_int};
|
|
|
|
|
2012-10-30 02:45:18 +00:00
|
|
|
pub trait ExactEq {
|
|
|
|
pure fn exact_eq(other: &self) -> bool;
|
|
|
|
}
|
|
|
|
|
2012-10-28 07:00:49 +00:00
|
|
|
//
|
|
|
|
// Min
|
|
|
|
//
|
|
|
|
#[inline]
|
|
|
|
pure fn min<T:Copy Ord>(a: &T, b: &T) -> T {
|
|
|
|
if a < b { *a }
|
|
|
|
else { *b }
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Max
|
|
|
|
//
|
|
|
|
#[inline]
|
|
|
|
pure fn max<T:Copy Ord>(a: &T, b: &T) -> T {
|
|
|
|
if a > b { *a }
|
|
|
|
else { *b }
|
|
|
|
}
|
|
|
|
|
|
|
|
// pure fn abs<T:Copy Num Ord>(x: &T) -> T {
|
|
|
|
// if x >= num::from_int(0) { *x } else {-x }
|
|
|
|
// }
|
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;
|
|
|
|
}
|
|
|
|
|
2012-10-28 06:57:41 +00:00
|
|
|
#[inline]
|
|
|
|
pure fn abs<T: Abs>(x: T) -> T {
|
|
|
|
x.abs()
|
|
|
|
}
|
|
|
|
|
|
|
|
impl i8: Abs {
|
|
|
|
#[inline]
|
|
|
|
pure fn abs() -> i8 {
|
|
|
|
if self >= 0 { self }
|
|
|
|
else {-self }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl i16: Abs {
|
|
|
|
#[inline]
|
|
|
|
pure fn abs() -> i16 {
|
|
|
|
if self >= 0 { self }
|
|
|
|
else {-self }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl i32: Abs {
|
|
|
|
#[inline]
|
|
|
|
pure fn abs() -> i32 {
|
|
|
|
if self >= 0 { self }
|
|
|
|
else {-self }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl i64: Abs {
|
|
|
|
#[inline]
|
|
|
|
pure fn abs() -> i64 {
|
|
|
|
if self >= 0 { self }
|
|
|
|
else {-self }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-08 05:54:31 +00:00
|
|
|
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
|
|
|
//
|
2012-10-28 07:00:49 +00:00
|
|
|
// Sqrt
|
2012-09-10 02:55:15 +00:00
|
|
|
//
|
2012-10-28 07:00:49 +00:00
|
|
|
trait Sqrt {
|
|
|
|
pure fn sqrt() -> self;
|
2012-09-08 05:54:31 +00:00
|
|
|
}
|
|
|
|
|
2012-09-24 02:54:24 +00:00
|
|
|
#[inline]
|
2012-10-28 07:00:49 +00:00
|
|
|
pure fn sqrt<T: Sqrt>(x: T) -> T {
|
|
|
|
x.sqrt()
|
2012-09-10 02:55:15 +00:00
|
|
|
}
|
|
|
|
|
2012-10-28 07:00:49 +00:00
|
|
|
impl u8: Sqrt {
|
|
|
|
#[inline]
|
|
|
|
pure fn sqrt() -> u8 {
|
|
|
|
(self as float).sqrt() as u8
|
|
|
|
}
|
2012-09-10 02:55:15 +00:00
|
|
|
}
|
|
|
|
|
2012-10-28 07:00:49 +00:00
|
|
|
impl u16: Sqrt {
|
|
|
|
#[inline]
|
|
|
|
pure fn sqrt() -> u16 {
|
|
|
|
(self as float).sqrt() as u16
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl u32: Sqrt {
|
|
|
|
#[inline]
|
|
|
|
pure fn sqrt() -> u32 {
|
|
|
|
(self as float).sqrt() as u32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl u64: Sqrt {
|
|
|
|
#[inline]
|
|
|
|
pure fn sqrt() -> u64 {
|
|
|
|
(self as float).sqrt() as u64
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl uint: Sqrt {
|
|
|
|
#[inline]
|
|
|
|
pure fn sqrt() -> uint {
|
|
|
|
(self as float).sqrt() as uint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl i8: Sqrt {
|
|
|
|
#[inline]
|
|
|
|
pure fn sqrt() -> i8 {
|
|
|
|
(self as float).sqrt() as i8
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl i16: Sqrt {
|
|
|
|
#[inline]
|
|
|
|
pure fn sqrt() -> i16 {
|
|
|
|
(self as float).sqrt() as i16
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl i32: Sqrt {
|
|
|
|
#[inline]
|
|
|
|
pure fn sqrt() -> i32 {
|
|
|
|
(self as float).sqrt() as i32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl i64: Sqrt {
|
|
|
|
#[inline]
|
|
|
|
pure fn sqrt() -> i64 {
|
|
|
|
(self as float).sqrt() as i64
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl int: Sqrt {
|
|
|
|
#[inline]
|
|
|
|
pure fn sqrt() -> int {
|
|
|
|
(self as float).sqrt() as int
|
|
|
|
}
|
|
|
|
}
|
2012-09-10 02:55:15 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|