-
-
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
fix literal-pow to return the right type when the base is -1 #53736
Closed
oscardssmith
wants to merge
3
commits into
JuliaLang:master
from
oscardssmith:os/fix-literalpow-neg1-base
Closed
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -369,8 +369,19 @@ const HWNumber = Union{HWReal, Complex{<:HWReal}, Rational{<:HWReal}} | |
@inline literal_pow(::typeof(^), x::HWNumber, ::Val{1}) = x | ||
@inline literal_pow(::typeof(^), x::HWNumber, ::Val{2}) = x*x | ||
@inline literal_pow(::typeof(^), x::HWNumber, ::Val{3}) = x*x*x | ||
@inline literal_pow(::typeof(^), x::HWNumber, ::Val{-1}) = inv(x) | ||
@inline literal_pow(::typeof(^), x::HWNumber, ::Val{-2}) = (i=inv(x); i*i) | ||
@inline function literal_pow(::typeof(^), x::HWNumber, ::Val{-1}) | ||
if x == -one(x) | ||
return isodd(p) ? one(x) : -one(x) | ||
oscardssmith marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
inv(x) | ||
end | ||
@inline function literal_pow(::typeof(^), x::HWNumber, ::Val{-2}) | ||
if x == -one(x) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See previous comment on line 373 |
||
return isodd(p) ? one(x) : -one(x) | ||
end | ||
i=inv(x) | ||
return i*i | ||
end | ||
|
||
# don't use the inv(x) transformation here since float^p is slightly more accurate | ||
@inline literal_pow(::typeof(^), x::AbstractFloat, ::Val{p}) where {p} = x^p | ||
|
@@ -379,7 +390,9 @@ const HWNumber = Union{HWReal, Complex{<:HWReal}, Rational{<:HWReal}} | |
# for other types, define x^-n as inv(x)^n so that negative literal powers can | ||
# be computed in a type-stable way even for e.g. integers. | ||
@inline function literal_pow(f::typeof(^), x, ::Val{p}) where {p} | ||
if p < 0 | ||
if x == -one(x) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See previous comment on line 373 |
||
return isodd(p) ? one(x) : -one(x) | ||
elseif p < 0 | ||
if x isa BitInteger64 | ||
f(Float64(x), p) # inv would cause rounding, while Float64^Integer is able to compensate the inverse | ||
else | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This change introduces even more type instability, now depending on the value of the
x
argument.Other question: why special-case
-1
and not1
?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.
the reason to specialize only on -1 is that inv(x) often has a different type than x, but x*x generally is ok. what is a fade where this version is type unstable? unitful?
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 meant depending on the first argument:
typeof ((-1)^-1) == Int
, whiletypeof(1^-1) == Float64)