From 505eb86c18f2b1105e9e995fd43a9afbec008743 Mon Sep 17 00:00:00 2001 From: runarberg Date: Sun, 17 May 2015 18:22:32 +0000 Subject: [PATCH] Added the power method for rational numbers --- src/rational.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/rational.rs b/src/rational.rs index 97d0a83..3f7d2ae 100644 --- a/src/rational.rs +++ b/src/rational.rs @@ -18,7 +18,7 @@ use std::fmt; use std::ops::{Add, Div, Mul, Neg, Rem, Sub}; use std::str::FromStr; -use traits::{FromPrimitive, Float}; +use traits::{FromPrimitive, Float, PrimInt}; use bigint::{BigInt, BigUint, Sign}; use {Num, Signed, Zero, One}; @@ -184,6 +184,19 @@ impl Ratio { } } +impl Ratio { + /// Raises the ratio to the power of an exponent + #[inline] + pub fn pow(&self, expon: i32) -> Ratio { + match expon.cmp(&0) { + cmp::Ordering::Equal => One::one(), + cmp::Ordering::Less => self.recip().pow(-expon), + cmp::Ordering::Greater => Ratio::new_raw(self.numer.pow(expon as u32), + self.denom.pow(expon as u32)), + } + } +} + impl Ratio { /// Converts a float into a rational number. pub fn from_float(f: T) -> Option { @@ -758,6 +771,18 @@ mod test { assert_eq!(_NEG1_2 * _NEG1_2.recip(), _1); } + #[test] + fn test_pow() { + assert_eq!(_1_2.pow(2), Ratio::new(1, 4)); + assert_eq!(_1_2.pow(-2), Ratio::new(4, 1)); + assert_eq!(_1.pow(1), _1); + assert_eq!(_NEG1_2.pow(2), _1_2.pow(2)); + assert_eq!(_NEG1_2.pow(3), -_1_2.pow(3)); + assert_eq!(_3_2.pow(0), _1); + assert_eq!(_3_2.pow(-1), _3_2.recip()); + assert_eq!(_3_2.pow(3), Ratio::new(27, 8)); + } + #[test] fn test_to_from_str() { fn test(r: Rational, s: String) {