-
-
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
[RFC] What is the integer overflow story in Julia? #50486
Comments
See also #21600. |
Regarding a policy for the treatment of integer overflow. It is easier to make the case for treating unsigned bitstypes as modular values (permitting operations to wraparound either way) than to make a compelling case for treating signed bitstypes as modular values. Many uses of unsigned bitstypes are designed with a reliance on the presence of unchecked wraparound. It is rare to design an algorithm that requires unchecked wraparound for signed bitstypes. The only common rationale for allowing signed bitstypes to wrap (over or under) is that provides accelerated throughput and the associated simplification of instruction flow is easier to optimize. Integer Wraparound is #14 on the 2023 CWE list of Top 25 Most Dangerous Software Weaknesses
|
I think of Therefore, I believe the following equality should hold for all basic integer operations, or they should op(x::Int64, y::Int64) == op(big(x), big(y)) % Int64 For example, one case where the above equality doesn't work is with
But I guess this is okay because the docstring for Though I'm not sure why |
That's because arguably Integer ^ Integer should in general be Float64 (if not Rational), one of few flaws in Julia (only exception when b of a ^ b is Unsigned, then I can argue for the status quo), for the same reason Integer / Integer is Float64 (the fast approximation of Rational), which is a good decision. Such a decision would have averted overflows or (costly software) checks (and is only done at parser time for constants), and I would like to see it changed for Julia 2.0 (see other issue on it, and what can be done for 1.x, let's not discuss this here). Insisting on Int result (for Signed b), is arguably faulty math, and I don't think many would rely on that very much. Then we wouldn't need to warn: This is one reason I want 2.0, to break (but in a good way), this integer result, most or all would be better off, I don't think ^ is very much used anyway for integers except for Unsigned in number theory and wouldn't be affected.
Since negative power gives a fraction, in general not an integer, why you should do (and only the above for positive power):
Right. And since you need to opt into them, work harder, I would supported them not checking for overflows. For (Signed) Integer you could actually have avoided any possibility of overflows, and thus checks. If just e.g. Int32 * Int32 -> Int64 (fast; no checks) and Int64 * Int64 -> Int128 (not as fast, nor for subsequent math, why I would prefer Int32 the default in Julia, also on 64-bit platforms...). |
That makes sense for Julia 2.0, but in general, |
For simple operations (e.g.
+
,*
,-
) we do modular arithmetic and do not warn on overflow.For complex operations, we sometimes check for intermediate overflow (
lcm
,getindex
,binomial
) and sometimes do not, (isapprox
,lcm
)I'd like a clear, documented, policy of when we let overflow happen, when we throw, and when we make sure to avoid it with widen or similar.
The first step is to come up with the policy. Current documentation is quite limited. Ideas?
The text was updated successfully, but these errors were encountered: