提交 8f63f978 编写于 作者: B Brendan Zabarauskas

Implement Fractional for Ratio

上级 c9620dc0
......@@ -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) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册