Skip to content

Fix isinteger #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/FixedPointNumbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ function isapprox(x::FixedPoint, y::FixedPoint; rtol=0, atol=max(eps(x), eps(y))
end

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

# identities
zero(::Type{X}) where {X <: FixedPoint} = X(zero(rawtype(X)), 0)
Expand Down
4 changes: 0 additions & 4 deletions src/normed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,6 @@ function round(::Type{Ti}, x::Normed) where {Ti <: Integer}
convert(Ti, r > (rawone(x) >> 0x1) ? d + oneunit(rawtype(x)) : d)
end

isfinite(x::Normed) = true
isnan(x::Normed) = false
isinf(x::Normed) = false

# Iteration
# The main subtlety here is that iterating over N0f8(0):N0f8(1) will wrap around
# unless we iterate using a wider type
Expand Down
33 changes: 32 additions & 1 deletion test/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ function test_fixed(::Type{T}, f) where {T}
if !(typemin(T) < x <= typemax(T))
continue
end
# isinteger(x) && @show x
fx = convert(T,x)
@test convert(T,convert(Float64, fx)) == fx
@test convert(T,convert(Float64, -fx)) == -fx
Expand Down Expand Up @@ -243,6 +242,38 @@ end
@test isa(float(one(Fixed{Int32,25})), Float64)
end

@testset "predicates" begin
@test isfinite(1Q7f8)
@test !isnan(1Q7f8)
@test !isinf(1Q7f8)

@testset "isinteger" begin
for T in (Int8, Int16)
@testset "isinteger(::Fixed{$T,$f})" for f = 0:bitwidth(T)-1
F = Fixed{T,f}
xs = typemin(F):eps(F):typemax(F)
@test all(x -> isinteger(x) == isinteger(float(x)), xs)
end
end
for T in (Int32, Int64)
@testset "isinteger(::Fixed{$T,$f})" for f = 0:bitwidth(T)-1
F = Fixed{T,f}
fzero, fmax, fmin = zero(F), typemax(F), typemin(F)
if f == 0
@test isinteger(fzero) & isinteger(fmax) & isinteger(fmin)
else
@test isinteger(fzero) & !isinteger(fmax) & isinteger(fmin)
end
end
end
@testset "isinteger(::Fixed{Int8,8})" begin # TODO: remove this testset
@test !isinteger(Fixed{Int8,8}(-0.5))
@test isinteger(Fixed{Int8,8}(0.0))
@test !isinteger(Fixed{Int8,8}(127/256))
end
end
end

@testset "Show" begin
x = Fixed{Int32,5}(0.25)
iob = IOBuffer()
Expand Down
29 changes: 26 additions & 3 deletions test/normed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ end
@testset "conversion" begin
x = N0f8(0.5)
@test convert(N0f8, x) === x
@test isfinite(x) == true
@test isnan(x) == false
@test isinf(x) == false

@test convert(N0f8, 1.1/typemax(UInt8)) == eps(N0f8)
@test convert(N6f10, 1.1/typemax(UInt16)*64) == eps(N6f10)
Expand Down Expand Up @@ -334,6 +331,32 @@ end
@test_throws OverflowError length(NInt1(0):NInt1(1):typemax(NInt1))
end

@testset "predicates" begin
@test isfinite(1N8f8)
@test !isnan(1N8f8)
@test !isinf(1N8f8)

@testset "isinteger" begin
for T in (UInt8, UInt16)
@testset "isinteger(::Normed{$T,$f})" for f = 1:bitwidth(T)
N = Normed{T,f}
xs = typemin(N):eps(N):typemax(N)
@test all(x -> isinteger(x) == isinteger(float(x)), xs)
end
end
for T in (UInt32, UInt64)
@testset "isinteger(::Normed{$T,$f})" for f = 1:bitwidth(T)
N = Normed{T,f}
if f == 1
@test isinteger(zero(N)) & isinteger(oneunit(N))
else
@test !isinteger(oneunit(N) - eps(N)) & isinteger(oneunit(N))
end
end
end
end
end

@testset "Promotion within Normed" begin
@test @inferred(promote(N0f8(0.2), N0f8(0.8))) ===
(N0f8(0.2), N0f8(0.8))
Expand Down