diff --git a/src/FixedPointNumbers.jl b/src/FixedPointNumbers.jl index 7bafaca..e58f26e 100644 --- a/src/FixedPointNumbers.jl +++ b/src/FixedPointNumbers.jl @@ -196,9 +196,8 @@ floattype(::Type{T}) where {T <: Integer} = Float64 floattype(::Type{T}) where {T <: LongInts} = BigFloat floattype(::Type{T}) where {I <: Integer, T <: Rational{I}} = typeof(zero(I)/oneunit(I)) floattype(::Type{<:AbstractIrrational}) = Float64 -floattype(::Type{X}) where {T <: ShortInts, X <: FixedPoint{T}} = Float32 -floattype(::Type{X}) where {T <: Integer, X <: FixedPoint{T}} = Float64 -floattype(::Type{X}) where {T <: LongInts, X <: FixedPoint{T}} = BigFloat +floattype(::Type{<:FixedPoint}) = BigFloat # fallback for `FixedPoint` +floattype(::Type{<:FixedPoint{T}}) where {T <: Integer} = isconcretetype(T) ? floattype(T) : BigFloat # Non-Real types floattype(::Type{Complex{T}}) where T = Complex{floattype(T)} diff --git a/test/fixed.jl b/test/fixed.jl index d0909c3..1b5fdd8 100644 --- a/test/fixed.jl +++ b/test/fixed.jl @@ -245,6 +245,7 @@ end @test float(0.75Q10f53)::BigFloat == big"0.75" test_floattype(Fixed) + # see test/traits.jl for floattype tests for non-concrete types end @testset "conversions from float" begin diff --git a/test/normed.jl b/test/normed.jl index 50b6009..28974e3 100644 --- a/test/normed.jl +++ b/test/normed.jl @@ -210,6 +210,7 @@ end @test float(1N11f53)::BigFloat == big"1.0" test_floattype(Normed) + # see test/traits.jl for floattype tests for non-concrete types end @testset "conversions from float" begin diff --git a/test/traits.jl b/test/traits.jl index c248b4c..e832b35 100644 --- a/test/traits.jl +++ b/test/traits.jl @@ -2,8 +2,21 @@ using FixedPointNumbers, Test using FixedPointNumbers: bitwidth struct MyReal <: Real end +struct MyInteger <: Integer end -@testset "floattype" begin +@testset "floattype for non-concrete/non-FixedPoint types" begin + @test floattype(FixedPoint) === BigFloat + @test floattype(Normed) === BigFloat + @test floattype(Fixed) === BigFloat + @test floattype(FixedPoint{Int8}) === Float32 + @test floattype(Normed{UInt32}) === Float64 + @test floattype(Fixed{Int64}) === BigFloat + @test floattype(FixedPoint{MyInteger}) === Float64 + @test floattype(FixedPoint{Union{},1}) === BigFloat + @test floattype(Normed{Unsigned,2}) === BigFloat + @test floattype(Fixed{T,3} where {T<:Signed}) === BigFloat + # TODO: eliminate `@test_skipped` when depwarn is eliminated. + @test_skip(@test_throws MethodError floattype(Fixed{T,3} where T)) # T is not bounded for T in (UInt8, UInt16, UInt32, UInt64, UInt128, Bool, Int8, Int16, Int32, Int64, Int128) @test typemax(T) <= maxintfloat(floattype(T)) @@ -17,4 +30,5 @@ struct MyReal <: Real end @test floattype(typeof(π)) === Float64 @test_skip(@test_throws MethodError floattype(MyReal)) # TODO: eliminate `@test_skipped` when depwarn is eliminated. See #177. + @test floattype(MyInteger) === Float64 end