Skip to content

Commit

Permalink
we might as well give a correct answer! (StefanKarpinski)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed Sep 13, 2018
1 parent d8e001f commit d2c92b8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
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
10 changes: 8 additions & 2 deletions test/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,14 @@ end
@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])

# with negative bases, we don't support yet unsigned integers which can't be converted to Signed
@test_throws InexactError ndigits(typemax(UInt64), base=-2)
# 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

0 comments on commit d2c92b8

Please sign in to comment.