Zero() and TwicePrecision

Hello! I’ve recently found a weird behaviour of StepLenRange. Consider:

N = 10
faces = range(start=0, stop=1, length=(N + 1))
cells = range(start=(0 + faces.step), step=faces.step, length=N)

On my computer (julia v.1.7.2) it produces the following error:

ERROR: MethodError: no method matching zero(::Base.TwicePrecision{Float64})
Stacktrace:
 [1] _rangestyle(#unused#::Base.Unordered, #unused#::Base.ArithmeticUnknown, a::Base.TwicePrecision{Float64}, step::Base.TwicePrecision{Float64}, len::Int64)
   @ Base .\range.jl:168
 [2] range_start_step_length(a::Base.TwicePrecision{Float64}, step::Base.TwicePrecision{Float64}, len::Int64)

So, I think the problem with 0 + faces.step. But is this a bug or a feature?:slight_smile:

EDIT:
the following works well:

x = 0:0.1:1
step = x.step
0 + x.step

which produces:

Base.TwicePrecision{Float64}(0.1, -5.551115123125763e-18)

The fields of StepLenRange, like the fields of pretty much all other types in Base, and thoughout the julia ecosystem are not part of the public API for that type.
And the Base.TwicePrecision type is intentionally not part of the public API for Base.

This works fine if you use the same step accessor function.

julia> N = 10
10

julia> faces = range(start=0, stop=1, length=(N + 1))
0.0:0.1:1.0

julia> cells = range(start=(0 + step(faces)), step=step(faces), length=N)
0.1:0.1:1.0

The Base.TwicePrecision type is an implementation detail.
It’s intentionally very limitted and doesn’t subtype Number because things don’t work with it in general.

6 Likes

Thank you!
I didn’t really know about

accessor functions

and now I’m reading about

public API

and how to recognize it :slight_smile:

3 Likes