-
-
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
consistent eltypes for all scalar+scalar, scalar+array, array+array ops #14252
Comments
I have now tried probably all of the standard combinations (with v4.1), but not the more "esoteric" ones (with rational or complex). scalar+scalar operations seem to be already in line with array+array behaviors. An outlier is array+scalar, where an integer array is not promoted if the scalar is a larger integer. I had stumbled over this behaviour. For Int_n_ + UInt_m_, where n,m in 8,16,32, scalar+scalar and array+array promote to always Int64, which can be a bit wasteful, but is probably rarely used: julia> Int8[1]+UInt8[2] Int16 would be sufficient in this case. For array+scalar there is no promotion, so some alignment would be needed. |
I think that's intended, as promoting the element type of the array could result in substantially more memory allocation for the result and may not be ideal to do unless specifically asked for. I thought we changed the integer promotion rules, but maybe not when combining signed with unsigned? |
This is kind of a mess but it is consistent between scalars and arrays: julia> typeof(Int8(1) + Int8(2))
Int8
julia> typeof(Int8[1] + Int8[2])
Array{Int8,1}
julia> typeof(Int8(1) + UInt8(2))
Int64
julia> typeof(Int8[1] + UInt8[2])
Array{Int64,1}
julia> typeof(Int8(1) + Int16(2))
Int16
julia> typeof(Int8[1] + Int16[2])
Array{Int16,1} |
As a user I would expect and prefer, if also array+scalar were consistent with array+array and scalar+scalar. If I wanted to avoid getting an in memory large array, I had used e.g. julia> repmat(Int8[0:126;], 1000000, 1)+Int8(1) instead of julia> repmat(Int8[0:126;], 1000000, 1)+1 In summary, a "high degree of consistency" could be achieved with the following changes:
|
+1 |
Would it be possible to also have a parallel set of "safe" promotion rules, that would be guaranteed to return a type that could fit any value from either, without truncation or rounding, UInt128+Int128 -> BigInt, UInt16+UInt16->UInt16, Int8+UInt8->Int16, Int64+Float64->BigFloat, etc.)? |
The checked versions of arith ops checked_add, checked_sub, checked_mul do alert. |
No, I was just talking about promotion rules, so that you don't get things like this silently happening: |
going to take a shot at this |
@rawls238 Are you working on this ? If not I would like to. |
@StefanKarpinski So what is the final thing to do for this issue? |
The open question seems to be what to do in the same-size, mixed signedness case, e.g. |
I agree. Julia views UInts more as machine numbers than as non-negative integers. Promoting Int + UInt => UInt puts the higher level type (Int, a subset of the Integers) under the lower level type (UInt). That is inappropriate; also people using UInts as non-negative integers would be surprised to see the signedness of Int in Int + UInt go away (as it does currently) while people using UInts as machine numbers generally know what's up with promotion rules. |
There's also the argument that it's much easier to do a computation that gets a smallish negative value than a computation that gets a huge value. Obviously, they're both equally possible, but you get what I mean. |
typeof(Int8(5) + UInt(6))
UInt64
julia> a = Int8(5) + UInt8(6)
11
julia> typeof(a)
Int64 Why different promotion here ? |
I'm proposing that they should be the same. |
The reasoning for the current behavior is that |
At last count, there were no votes for continuing the inconsistency. |
One argument that appeared in the last round of discussions is that all literal numbers are I'd also invert the "higher level type" argument: Clearly, |
Indeed, AFAIK the rule as regards integer sizes is that non-standard types win. |
agree to disagree (my experience with UInts is I need to avoid mixing Ints to keep the calc clean). and how do you alter the background of the type names? |
@JeffreySarnoff with backticks |
I think this is now fixed. |
See discussion here: https://groups.google.com/forum/#!topic/julia-users/hA0FLKqYEOs. We've already brought many scalar+scalar operations in line with the array+array behaviors; we should probably take this all the way so that these behaviors are always consistent.
The text was updated successfully, but these errors were encountered: