File tree 2 files changed +12
-4
lines changed 2 files changed +12
-4
lines changed Original file line number Diff line number Diff line change @@ -106,15 +106,15 @@ floatmax(::Type{T}) where {T <: FixedPoint} = typemax(T)
106
106
107
107
108
108
"""
109
- floattype(::Type{T})
109
+ floattype(::Type{T})::Type{<:AbstractFloat}
110
110
111
- Return the minimum float type that represents `T` without overflow to `Inf`.
111
+ Return a minimal `AbstractFloat` type suitable for performing computations with instances of type `T` without overflow to `Inf`.
112
112
113
113
# Example
114
114
115
115
A classic usage is to avoid overflow behavior by promoting `FixedPoint` to `AbstractFloat`
116
116
117
- ```julia
117
+ ```jldoctest
118
118
julia> x = N0f8(1.0)
119
119
1.0N0f8
120
120
@@ -128,12 +128,13 @@ julia> float_x + float_x
128
128
2.0f0
129
129
```
130
130
"""
131
- floattype (:: Type{T} ) where {T <: Real } = T # fallback
131
+ floattype (:: Type{T} ) where {T <: AbstractFloat } = T # fallback (deliberately get MethodError if no method producing AbstractFloat defined)
132
132
floattype (:: Type{T} ) where {T <: Union{ShortInts, Bool} } = Float32
133
133
floattype (:: Type{T} ) where {T <: Integer } = Float64
134
134
floattype (:: Type{T} ) where {T <: LongInts } = BigFloat
135
135
floattype (:: Type{X} ) where {T <: ShortInts , X <: FixedPoint{T} } = Float32
136
136
floattype (:: Type{X} ) where {T <: Integer , X <: FixedPoint{T} } = Float64
137
+ floattype (:: Type{X} ) where {T <: Integer , X <: Rational{T} } = typeof (zero (T)/ oneunit (T))
137
138
floattype (:: Type{X} ) where {T <: LongInts , X <: FixedPoint{T} } = BigFloat
138
139
139
140
float (x:: FixedPoint ) = convert (floattype (x), x)
Original file line number Diff line number Diff line change
1
+ using FixedPointNumbers, Test
2
+
3
+ struct MyReal <: Real end
4
+
1
5
@testset " floattype" begin
2
6
function _is_fixed_type (x:: Symbol )
3
7
try
16
20
for T in exact_types
17
21
@test typemax (T) <= maxintfloat (floattype (T))
18
22
end
23
+ @test floattype (Rational{Int}) === Float64
24
+
25
+ @test_throws MethodError floattype (MyReal)
19
26
end
You can’t perform that action at this time.
0 commit comments