Skip to content

Commit 4f81c47

Browse files
authored
Merge pull request #54 from JuliaMath/teh/coverage
Turn on coverage-testing for Julia 0.6, add README badges
2 parents 020c74f + 0e38c23 commit 4f81c47

4 files changed

Lines changed: 33 additions & 15 deletions

File tree

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ notifications:
1111
script:
1212
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
1313
- julia -e 'Pkg.clone(pwd()); Pkg.build("FixedPointNumbers")'
14-
- julia -e 'cd(Pkg.dir("FixedPointNumbers", "test")); include("runtests.jl")'
14+
- julia -e 'if VERSION >= v"0.6.0-dev.565" Pkg.test("FixedPointNumbers"; coverage=true); else cd(Pkg.dir("FixedPointNumbers", "test")); include("runtests.jl"); end '
15+
after_success:
16+
# push coverage results to Codecov
17+
- julia -e 'if VERSION >= v"0.6.0-dev.565" cd(Pkg.dir("FixedPointNumbers")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder()); end'

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# FixedPointNumbers
22

3-
This library exports fixed-point number types. A
3+
[![Build Status](https://travis-ci.org/JuliaMath/FixedPointNumbers.jl.svg?branch=master)](https://travis-ci.org/JuliaMath/FixedPointNumbers.jl)
4+
5+
[![codecov.io](http://codecov.io/github/JuliaMath/FixedPointNumbers.jl/coverage.svg?branch=master)](http://codecov.io/github/JuliaMath/FixedPointNumbers.jl?branch=master)
6+
7+
This library implements fixed-point number types. A
48
[fixed-point number][wikipedia] represents a fractional, or
59
non-integral, number. In contrast with the more widely known
610
floating-point numbers, with fixed-point numbers the decimal point

src/ufixed.jl

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ end
1111
rawtype{T,f}(::Type{UFixed{T,f}}) = T
1212
rawtype(x::Number) = rawtype(typeof(x))
1313
nbitsfrac{T,f}(::Type{UFixed{T,f}}) = f
14-
nbitsfrac(x::Number) = nbitsfract(typeof(x))
1514

1615
typealias UFixed8 UFixed{UInt8,8}
1716
typealias UFixed10 UFixed{UInt16,10}
@@ -42,10 +41,12 @@ rawone(v) = reinterpret(one(v))
4241

4342
# Conversions
4443
convert{T<:UFixed}(::Type{T}, x::T) = x
45-
function convert{T<:UFixed}(::Type{T}, x::UFixed)
46-
y = round((rawone(T)/rawone(x))*reinterpret(x))
47-
(0 <= y) & (y <= typemax(rawtype(T))) || throw_converterror(T, x)
48-
reinterpret(T, _unsafe_trunc(rawtype(T), y))
44+
convert{T1,T2,f}(::Type{UFixed{T1,f}}, x::UFixed{T2,f}) = UFixed{T1,f}(convert(T1, x.i), 0)
45+
function convert{T,f}(::Type{UFixed{T,f}}, x::UFixed)
46+
U = UFixed{T,f}
47+
y = round((rawone(U)/rawone(x))*reinterpret(x))
48+
(0 <= y) & (y <= typemax(T)) || throw_converterror(U, x)
49+
reinterpret(U, _unsafe_trunc(T, y))
4950
end
5051
convert(::Type{UFixed16}, x::UFixed8) = reinterpret(UFixed16, convert(UInt16, 0x0101*reinterpret(x)))
5152
convert{U<:UFixed}(::Type{U}, x::Real) = _convert(U, rawtype(U), x)
@@ -144,14 +145,11 @@ end
144145
# Iteration
145146
# The main subtlety here is that iterating over 0x00uf8:0xffuf8 will wrap around
146147
# unless we iterate using a wider type
147-
if VERSION < v"0.3-"
148-
start{T<:UFixed}(r::Range{T}) = convert(typeof(reinterpret(r.start)+reinterpret(r.step)), reinterpret(r.start))
149-
next{T<:UFixed}(r::Range{T}, i::Integer) = (T(i,0), i+reinterpret(r.step))
150-
done{T<:UFixed}(r::Range{T}, i::Integer) = isempty(r) || (i > r.len)
151-
else
152-
start{T<:UFixed}(r::StepRange{T}) = convert(typeof(reinterpret(r.start)+reinterpret(r.step)), reinterpret(r.start))
153-
next{T<:UFixed}(r::StepRange{T}, i::Integer) = (T(i,0), i+reinterpret(r.step))
154-
done{T<:UFixed}(r::StepRange{T}, i::Integer) = isempty(r) || (i > reinterpret(r.stop))
148+
@inline start{T<:UFixed}(r::StepRange{T}) = widen1(reinterpret(r.start))
149+
@inline next{T<:UFixed}(r::StepRange{T}, i::Integer) = (T(i,0), i+reinterpret(r.step))
150+
@inline function done{T<:UFixed}(r::StepRange{T}, i::Integer)
151+
i1, i2 = reinterpret(r.start), reinterpret(r.stop)
152+
isempty(r) | (i < min(i1, i2)) | (i > max(i1, i2))
155153
end
156154

157155
function decompose(x::UFixed)

test/ufixed.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ for T in (FixedPointNumbers.UF..., UF2...)
8484
@test convert(Rational, one(T)) == 1
8585
end
8686
@test convert(Rational, convert(UFixed8, 0.5)) == 0x80//0xff
87+
@test convert(UFixed16, one(UFixed8)) === one(UFixed16)
88+
@test convert(UFixed16, UFixed8(0.5)).i === 0x8080
89+
@test convert(UFixed{UInt16,7}, UFixed{UInt8,7}(0.504)) === UFixed{UInt16,7}(0.504)
90+
91+
@test UFixed8(0.2) % UFixed8 === UFixed8(0.2)
92+
@test UFixed14(1.2) % UFixed16 === UFixed16(0.20002)
93+
@test UFixed14(1.2) % UFixed8 === UFixed8(0.196)
8794

8895
for i = 0.0:0.1:1.0
8996
@test i % UFixed8 === UFixed8(i)
@@ -176,6 +183,12 @@ end
176183
r = 1uf8:1uf8:48uf8
177184
@test length(r) == 48
178185

186+
counter = 0
187+
for x in UFixed8(0):eps(UFixed8):UFixed8(1)
188+
counter += 1
189+
end
190+
@test counter == 256
191+
179192
# Promotion within UFixed
180193
@test @inferred(promote(UFixed8(0.2), UFixed8(0.8))) ===
181194
(UFixed8(0.2), UFixed8(0.8))

0 commit comments

Comments
 (0)