Implement Sqrt for more types

This commit is contained in:
Brendan Zabarauskas 2012-10-28 17:00:49 +10:00
parent 28a9a8250a
commit 44ab57cb43

View file

@ -1,4 +1,27 @@
use cmp::Ord;
use num::{Num, from_int};
//
// 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 }
// }
//
// Abs
@ -76,24 +99,6 @@ impl f64: Abs {
}
}
//
// 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 }
}
//
// Sqrt
//
@ -101,12 +106,80 @@ trait Sqrt {
pure fn sqrt() -> self;
}
// impl int: Sqrt {
// #[inline]
// pure fn sqrt() -> int {
// sqrt(self)
// }
// }
#[inline]
pure fn sqrt<T: Sqrt>(x: T) -> T {
x.sqrt()
}
impl u8: Sqrt {
#[inline]
pure fn sqrt() -> u8 {
(self as float).sqrt() as u8
}
}
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
}
}
impl float: Sqrt {
#[inline]