Skip to content

Commit 045b3f0

Browse files
authored
Merge pull request #49 from JeffBezanson/teh/unchecked
Add unchecked conversions
2 parents b8506b5 + 4e96201 commit 045b3f0

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

src/fixed.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ convert{T,f}(::Type{Fixed{T,f}}, x::Integer) = Fixed{T,f}(round(T, convert(widen
3434
convert{T,f}(::Type{Fixed{T,f}}, x::AbstractFloat) = Fixed{T,f}(round(T, trunc(widen1(T),x)<<f + rem(x,1)*(1<<f)),0)
3535
convert{T,f}(::Type{Fixed{T,f}}, x::Rational) = Fixed{T,f}(x.num)/Fixed{T,f}(x.den)
3636

37+
rem{T,f}(x::Integer, ::Type{Fixed{T,f}}) = Fixed{T,f}(rem(x,T)<<f,0)
38+
rem{T,f}(x::Real, ::Type{Fixed{T,f}}) = Fixed{T,f}(rem(Integer(trunc(x)),T)<<f + rem(Integer(round(rem(x,1)*(1<<f))),T),0)
39+
3740
convert{T,f}(::Type{BigFloat}, x::Fixed{T,f}) =
3841
convert(BigFloat,x.i>>f) + convert(BigFloat,x.i&(1<<f - 1))/convert(BigFloat,1<<f)
3942
convert{TF<:AbstractFloat,T,f}(::Type{TF}, x::Fixed{T,f}) =

src/ufixed.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ convert{U<:UFixed}(::Type{U}, x::Real) = _convert(U, rawtype(U), x)
4747
_convert{U<:UFixed,T}(::Type{U}, ::Type{T}, x) = U(round(T, widen1(rawone(U))*x), 0)
4848
_convert{U<:UFixed }(::Type{U}, ::Type{UInt128}, x) = U(round(UInt128, rawone(U)*x), 0)
4949

50+
rem{T<:UFixed}(x::T, ::Type{T}) = x
51+
rem{T<:UFixed}(x::UFixed, ::Type{T}) = reinterpret(T, rem(unsafe_trunc(UInt, round((rawone(T)/rawone(x))*reinterpret(x))), rawtype(T)))
52+
rem{T<:UFixed}(x::Real, ::Type{T}) = reinterpret(T, rem(unsafe_trunc(Int, round(rawone(T)*x)), rawtype(T)))
5053

5154
convert(::Type{BigFloat}, x::UFixed) = reinterpret(x)*(1/BigFloat(rawone(x)))
5255
function convert{T<:AbstractFloat}(::Type{T}, x::UFixed)

test/fixed.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ for (TI, f) in [(Int8, 8), (Int16, 8), (Int16, 10), (Int32, 16)]
6464
test_fixed(T, f)
6565
end
6666

67+
T = Fixed{Int8,7}
68+
for i = -1.0:0.1:typemax(T)
69+
@test i % T === T(i)
70+
end
71+
@test ( 1.5 % T).i == round(Int, 1.5*128) % Int8
72+
@test (-0.3 % T).i == round(Int, -0.3*128) % Int8
73+
74+
T = Fixed{Int16,9}
75+
for i = -64.0:0.1:typemax(T)
76+
@test i % T === T(i)
77+
end
78+
@test ( 65.2 % T).i == round(Int, 65.2*512) % Int16
79+
@test (-67.2 % T).i == round(Int, -67.2*512) % Int16
80+
6781
# reductions
6882
F8 = Fixed{Int8,8}
6983
a = F8[0.498, 0.1]

test/ufixed.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ for T in (FixedPointNumbers.UF..., UF2...)
8181
end
8282
@test convert(Rational, convert(UFixed8, 0.5)) == 0x80//0xff
8383

84+
for i = 0.0:0.1:1.0
85+
@test i % UFixed8 === UFixed8(i)
86+
end
87+
@test ( 1.5 % UFixed8).i == round(Int, 1.5*255) % UInt8
88+
@test (-0.3 % UFixed8).i == round(Int, -0.3*255) % UInt8
89+
90+
for i = 0.0:0.1:64.0
91+
@test i % UFixed10 === UFixed10(i)
92+
end
93+
@test (65.2 % UFixed10).i == round(Int, 65.2*1023) % UInt16
94+
@test (-0.3 % UFixed10).i == round(Int, -0.3*1023) % UInt16
95+
8496
x = UFixed8(0b01010001, 0)
8597
@test ~x == UFixed8(0b10101110, 0)
8698
@test -x == 0xafuf8

0 commit comments

Comments
 (0)