From 8736892882ffbfb122806bff21ecbf18a35b26c6 Mon Sep 17 00:00:00 2001 From: Meltinglava Date: Wed, 18 Jul 2018 11:47:16 +0200 Subject: [PATCH 1/3] =?UTF-8?q?Pow=20no=20longer=20returns=201=20when=200?= =?UTF-8?q?=E2=81=B0=20is=20given.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit pow now panics in case of 0⁰ check_pow now returns None in case of 0⁰ pow and checked_pow now requires the trait 'Zero' as we need it to check if base is zero when exp is zero. Added basic seperate tests to check that basic behaivor works, and that bought pow and checked_pow behaves correclty when 0⁰ is given. Updated the documentation to state behavior in 0⁰ case. Fixes #77 --- src/pow.rs | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/src/pow.rs b/src/pow.rs index 7ef8d3f4..c7893a78 100644 --- a/src/pow.rs +++ b/src/pow.rs @@ -1,6 +1,6 @@ use core::num::Wrapping; use core::ops::Mul; -use {CheckedMul, One}; +use {CheckedMul, Zero, One}; /// Binary operator for raising a value to a power. pub trait Pow { @@ -172,6 +172,7 @@ mod float_impls { } /// Raises a value to the power of exp, using exponentiation by squaring. +/// Panics if pow(0, 0) (0⁰ is undefined) /// /// # Example /// @@ -182,9 +183,13 @@ mod float_impls { /// assert_eq!(pow(6u8, 3), 216); /// ``` #[inline] -pub fn pow>(mut base: T, mut exp: usize) -> T { +pub fn pow>(mut base: T, mut exp: usize) -> T { if exp == 0 { - return T::one(); + return if base.is_zero() { + panic!("0⁰ is undefined") + } else { + T::one() + } } while exp & 1 == 0 { @@ -207,6 +212,7 @@ pub fn pow>(mut base: T, mut exp: usize) -> } /// Raises a value to the power of exp, returning `None` if an overflow occurred. +/// Also returns None if checked_pow(0, 0) was entered (0⁰ is undefined) /// /// Otherwise same as the `pow` function. /// @@ -218,11 +224,16 @@ pub fn pow>(mut base: T, mut exp: usize) -> /// assert_eq!(checked_pow(2i8, 4), Some(16)); /// assert_eq!(checked_pow(7i8, 8), None); /// assert_eq!(checked_pow(7u32, 8), Some(5_764_801)); +/// assert_eq!(checked_pow(0i8, 0), None); //undefined /// ``` #[inline] -pub fn checked_pow(mut base: T, mut exp: usize) -> Option { +pub fn checked_pow(mut base: T, mut exp: usize) -> Option { if exp == 0 { - return Some(T::one()); + return if base.is_zero() { + None + } else { + Some(T::one()) + } } macro_rules! optry { @@ -253,3 +264,21 @@ pub fn checked_pow(mut base: T, mut exp: usize) -> } Some(acc) } + +#[cfg(test)] +mod tests { + #[test] + fn pow_3_3() { + assert_eq!(super::checked_pow(3, 3), Some(27)); + assert_eq!(super::pow(3, 3), 27); + } + #[test] + fn checked_pow_0_0() { + assert_eq!(super::checked_pow(0, 0), None); + } + #[test] + #[should_panic] + fn pow_0_0() { + super::pow(0, 0); + } +} From 444f5fbc2363173439e6b0a32b31bf8ce855e22d Mon Sep 17 00:00:00 2001 From: Meltinglava Date: Wed, 18 Jul 2018 11:55:03 +0200 Subject: [PATCH 2/3] run cargo-fmt --- src/pow.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/pow.rs b/src/pow.rs index c7893a78..34e26531 100644 --- a/src/pow.rs +++ b/src/pow.rs @@ -1,6 +1,6 @@ use core::num::Wrapping; use core::ops::Mul; -use {CheckedMul, Zero, One}; +use {CheckedMul, One, Zero}; /// Binary operator for raising a value to a power. pub trait Pow { @@ -189,7 +189,7 @@ pub fn pow>(mut base: T, mut exp: usi panic!("0⁰ is undefined") } else { T::one() - } + }; } while exp & 1 == 0 { @@ -229,11 +229,7 @@ pub fn pow>(mut base: T, mut exp: usi #[inline] pub fn checked_pow(mut base: T, mut exp: usize) -> Option { if exp == 0 { - return if base.is_zero() { - None - } else { - Some(T::one()) - } + return if base.is_zero() { None } else { Some(T::one()) }; } macro_rules! optry { From 269c141e46935b4862e37b1840c9a7cddb070ac3 Mon Sep 17 00:00:00 2001 From: Meltinglava Date: Wed, 18 Jul 2018 12:25:03 +0200 Subject: [PATCH 3/3] panic case is non utf-8 --- src/pow.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pow.rs b/src/pow.rs index 34e26531..ee85257d 100644 --- a/src/pow.rs +++ b/src/pow.rs @@ -186,7 +186,7 @@ mod float_impls { pub fn pow>(mut base: T, mut exp: usize) -> T { if exp == 0 { return if base.is_zero() { - panic!("0⁰ is undefined") + panic!("pow(0, 0) is undefined") } else { T::one() };