-
-
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
Raise informative error for float/integer exponentiation that yields … #12822
Conversation
print(io, "a zero decimal (e.g. 2.0^-n instead of 2^-n), or write 1/x^n, float(x)^-n, or (x//1)^-n .") | ||
elseif code[1] == :^ && (code[2] == symbol("promotion.jl") || code[2] == symbol("math.jl")) | ||
println(io, "\nCannot raise an integer or float yielding a complex number. ") | ||
print(io, "If x is the input number that should be raised to y, use (x+0im)^y or convert(Complex, x)^y .") |
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.
Complex(x)
is simpler than convert
. Maybe: "Replace x^y with (x+0im)^y, Complex(x)^y, or similar."
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.
Stylistically, it is weird that we are using multiple print
calls here. Would be cleaner to replace this (and similar constructions above) with:
print(io, "\n ............",
"\n .......")
@stevengj , thanks for the input. |
Raise informative error for float/integer exponentiation that yields …
Hmm, this was technically not a bugfix; should I not have merged in the 0.4-pre cycle? Since it was just an improved error message, it seemed innocuous. |
elseif (code[1] == :^ && code[2] == symbol("intfuncs.jl")) || code[1] == :power_by_squaring | ||
println(io, "\nCannot raise an integer x to a negative power -n. Make x a float by adding") | ||
print(io, "a zero decimal (e.g. 2.0^-n instead of 2^-n), or write 1/x^n, float(x)^-n, or (x//1)^-n") | ||
println(io,"\n$(code[1]) will only return a complex result if called with a complex argument. ", |
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.
Hmm, this probably should have been print
, not println
; you added an extra newline to the output.
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.
@stevengj , actually that was the original design. I didn't introduce it. In fact, I'm not getting extraneous newlines comparing the output before and after your commit removing those println
(e3d2cb7). Since I didn't notice that behavior, I didn't bother to change the old code.
Before
_/ |\__'_|_|_|\__'_| | Commit 732860a (0 days old master)
|__/ | x86_64-apple-darwin14.5.0
julia> log(-2)
ERROR: DomainError:
log will only return a complex result if called with a complex argument.
try log (complex(x))
in log at math.jl:139
julia> (-2.0)^(2.2)
ERROR: DomainError:
in ^ at math.jl:275
After
_/ |\__'_|_|_|\__'_| | Commit e3d2cb7 (0 days old master)
|__/ | x86_64-apple-darwin14.5.0
julia> log(-2)
ERROR: DomainError:
log will only return a complex result if called with a complex argument. Try log (complex(x)).
in log at math.jl:139
julia> (-2.0)^(2.2)
ERROR: DomainError:
Exponentiation yielding a complex result requires a complex argument.
Replace x^y with (x+0im)^y, Complex(x)^y, or similar.
in ^ at math.jl:275
Seems okay to me, I don't think this would break anything. |
@felipenoris, the original code had |
…Complex
Same as #3024.
Deals with the following cases: