Skip to content

enhance and export floattype #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 15, 2019
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
30 changes: 30 additions & 0 deletions src/FixedPointNumbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export
FixedPoint,
Fixed,
Normed,
floattype,
# "special" typealiases
# Q and U typealiases are exported in separate source files
# Functions
Expand Down Expand Up @@ -74,9 +75,38 @@ widen1(::Type{UInt128}) = UInt128
widen1(x::Integer) = x % widen1(typeof(x))

const ShortInts = Union{Int8,UInt8,Int16,UInt16}
const LongInts = Union{UInt64, UInt128, Int64, Int128, BigInt}

"""
floattype(::Type{T})

Return the minimum float type that represents `T` without overflow to `Inf`.

# Example

A classic usage is to avoid overflow behavior by promoting `FixedPoint` to `AbstractFloat`

```julia
julia> x = N0f8(1.0)
1.0N0f8

julia> x + x # overflow
0.996N0f8

julia> float_x = floattype(eltype(x))(x)
1.0f0

julia> float_x + float_x
2.0f0
```
"""
floattype(::Type{T}) where {T <: Real} = T # fallback
floattype(::Type{T}) where {T <: Union{ShortInts, Bool}} = Float32
floattype(::Type{T}) where {T <: Integer} = Float64
floattype(::Type{T}) where {T <: LongInts} = BigFloat
floattype(::Type{FixedPoint{T,f}}) where {T <: ShortInts,f} = Float32
floattype(::Type{FixedPoint{T,f}}) where {T <: Integer,f} = Float64
floattype(::Type{FixedPoint{T,f}}) where {T <: LongInts,f} = BigFloat
floattype(::Type{F}) where {F <: FixedPoint} = floattype(supertype(F))
floattype(x::FixedPoint) = floattype(typeof(x))

Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ end
@testset "fixed" begin
include("fixed.jl")
end

@testset "traits" begin
include("traits.jl")
end
19 changes: 19 additions & 0 deletions test/traits.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@testset "floattype" begin
function _is_fixed_type(x::Symbol)
try
@eval $(x) isa Type && $(x) <: FixedPoint && return true
catch
return false
end
end

fixed_types = setdiff(filter(_is_fixed_type, names(FixedPointNumbers)), [:Fixed, :Normed, :FixedPoint])
fixed_types = [@eval $(x) for x in fixed_types]

exact_types = vcat([UInt8, UInt16, UInt32, UInt64, UInt128, Bool,
Int8, Int16, Int32, Int64, Int128],
fixed_types)
for T in exact_types
@test typemax(T) <= maxintfloat(floattype(T))
end
end