-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Base.factorial throws an uninformative error with large Int #18303
Comments
We could add a reason field to |
The reason we don't do that now is the performance hit this will cause to the non-error branch. |
@yuyichao How about singleton |
The type provides little information regarding the causes of the error. In the above example, saying that the type is |
The type provides a reasonable amount of information, especially in combination with the stack trace. julia> factorial(20000)
ERROR: OverflowError(): Computation cannot be completed because an `Int64` operation would
overflow; consider using big() to perform the calculation on `BigInt` instead
in factorial_lookup at combinatorics.jl:29
julia> 0x01//0x01 - 0x02//0x01
ERROR: OverflowError(): Computation cannot be completed because an `UInt8` operation would
overflow; consider using big() to perform the calculation on `BigInt` instead, or using
`Int8` to perform the calculation on a singed integer.
in -(::Rational{UInt8}, ::Rational{UInt8}) at ./rational.jl:200 |
@TotalVerb's suggestion would make a lot of sense, in my opinion. By the way, may OverflowError be thrown with floating-point numbers? (Just to make sure this case is taken into account.) |
Currently Julia 0.6 and 0.7 (and probably 1.0 but I did not try) still return an OverFlow error when trying to calculate the factorial of 100... It is easily solved by calculating as factorial(big(100)). Any reason why this is not directly implemented in Base? It seems to me that such detail should be transparent to the mainstream user... |
The two-word answer is "type stability". You'll probably want to search on discourse or ask about it. |
I am considering submitting a PR to improve still a bit the error message, to show something like |
When trying to compute the factorial of 20,000, I get an error with the base implementation:
However, this error is not informative enough: it is not easy to determine what Julia means by an "overflow" before thinking a bit about the issue. I think the error message should be more explicit, i.e. say that you should use
big()
somewhere, like this:I did not find a way to add such message when throwing an OverflowError, otherwise I'd have submitted a PR instead (I guess it would require adding a msg field to
OverflowError
just likeErrorException
has one).The text was updated successfully, but these errors were encountered: