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

Added helpfull message for DomainError #4249

Merged
merged 1 commit into from
Sep 18, 2013

Conversation

ivarne
Copy link
Member

@ivarne ivarne commented Sep 10, 2013

Some functions throw DomainError for Real arguments because of type
stability and that the correct answer is complex. This patch add a ugly
hack, inspecting the bactrace, to give newcommers a helpefull hint.

julia> sqrt(-1)
ERROR: DomainError
    sqrt will only return complex result if called width a complex argument. Eg: sqrt(complex(x))

julia> log(-1)
ERROR: DomainError
    log will only return complex result if called width a complex argument. Eg: log(complex(x))

See: #2144

I hate that this approach includes a list of functions (:log, :log2, :log10, :sqrt) that has to be updated and can not be extended by module writers, but from the discussion in #2144 it looks like something that might be accepted.

@JeffBezanson
Copy link
Member

I'm ok with this approach since it is so non-invasive. It needs some tweaks though. It needs a break so it can only print the message once per backtrace, and it should also only print the message if the relevant function is the first non-empty entry in the backtrace. Lastly I think a call to show_backtrace is needed, since defining the 3-argument form of showerror overrides the normal way that call gets done.

Some functions throw DomainError for Real arguments because of type
stability and that the correct answer is complex. This patch add a ugly
hack, inspecting the bactrace, to give newcommers a helpefull hint. It
also document some of the functions that throws DomainError for negative
Real arguments.
@ivarne
Copy link
Member Author

ivarne commented Sep 13, 2013

Thanks for the comments. I have now updated the PR. (Sorry for messing with the history, it felt like a good idea, but it doesn't look right)

I didn't add the Bessel functions because I only know them by name and did not want to do anything wrong. They also have more than one parameter so my message would be wrong. There is also a bunch of trigonometric functions that might throw DomainError, but I have never used them to get complex results, and don't know how that would work.

Does anybody want to add a function to the list of DomainError messages?

JeffBezanson added a commit that referenced this pull request Sep 18, 2013
Added helpfull message for DomainError
@JeffBezanson JeffBezanson merged commit fb18349 into JuliaLang:master Sep 18, 2013
@ivarne ivarne deleted the DomainError_help branch December 10, 2013 12:42
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.

2 participants