Skip to content
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

Inconsistent behaviors when evaluating negative exponents #28717

Closed
HarrisonGrodin opened this issue Aug 17, 2018 · 2 comments
Closed

Inconsistent behaviors when evaluating negative exponents #28717

HarrisonGrodin opened this issue Aug 17, 2018 · 2 comments

Comments

@HarrisonGrodin
Copy link
Contributor

Working with negative exponents has some very odd behaviors, to say the least. Running the internals of the ^ function gives different results than ^ itself, evaluation success changes based on whether a variable is replaced with its value, and @code_* forms are convinced that the result should always be of type Int64. Tested on v1.0.0 and v1.1.0-DEV.

julia> 2 ^ -5
0.03125

julia> Base.power_by_squaring(2, -5)
ERROR: DomainError with -5:
Cannot raise an integer x to a negative power -5.
Make x a float by adding a zero decimal (e.g., 2.0^-5 instead of 2^-5), or write 1/x^5, float(x)^-5, or (x//1)^-5
Stacktrace:
 [1] throw_domerr_powbysq(::Int64, ::Int64) at ./intfuncs.jl:176
 [2] power_by_squaring(::Int64, ::Int64) at ./intfuncs.jl:196
 [3] top-level scope at none:0

julia> @edit 2 ^ -5
# ^(x::T, p::T) where {T<:Integer} = power_by_squaring(x,p)
julia> x = -2;

julia> x ^ x
ERROR: DomainError with -2:
Cannot raise an integer x to a negative power -2.
Make x a float by adding a zero decimal (e.g., 2.0^-2 instead of 2^-2), or write 1/x^2, float(x)^-2, or (x//1)^-2
Stacktrace:
 [1] throw_domerr_powbysq(::Int64, ::Int64) at ./intfuncs.jl:176
 [2] power_by_squaring(::Int64, ::Int64) at ./intfuncs.jl:196
 [3] ^(::Int64, ::Int64) at ./intfuncs.jl:220
 [4] top-level scope at none:0

julia> x ^ (-2)
0.25

julia> (-2) ^ x
ERROR: DomainError with -2:
Cannot raise an integer x to a negative power -2.
Make x a float by adding a zero decimal (e.g., 2.0^-2 instead of 2^-2), or write 1/x^2, float(x)^-2, or (x//1)^-2
Stacktrace:
 [1] throw_domerr_powbysq(::Int64, ::Int64) at ./intfuncs.jl:176
 [2] power_by_squaring(::Int64, ::Int64) at ./intfuncs.jl:196
 [3] ^(::Int64, ::Int64) at ./intfuncs.jl:220
 [4] top-level scope at none:0

julia> (-2) ^ (-2)
0.25

julia> @code_warntype x ^ x
Body::Int64
220 1%1 = invoke Base.power_by_squaring(_2::Int64, _3::Int64)::Int64                                          │
    └──      return %1
@affans
Copy link
Contributor

affans commented Aug 17, 2018

This is intentional. See associated issue and discourse forum post.

#28685

@fredrikekre
Copy link
Member

Closing as duplicate of #28685

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants