-
Notifications
You must be signed in to change notification settings - Fork 139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pow(0, 0) no longer returnes 1 #78
Conversation
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
Outdated
if exp == 0 { | ||
return T::one(); | ||
return if base.is_zero() { | ||
panic!("0⁰ is undefined") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would restrain myself from using Unicode in panic messages as it can be problematic on platforms that do not support Unicode in CLI (aka Windows).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we want:
- 0^0
- pow(0, 0)
- any other?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would go with pow(0, 0)
as this is what user wrote. 0^0
is common syntax but in this case can be confused with XOR operator.
@@ -182,9 +183,13 @@ mod float_impls { | |||
/// assert_eq!(pow(6u8, 3), 216); | |||
/// ``` | |||
#[inline] | |||
pub fn pow<T: Clone + One + Mul<T, Output = T>>(mut base: T, mut exp: usize) -> T { | |||
pub fn pow<T: Clone + Zero + One + Mul<T, Output = T>>(mut base: T, mut exp: usize) -> T { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that this is breaking change, so we should think if this is really only way.
Oddly rustc also prints 1 for 0⁰. Did not find a issue for it, nor anywhere that says that it should be like that. Did not even find a unit test for this case(in rustc). Might want to make an issue for rustc |
cc #77 for the cross-reference Many other languages return 1: https://rosettacode.org/wiki/Zero_to_the_zero_power I think we should leave the current behavior alone and just document this edge case. Since we can't even check for this case without adding |
Closing in favor of #79. |
No description provided.