`convert` truncates integers #5413

pygy opened this Issue Jan 15, 2014 · 4 comments


None yet

3 participants

pygy commented Jan 15, 2014
d = (Bool=>Int8)[]
d[true] = 2345 # => 2345
d[true] # => 41

The culprit lies here. The key is checked for truncation, but not the value.


We could check that values are isequal after conversion for Dicts without incurring substantial overhead relative to the cost of hashing, but I'm not sure that really makes sense, since Arrays do the same thing:

julia> d = Array(Int8, 1);

julia> d[1] = 2345

julia> d[1]

The underlying reason for this behavior is that convert(Int8, 2345) doesn't throw, even though 2345 can't be expressed as an Int8. This came up previously in #4166 and @StefanKarpinski expressed some misgivings about the present convert behavior, but we decided not to change it then.


Yes, this is about convert truncating integers. Given the definition of convert, it is clear that this is incorrect, but it just happens that one very often wants to truncate integers. We should probably bite the bullet and change it.


See also #7517. When this is changed, itrunc should be changed too.

@JeffBezanson JeffBezanson added this to the 0.4 milestone Jul 4, 2014
@JeffBezanson JeffBezanson added a commit that referenced this issue Sep 17, 2014
@JeffBezanson JeffBezanson check integer truncation (#5413) and make more operators follow T+T =…
…> T (#3759)

the sysimg builds, but still need to:
  - check same-size signed<->unsigned conversion
  - make tests pass

closed by #8420

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment