Properties of Range

Should I expect that the length of a range is preserved under arithmetic operations on the range? I ran into a bug where simply changing the sign in front of a range in an expression caused a dimension mismatch error. For example:

julia> collect((22:-1:7)-0.0030026930763747783)
15-element Array{Float64,1}:
 21.997
 20.997
 19.997
 18.997
 17.997
 16.997
 15.997
 14.997
 13.997
 12.997
 11.997
 10.997
  9.997
  8.997
  7.997

julia> collect((22:-1:7))-0.0030026930763747783
16-element Array{Float64,1}:
 21.997
 20.997
 19.997
 18.997
 17.997
 16.997
 15.997
 14.997
 13.997
 12.997
 11.997
 10.997
  9.997
  8.997
  7.997
  6.997

After some experimentation, I discovered, at least in this example, if the range is floating point, I get the expected behavior. But then you get the weird property that the following are not the same. Also, I don’t know if the length preservation holds in general.

# Should these be the same?
((22:-1:7)*1.0)-0.0030026930763747783
(22:-1:7)-0.0030026930763747783
1 Like

This is definitely a bug, I’ve opened an issue: https://github.com/JuliaLang/julia/issues/25521