-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
(±1)^-n should not throw a DomainError #8900
Conversation
What does @JeffBezanson think about the hidden |
@ivarne, I used |
The travis failure looks unrelated to me. |
It doesn't bother me. First, I'm not sure Second, I don't think anything is expected about the aliasing behavior of However, calling |
@JeffBezanson, changed to |
@@ -77,6 +77,8 @@ function power_by_squaring(x, p::Integer) | |||
elseif p == 2 | |||
return x*x | |||
elseif p < 0 | |||
x == one(x) && return copy(x) | |||
x == -1 && return iseven(p) ? one(x) : copy(x) |
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.
Comparing x == 1
should suffice here. If you want to make the comparison is between like types, it may make sense to construct u = one(x)
only once and then also return that instance since this is faster for numeric types that do actually require allocation (e.g. BigInt
, BigFloat
, etc.). However, it seems like not constructing this object at all would be better in such cases.
When would there be a difference between It might be the case that |
There shouldn't be a difference but of course people can define these functions to do whatever they want. |
This function was originally written to also handle matrices, where |
Ah, yes. Good point. In that case you definitely don't want to construct it twice. |
Note that this function is never actually called for matrices when the power is negative, as far as I can tell. (And for general matrices, a |
Just in case anyone else is as confused as me.
|
I'm less concerned about matrices (which follow a different code branch for negative powers) than I am about other user-defined fields where |
Can be rebased and merged I think. |
rebased |
Superceded by #18342? |
Ah right, I forgot I had already filed a PR for this. |
Since this has an integer result, no need to throw an exception.