-
Notifications
You must be signed in to change notification settings - Fork 33
fix isinteger #123
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
fix isinteger #123
Changes from all commits
5cf20d5
067dc1f
4bcc60c
279ea8a
0d72abf
8b19b8d
7ee8a04
e9b2da1
561eb94
d9a1201
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -146,6 +146,16 @@ end | |||
@test floatmin(Q11f4) == Q11f4(0.06) | ||||
end | ||||
|
||||
@testset "isinteger" begin | ||||
# issue #120 | ||||
for T in generate_fixedpoint_types(:Fixed) | ||||
T_ints = T.(clamp.(rand(rawtype(T), 500, 500), | ||||
ceil(rawtype(T), floattype(T)(typemin(T))), | ||||
floor(rawtype(T), floattype(T)(typemax(T))))) | ||||
@test all(isinteger.(T_ints)) | ||||
end | ||||
end | ||||
Comment on lines
+149
to
+157
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason for the failures on 32-bit systems is that the bit mask is only 32-bit length even for
julia> typeof(1)
Int32
julia> typeof(1<<63)
Int32
julia> bitstring(1<<63-1)
"11111111111111111111111111111111"
julia> bitstring(one(Int64)<<63-1)
"0111111111111111111111111111111111111111111111111111111111111111" Therefore, we should fix it up as follows: isinteger(x::Fixed{T,f}) where {T,f} = (x.i&(one(T)<<f-0x1)) == 0 BTW, since this is a testset for |
||||
|
||||
@testset "Disambiguation constructors" begin | ||||
@test_throws ArgumentError Fixed{Int32,16}('a') | ||||
@test_throws InexactError Fixed{Int32,16}(complex(1.0, 1.0)) | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -345,6 +345,16 @@ end | |
@test summary(view(a, 1:2)) == "2-element view(::Array{N0f8,1}, 1:2) with eltype Normed{UInt8,8}" | ||
end | ||
|
||
@testset "isinteger" begin | ||
# issue #120 | ||
for T in generate_fixedpoint_types(:Normed) | ||
T_ints = T.(clamp.(rand(rawtype(T), 500, 500), | ||
ceil(rawtype(T), floattype(T)(typemin(T))), | ||
floor(rawtype(T), floattype(T)(typemax(T))))) | ||
@test all(isinteger.(T_ints)) | ||
end | ||
end | ||
Comment on lines
+348
to
+356
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the comment for |
||
|
||
@testset "disambiguation constructors" begin | ||
@test_throws ArgumentError Normed{UInt32,16}('a') | ||
@test_throws InexactError Normed{UInt32,16}(complex(1.0, 1.0)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
""" return true if symbol `x` is a `ST` type""" | ||
function _is_type(x::Symbol, ST::Symbol) | ||
try | ||
@eval $(x) isa Type && $(x) <: $(ST) && return true | ||
catch | ||
return false | ||
end | ||
end | ||
|
||
""" | ||
generate_fixedpoint_types(ST::Symbol=:FixedPoint) | ||
|
||
generate a list of concrete `ST` types, where `ST ∈ (:FixedPoint, :Fixed, :Normed)` | ||
""" | ||
function generate_fixedpoint_types(ST::Symbol=:FixedPoint) | ||
fixed_types = setdiff(filter(x->_is_type(x, ST), names(FixedPointNumbers)), [:Fixed, :Normed, :FixedPoint]) | ||
fixed_types = [@eval $(x) for x in fixed_types] | ||
end | ||
Comment on lines
+1
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about using """
concrete_subtypes(T::Type)
generate a list of concrete subtypes of `T` which are defined in `FixedPointNumbers`.
"""
function concrete_subtypes(T::Type)
is_concrete_subtype(x) = x isa Type && x <: T && isconcretetype(x)
filter(is_concrete_subtype, eval.(names(FixedPointNumbers)))
end I believe that any exceptions do not occur in unbroken modules. If any exceptions should occur, they should not be caught. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See also comment for the
Fixed
test.isinteger(x::Normed{T,f}) where {T,f} = (x.i%(one(T)<<f-1)) == 0
Edit:
FYI,
(n%-1) == 0
is alwaystrue
.Moreover, if you write this here (I think we should do so), we should remove the following:
FixedPointNumbers.jl/src/FixedPointNumbers.jl
Lines 56 to 57 in da39318
And then, add
isinteger(x::Fixed{T,f}) where {T,f} = (x.i&(one(T)<<f-0x1)) == 0
into "src/fixed.jl"There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The above is analogous to
isinteger(x::Fixed)
. Of course, you can userawone(Normed{T,f})
.