From bd936843d1e7cf8cf79de96535bf8ed221bb3ca0 Mon Sep 17 00:00:00 2001 From: kimikage Date: Tue, 13 Oct 2026 01:59:16 +0900 Subject: [PATCH] Fix `floattype()` for non-concrete types while preserving the depwarn --- src/FixedPointNumbers.jl | 5 ++--- test/fixed.jl | 1 + test/normed.jl | 1 + test/traits.jl | 16 +++++++++++++++- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/FixedPointNumbers.jl b/src/FixedPointNumbers.jl index 7bafaca2..e58f26ee 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 d0909c3b..1b5fdd86 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 50b60093..28974e35 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 c248b4c0..e832b352 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