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

Raise informative error for float/integer exponentiation that yields … #12822

Merged
merged 1 commit into from
Aug 30, 2015
Merged

Raise informative error for float/integer exponentiation that yields … #12822

merged 1 commit into from
Aug 30, 2015

Conversation

felipenoris
Copy link
Contributor

…Complex

Same as #3024.
Deals with the following cases:

julia> (-2)^(2.2)
julia> (-2.0)^(2.2)

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 .")
Copy link
Member

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."

Copy link
Member

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 .......")

@felipenoris
Copy link
Contributor Author

@stevengj , thanks for the input.

stevengj added a commit that referenced this pull request Aug 30, 2015
Raise informative error for float/integer exponentiation that yields …
@stevengj stevengj merged commit dea65d7 into JuliaLang:master Aug 30, 2015
@stevengj
Copy link
Member

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. ",
Copy link
Member

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.

Copy link
Contributor Author

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

stevengj added a commit that referenced this pull request Aug 30, 2015
@felipenoris felipenoris deleted the fn/raise_complex branch August 30, 2015 02:32
@tkelman
Copy link
Contributor

tkelman commented Aug 30, 2015

Seems okay to me, I don't think this would break anything.

@stevengj
Copy link
Member

@felipenoris, the original code had println() ... print() which does not append a newline at the very end. You replaced this with println(...), which does append a newline at the very end.

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

Successfully merging this pull request may close these issues.

3 participants