Skip to content

Commit a6f2c49

Browse files
committed
Fix isinteger
1 parent 402e637 commit a6f2c49

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
@@ -60,7 +60,10 @@ function isapprox(x::FixedPoint, y::FixedPoint; rtol=0, atol=max(eps(x), eps(y))
6060
end
6161

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

6568
# identities
6669
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
@@ -215,6 +214,38 @@ end
215214
@test isa(float(one(Fixed{Int32,25})), Float64)
216215
end
217216

217+
@testset "predicates" begin
218+
@test isfinite(1Q7f8)
219+
@test !isnan(1Q7f8)
220+
@test !isinf(1Q7f8)
221+
222+
@testset "isinteger" begin
223+
for T in (Int8, Int16)
224+
@testset "isinteger(::Fixed{$T,$f})" for f = 0:bitwidth(T)-1
225+
F = Fixed{T,f}
226+
xs = typemin(F):eps(F):typemax(F)
227+
@test all(x -> isinteger(x) == isinteger(float(x)), xs)
228+
end
229+
end
230+
for T in (Int32, Int64)
231+
@testset "isinteger(::Fixed{$T,$f})" for f = 0:bitwidth(T)-1
232+
F = Fixed{T,f}
233+
fzero, fmax, fmin = zero(F), typemax(F), typemin(F)
234+
if f == 0
235+
@test isinteger(fzero) & isinteger(fmax) & isinteger(fmin)
236+
else
237+
@test isinteger(fzero) & !isinteger(fmax) & isinteger(fmin)
238+
end
239+
end
240+
end
241+
@testset "isinteger(::Fixed{Int8,8})" begin # TODO: remove this testset
242+
@test !isinteger(Fixed{Int8,8}(-0.5))
243+
@test isinteger(Fixed{Int8,8}(0.0))
244+
@test !isinteger(Fixed{Int8,8}(127/256))
245+
end
246+
end
247+
end
248+
218249
@testset "Show" begin
219250
x = Fixed{Int32,5}(0.25)
220251
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)
@@ -313,6 +310,32 @@ end
313310
@test counter == 256
314311
end
315312

313+
@testset "predicates" begin
314+
@test isfinite(1N8f8)
315+
@test !isnan(1N8f8)
316+
@test !isinf(1N8f8)
317+
318+
@testset "isinteger" begin
319+
for T in (UInt8, UInt16)
320+
@testset "isinteger(::Normed{$T,$f})" for f = 1:bitwidth(T)
321+
N = Normed{T,f}
322+
xs = typemin(N):eps(N):typemax(N)
323+
@test all(x -> isinteger(x) == isinteger(float(x)), xs)
324+
end
325+
end
326+
for T in (UInt32, UInt64)
327+
@testset "isinteger(::Normed{$T,$f})" for f = 1:bitwidth(T)
328+
N = Normed{T,f}
329+
if f == 1
330+
@test isinteger(zero(N)) & isinteger(oneunit(N))
331+
else
332+
@test !isinteger(oneunit(N) - eps(N)) & isinteger(oneunit(N))
333+
end
334+
end
335+
end
336+
end
337+
end
338+
316339
@testset "Promotion within Normed" begin
317340
@test @inferred(promote(N0f8(0.2), N0f8(0.8))) ===
318341
(N0f8(0.2), N0f8(0.8))

0 commit comments

Comments
 (0)