Skip to content

Commit 930f19a

Browse files
authored
Fix isinteger and Commonize predicates (e.g. isnan) (Fixes #120) (#165)
1 parent bae6542 commit 930f19a

File tree

4 files changed

+62
-9
lines changed

4 files changed

+62
-9
lines changed

src/FixedPointNumbers.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ function isapprox(x::FixedPoint, y::FixedPoint; rtol=0, atol=max(eps(x), eps(y))
6262
end
6363

6464
# predicates
65-
isinteger(x::FixedPoint{T,f}) where {T,f} = (x.i&(1<<f-1)) == 0
65+
isinteger(x::FixedPoint) = x == trunc(x) # TODO: use floor(x) when dropping support for Fixed{Int8,8}
66+
isfinite(x::FixedPoint) = true
67+
isnan(x::FixedPoint) = false
68+
isinf(x::FixedPoint) = false
6669

6770
# identities
6871
zero(::Type{X}) where {X <: FixedPoint} = X(zero(rawtype(X)), 0)

src/normed.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,6 @@ function round(::Type{Ti}, x::Normed) where {Ti <: Integer}
268268
convert(Ti, r > (rawone(x) >> 0x1) ? d + oneunit(rawtype(x)) : d)
269269
end
270270

271-
isfinite(x::Normed) = true
272-
isnan(x::Normed) = false
273-
isinf(x::Normed) = false
274-
275271
# Iteration
276272
# The main subtlety here is that iterating over N0f8(0):N0f8(1) will wrap around
277273
# unless we iterate using a wider type

test/fixed.jl

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ function test_fixed(::Type{T}, f) where {T}
1717
if !(typemin(T) < x <= typemax(T))
1818
continue
1919
end
20-
# isinteger(x) && @show x
2120
fx = convert(T,x)
2221
@test convert(T,convert(Float64, fx)) == fx
2322
@test convert(T,convert(Float64, -fx)) == -fx
@@ -243,6 +242,38 @@ end
243242
@test isa(float(one(Fixed{Int32,25})), Float64)
244243
end
245244

245+
@testset "predicates" begin
246+
@test isfinite(1Q7f8)
247+
@test !isnan(1Q7f8)
248+
@test !isinf(1Q7f8)
249+
250+
@testset "isinteger" begin
251+
for T in (Int8, Int16)
252+
@testset "isinteger(::Fixed{$T,$f})" for f = 0:bitwidth(T)-1
253+
F = Fixed{T,f}
254+
xs = typemin(F):eps(F):typemax(F)
255+
@test all(x -> isinteger(x) == isinteger(float(x)), xs)
256+
end
257+
end
258+
for T in (Int32, Int64)
259+
@testset "isinteger(::Fixed{$T,$f})" for f = 0:bitwidth(T)-1
260+
F = Fixed{T,f}
261+
fzero, fmax, fmin = zero(F), typemax(F), typemin(F)
262+
if f == 0
263+
@test isinteger(fzero) & isinteger(fmax) & isinteger(fmin)
264+
else
265+
@test isinteger(fzero) & !isinteger(fmax) & isinteger(fmin)
266+
end
267+
end
268+
end
269+
@testset "isinteger(::Fixed{Int8,8})" begin # TODO: remove this testset
270+
@test !isinteger(Fixed{Int8,8}(-0.5))
271+
@test isinteger(Fixed{Int8,8}(0.0))
272+
@test !isinteger(Fixed{Int8,8}(127/256))
273+
end
274+
end
275+
end
276+
246277
@testset "Show" begin
247278
x = Fixed{Int32,5}(0.25)
248279
iob = IOBuffer()

test/normed.jl

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,6 @@ end
8080
@testset "conversion" begin
8181
x = N0f8(0.5)
8282
@test convert(N0f8, x) === x
83-
@test isfinite(x) == true
84-
@test isnan(x) == false
85-
@test isinf(x) == false
8683

8784
@test convert(N0f8, 1.1/typemax(UInt8)) == eps(N0f8)
8885
@test convert(N6f10, 1.1/typemax(UInt16)*64) == eps(N6f10)
@@ -334,6 +331,32 @@ end
334331
@test_throws OverflowError length(NInt1(0):NInt1(1):typemax(NInt1))
335332
end
336333

334+
@testset "predicates" begin
335+
@test isfinite(1N8f8)
336+
@test !isnan(1N8f8)
337+
@test !isinf(1N8f8)
338+
339+
@testset "isinteger" begin
340+
for T in (UInt8, UInt16)
341+
@testset "isinteger(::Normed{$T,$f})" for f = 1:bitwidth(T)
342+
N = Normed{T,f}
343+
xs = typemin(N):eps(N):typemax(N)
344+
@test all(x -> isinteger(x) == isinteger(float(x)), xs)
345+
end
346+
end
347+
for T in (UInt32, UInt64)
348+
@testset "isinteger(::Normed{$T,$f})" for f = 1:bitwidth(T)
349+
N = Normed{T,f}
350+
if f == 1
351+
@test isinteger(zero(N)) & isinteger(oneunit(N))
352+
else
353+
@test !isinteger(oneunit(N) - eps(N)) & isinteger(oneunit(N))
354+
end
355+
end
356+
end
357+
end
358+
end
359+
337360
@testset "Promotion within Normed" begin
338361
@test @inferred(promote(N0f8(0.2), N0f8(0.8))) ===
339362
(N0f8(0.2), N0f8(0.8))

0 commit comments

Comments
 (0)