Skip to content
Merged
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))

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.

While there are doubts regarding the guarantee that this returns an AbstractFloat, that is an issue to be addressed by the package that defined the type.

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)}
Expand Down
8 changes: 0 additions & 8 deletions src/deprecations.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import Base.@deprecate_binding

function floattype(::Type{T}) where {T <: Real}
Base.depwarn("""
In a future release, the fallback definition of `floattype` will throw a MethodError if it cannot return a type `<:AbstractFloat`.
See the documentation on `floattype` for guidance on whether to define a custom `floattype(::Type{$T})` method.
""", :floattype)
return T
end

function typechar(::Type{X}) where {X}
Base.depwarn("""
`typechar` was deprecated since the prefix may not be a single character in the future.
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
18 changes: 16 additions & 2 deletions 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 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
@test_throws MethodError floattype(Fixed{T,3} where T) # T is not bounded

@testset "floattype" begin
for T in (UInt8, UInt16, UInt32, UInt64, UInt128, Bool,
Int8, Int16, Int32, Int64, Int128)
@test typemax(T) <= maxintfloat(floattype(T))
Expand All @@ -16,5 +29,6 @@ struct MyReal <: Real end
@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.
@test_throws MethodError floattype(MyReal) # See #177.
@test floattype(MyInteger) === Float64
end
Loading