-
-
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
Reliability of rational arithmetic #11736
Comments
@khinsen I did bring up the issue of how the arithmetic in julia is unchecked, but I just looked at the code for rational numbers, and they are supposed to be used checked arithmetic (i.e. |
|
A reproducible standalone checked-math failure for |
It's issue #4905, and is fixed upstream. |
See #11522. |
Also #2960 |
Thanks @mbauman; I didn't have the right search term for that. |
@pao: I am not familiar enough with Julia internals to prepare a bug report about checked Int128 arithmetic, sorry! If it's a fixed problem in LLVM, I guess we just have to wait anyway. @simonbyrne: After reading through #11522, I understand that some people are interested in high-performance rational arithmetic within the limits of Int64 denominators, and that this somehow relates to interpolation, but I didn't find any more explicit reference to a use case. |
@khinsen That issue was prompted by JuliaMath/Interpolations.jl#37 |
That was #4905 that @mbauman mentioned, so no need. It would have amounted to "here's a small reproducible case for a failed checked arithmetic call." Nothing scary! |
Given that we probably don't want to promote to |
Closed by #14362. |
Can we add this as a test? |
Consider the following code that computes a cubic root using Newton's method from a given initial value:
Let's do this in the standard way, using floating-point arithmetic (
Complex128
):The result is
-0.5185349749124928 - 0.8466618927868804im
, which is indeed one of the cubic roots of 1.Since the root that the iteration converges to is hard to predict, let's try again using rational arithmetic:
Bad luck:
This can be tracked down to the usual issue with rational arithmetic: the denominators of all the intermediate results grow rapidly and overflow the
Int64
that is available for storage. Don't even consider running this on a 32 bit machine!So let's try
BigInt
:with the result:
That's what I think everyone would have expected to get - but it takes considerable care and ugly syntax to get there.
Finally, let's see the remaining intermediate option,
Int128
:The result is surprising:
1//1 + 0//1*im
. I suspect this is due to denominator overflow as well, but it's much worse because it leads to a wrong result.Question: are there any situations where arithmetic
Rational{Int64}
is a good choice?In my personal experience, rational arithmetic is mainly used for infinite-precision computations, where correctness is the top priority. This implies that
Rational{BigInt}
should be the default, in particular for the result of `//``.The text was updated successfully, but these errors were encountered: