Weird behavior of range with .-

I came around the following behavior using range I did not expect:

creating the range specified below and subtracting its last element results in a range with a fairly wide difference to 0 at the last element.

a = range(0, 100, length=50)

julia> b = 100.

julia> a .- b

julia> a[end] - b

this behavior is also true for a .- a[end]. However

julia> c = collect(range(0, 100, length=50))

julia> (c .- b)[end]

gives 0.0 as I would expect.

Does anybody know were this difference come from? I’m using version 1.5.0 on Linux

looks like it boils down to this line:

and running it manually:

julia> a = range(0, 100, length=50)

julia> StepRangeLen{Float64}(a.ref - 100., a.step, length(a), a.offset)

my guess is that it’s because a.step is a TwicePrecision and some floating point number behavior happened along the way.


You might be able to avoid this kind of issue if you did something like


this problem would likely go away. I admit that this would make your array have length 51 instead of 50.

Well, the problem is: when what you expect is 0., any relative difference is “fairly wide”.

What happens here is a cancellation, i.e. the subtraction of two numbers that are very close to one another, resulting in a large increase of the relative error (but substantially smaller increase of the absolute error).

StepRange objects store their start point (0, in this case), length (50 in this case) and step size (100/49 in this case). And the endpoint is re-computed from there. In this case, with infinite precision arithmetic it would be:

julia> 49 * (100//49)

i.e. exactly what we expect, no error at all. If the step size was computed with double-precision arithmetic, we’d have something like

julia> 49*BigFloat(100/49)

julia> 49*BigFloat(100/49)-100.

i.e. an absolute error in the order of 1e-15. But Julia goes an extra step along the way to avoid FP errors, and doubles the working precision when computing the range step:

julia> a = range(0, 100, length=50)

julia> a.step
Base.TwicePrecision{Float64}(2.040816326530603, 9.28055818135641e-15)

julia> 49*a.step
Base.TwicePrecision{Float64}(100.0, -5.048709793414476e-29)

julia> 49*a.step - 100.
Base.TwicePrecision{Float64}(-5.048709793414476e-29, 0.0)

In short, the 5e-29 you’re measuring here should not be seen as the (infinite) relative error of a computation returning 0, but rather as an absolute error (rather small if you consider the magnitude of all intermediate results involved).

If you want to avoid such problems with inaccurate end points in ranges, you could perhaps switch to using a LinRange (which stores both end points of the range) instead of a StepRange (which stores the start point in combination with the step size):

julia> a = LinRange(0, 100, 50)
50-element LinRange{Float64}:

julia> b = 100.

julia> c = a .- b
50-element LinRange{Float64}:

julia> c[end]

Tank you for pointing out LinRange, I will use this in my case.

I didn’t expect this absolute error here, because letting the StepRange computing its endpoint and subtracting 100. gives the exact zero.

julia> a = range(1, 100, length=50)

julia> a[end] - 100.

Yes. Let me try to explain: computing a[end] produces the exact same absolute error of 5\times 10^{-29}, i.e. the result computed by the last instruction would be approximately 100 - 5\times10^{-29}. But then this result is rounded to the nearest representable FP number. Which is 100 in this case:

  • 100 is itself representable as a FP number (we know that not-too-large integers are always representable), and
  • the two neighbouring representable FP numbers (given by prevfloat(100.) and nextfloat(100.)) are approximately 100 \mp 1.4\times 10^{-14}.

So the result of a[end], rounded to double precision, is exactly 100. And therefore, as you noted:

julia> a[end] - 100.

In short: because a[end] is (i) not too small and (ii) representable as a FP number, the round-off errors introduced in the computation a.ref + a.len * a.step (which happens with twice the working precision), are so small that they vanish thanks to the last round-off (which happens in double precision).


Okay, that’s it, I missed that in the first case it’s rounded and afterwards subtracted. Thank you for this insight.