Skip to content

Commit 54fda32

Browse files
committed
Provide floattype methods for non-Real types
1 parent f0257ee commit 54fda32

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/FixedPointNumbers.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ floattype(::Type{X}) where {T <: Integer, X <: FixedPoint{T}} = Float64
156156
floattype(::Type{X}) where {T <: Integer, X <: Rational{T}} = typeof(zero(T)/oneunit(T))
157157
floattype(::Type{X}) where {T <: LongInts, X <: FixedPoint{T}} = BigFloat
158158

159+
# Non-Real types
160+
floattype(::Type{Complex{T}}) where T = Complex{floattype(T)}
161+
floattype(::Type{<:Irrational}) = Float64
162+
floattype(::Type{Base.TwicePrecision{Float64}}) = Float64 # wider would be nice, but hardware support is paramount
163+
floattype(::Type{Base.TwicePrecision{T}}) where T<:Union{Float16,Float32} = widen(T)
164+
159165
float(x::FixedPoint) = convert(floattype(x), x)
160166

161167
function minmax(x::X, y::X) where {X <: FixedPoint}

test/traits.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ struct MyReal <: Real end
2121
@test typemax(T) <= maxintfloat(floattype(T))
2222
end
2323
@test floattype(Rational{Int}) === Float64
24+
@test floattype(Complex{Int16}) === Complex{Float32}
25+
@test floattype(Complex{Float32}) === Complex{Float32}
26+
@test floattype(Base.TwicePrecision{Float16}) === Float32
27+
@test floattype(Base.TwicePrecision{Float32}) === Float64
28+
@test floattype(Base.TwicePrecision{Float64}) === Float64
29+
@test floattype(typeof(π)) === Float64
2430

2531
@test_skip(@test_throws MethodError floattype(MyReal)) # TODO: eliminate `@test_skipped` when depwarn is eliminated. See #177.
2632
end

0 commit comments

Comments
 (0)