Skip to content

Commit 15346e8

Browse files
committed
Make floattype depend on the total number of bits, not just fractional bits
This ensures that conversion is lossless even for the largest representable values.
1 parent 71eb75e commit 15346e8

File tree

3 files changed

+6
-11
lines changed

3 files changed

+6
-11
lines changed

src/FixedPointNumbers.jl

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,10 @@ widen1(::Type{Int64}) = Int128
8484
widen1(::Type{UInt64}) = UInt128
8585
widen1(x::Integer) = x % widen1(typeof(x))
8686

87-
if VERSION <= v"0.5.0-dev+755"
88-
@generated function floattype{T,f}(::Type{FixedPoint{T,f}})
89-
f>22 ? :(Float64) : :(Float32)
90-
end
91-
else
92-
@pure function floattype{T,f}(::Type{FixedPoint{T,f}})
93-
f>22 ? Float64 : Float32
94-
end
95-
end
87+
typealias ShortInts Union{Int8,UInt8,Int16,UInt16}
88+
89+
floattype{T<:ShortInts,f}(::Type{FixedPoint{T,f}}) = Float32
90+
floattype{T,f}(::Type{FixedPoint{T,f}}) = Float64
9691
floattype(x::FixedPoint) = floattype(supertype(typeof(x)))
9792

9893

test/fixed.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ end
103103

104104
# Floating-point conversions
105105
@test isa(float(one(Fixed{Int8,6})), Float32)
106-
@test isa(float(one(Fixed{Int32,18})), Float32)
106+
@test isa(float(one(Fixed{Int32,18})), Float64)
107107
@test isa(float(one(Fixed{Int32,25})), Float64)
108108

109109
# Show

test/ufixed.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ x = UFixed8(0b01010001, 0)
112112
@test -x == 0xafuf8
113113

114114
@test isa(float(one(UFixed{UInt8,7})), Float32)
115-
@test isa(float(one(UFixed{UInt32,18})), Float32)
115+
@test isa(float(one(UFixed{UInt32,18})), Float64)
116116
@test isa(float(one(UFixed{UInt32,25})), Float64)
117117

118118
for T in (FixedPointNumbers.UF..., UF2...)

0 commit comments

Comments
 (0)