Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/FixedPointNumbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your proposal would return BigFloat for Union{N0f8, N0f16}, which in my read violates the docstring: "In general the returned type should have the minimum bitwidth needed to encode the full precision of the input type."

Not perfect, but I think better:

Suggested change
floattype(::Type{<:FixedPoint}) = BigFloat # fallback for `FixedPoint`
floattype(::Type{T}) where {T <: FixedPoint} =
T isa Union ? promote_type(map(floattype, Base.uniontypes(T))...) : BigFloat

@kimikage kimikage Oct 18, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as non-concrete types are concerned, I don't think that document makes much sense.
BTW, have you read this comment? #343 (comment)
As you've probably noticed, we can create various edge cases (e.g., Union{N0f8, Float32}).

floattype(::Type{<:FixedPoint{T}}) where {T <: Integer} = isconcretetype(T) ? floattype(T) : BigFloat

# Non-Real types
floattype(::Type{Complex{T}}) where T = Complex{floattype(T)}
Expand Down
1 change: 1 addition & 0 deletions test/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/normed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion test/traits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +8 to +17

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add few non-trivial Union cases, too.

# 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))
Expand All @@ -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
Loading