Skip to content
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

ndigits with negative base: error out instead of giving incorrect result #29148

Merged
merged 2 commits into from
Sep 13, 2018
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
3 changes: 2 additions & 1 deletion base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@ function ndigits0znb(x::Integer, b::Integer)
return d
end

ndigits0znb(x::Unsigned, b::Integer) = ndigits0znb(signed(x), b)
# do first division before conversion with signed here, which can otherwise overflow
ndigits0znb(x::Unsigned, b::Integer) = ndigits0znb(-signed(fld(x, -b)), b) + (x != 0)
ndigits0znb(x::Bool, b::Integer) = x % Int

# The suffix "pb" stands for "positive base"
Expand Down
9 changes: 9 additions & 0 deletions test/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ end
@test iszero([Base.ndigits0z(false, b) for b in [-20:-2;2:20]])
@test all(n -> n == 1, Base.ndigits0z(true, b) for b in [-20:-2;2:20])
@test all(n -> n == 1, ndigits(x, base=b) for b in [-20:-2;2:20] for x in [true, false])

# issue #29148
@test ndigits(typemax(UInt64), base=-2) == ndigits(big(typemax(UInt64)), base=-2)
for T in Base.BitInteger_types
n = rand(T)
b = -rand(2:100)
@test ndigits(n, base=b) == ndigits(big(n), base=b)
end

end
@testset "bin/oct/dec/hex/bits" begin
@test string(UInt32('3'), base = 2) == "110011"
Expand Down