Julia: Base.TwicePrecision{Float64} <: Real == false

Created on 21 May 2019  路  3Comments  路  Source: JuliaLang/julia

https://github.com/JuliaLang/julia/blob/a43c46b68796228838041a140356f96e6f76184b/base/twiceprecision.jl#L184-L187

Is there a good reason why TwicePrecision{Float64} shouldn't also be a subtype of Real?
If so, how should one specify that why want a StepRangeLen where R and S might be regular reals or a TwicePrecision version?

Most helpful comment

If you want "regular reals," use StepRangeLen(ref, step, len) directly. If you want TwicePrecision, use range(start, step=step, length=len).

TwicePrecision is an internal type designed to enable hitting the endpoints of Float64 ranges accurately. If we start turning it into more of a regular number, keep in mind we might eventually need QuadPrecision to enable precise TwicePrecision range operations. I'd rather not go down that road, personally.

All 3 comments

If you want "regular reals," use StepRangeLen(ref, step, len) directly. If you want TwicePrecision, use range(start, step=step, length=len).

TwicePrecision is an internal type designed to enable hitting the endpoints of Float64 ranges accurately. If we start turning it into more of a regular number, keep in mind we might eventually need QuadPrecision to enable precise TwicePrecision range operations. I'd rather not go down that road, personally.

My problem is that I'm trying to create struct's and functions that have types based on the types in a parameter which is a range. If the range happens to be using a TwicePrecission, I still want it to call the same version of the function as would normally be called if it were a real. I guess I could do something like
T1 although I was hoping there was a clearner way.

That doesn't look terribly messy to me. But if you don't want to have to deal with even acknowledging the existence of TwicePrecision, you may not need to:

julia> r = 1.2:2.8:16.4
1.2:2.8:15.2

julia> eltype(r)
Float64

julia> typeof(r)
StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}

julia> supertype(typeof(r))
AbstractRange{Float64}

You might be able to write all your methods to dispatch on AbstractRange{T} where T and just use the generic interface functions first, step, last.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dpsanders picture dpsanders  路  3Comments

omus picture omus  路  3Comments

yurivish picture yurivish  路  3Comments

sbromberger picture sbromberger  路  3Comments

StefanKarpinski picture StefanKarpinski  路  3Comments