Skip to content

Commit

Permalink
Provide floattype methods for non-Real types
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Mar 18, 2020
1 parent 186aa63 commit e1c9cbf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/FixedPointNumbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ floattype(::Type{X}) where {T <: Integer, X <: FixedPoint{T}} = Float64
floattype(::Type{X}) where {T <: Integer, X <: Rational{T}} = typeof(zero(T)/oneunit(T))
floattype(::Type{X}) where {T <: LongInts, X <: FixedPoint{T}} = BigFloat

# Non-Real types
floattype(::Type{Complex{T}}) where T = Complex{floattype(T)}
floattype(::Type{<:Irrational}) = Float64
floattype(::Type{Base.TwicePrecision{Float64}}) = Float64 # wider would be nice, but hardware support is paramount
floattype(::Type{Base.TwicePrecision{T}}) where T<:Union{Float16,Float32} = widen(T)

float(x::FixedPoint) = convert(floattype(x), x)

function minmax(x::X, y::X) where {X <: FixedPoint}
Expand Down
6 changes: 6 additions & 0 deletions test/traits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ struct MyReal <: Real end
@test typemax(T) <= maxintfloat(floattype(T))
end
@test floattype(Rational{Int}) === Float64
@test floattype(Complex{Int16}) === Complex{Float32}
@test floattype(Complex{Float32}) === Complex{Float32}
@test floattype(Base.TwicePrecision{Float16}) === Float32
@test floattype(Base.TwicePrecision{Float32}) === Float64
@test floattype(Base.TwicePrecision{Float64}) === Float64
@test floattype(typeof(π)) === Float64

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

0 comments on commit e1c9cbf

Please sign in to comment.