Skip to content

Commit

Permalink
Merge pull request #21936 from JuliaLang/rf/ndigits-bool
Browse files Browse the repository at this point in the history
re-enable ndigits(::Bool, b) (fix #21919)
  • Loading branch information
JeffBezanson authored May 18, 2017
2 parents cc7bd6c + b46682f commit 5375620
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
32 changes: 17 additions & 15 deletions base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -360,38 +360,39 @@ ndigits(x::Integer) = iszero(x) ? 1 : ndigits0z(x)
## ndigits with specified base ##

# The suffix "nb" stands for "negative base"
function ndigits0znb(n::Integer, b::Integer)
# precondition: b < -1 && !(typeof(n) <: Unsigned)
function ndigits0znb(x::Integer, b::Integer)
# precondition: b < -1 && !(typeof(x) <: Unsigned)
d = 0
while n != 0
n = cld(n,b)
while x != 0
x = cld(x,b)
d += 1
end
return d
end

ndigits0znb(n::Unsigned, b::Integer) = ndigits0znb(signed(n), b)
ndigits0znb(x::Unsigned, b::Integer) = ndigits0znb(signed(x), b)
ndigits0znb(x::Bool, b::Integer) = x % Int

# The suffix "pb" stands for "positive base"
# TODO: allow b::Integer
function ndigits0zpb(n::Base.BitUnsigned, b::Int)
function ndigits0zpb(x::Base.BitUnsigned, b::Int)
# precondition: b > 1
b < 0 && return ndigits0znb(signed(n), b)
b == 2 && return sizeof(n)<<3 - leading_zeros(n)
b == 8 && return (sizeof(n)<<3 - leading_zeros(n) + 2) ÷ 3
b == 16 && return sizeof(n)<<1 - leading_zeros(n)>>2
b == 10 && return ndigits0z(n)
b < 0 && return ndigits0znb(signed(x), b)
b == 2 && return sizeof(x)<<3 - leading_zeros(x)
b == 8 && return (sizeof(x)<<3 - leading_zeros(x) + 2) ÷ 3
b == 16 && return sizeof(x)<<1 - leading_zeros(x)>>2
b == 10 && return ndigits0z(x)

d = 0
while n > typemax(Int)
n = div(n,b)
while x > typemax(Int)
x = div(x,b)
d += 1
end
n = div(n,b)
x = div(x,b)
d += 1

m = 1
while m <= n
while m <= x
m *= b
d += 1
end
Expand All @@ -400,6 +401,7 @@ end

ndigits0zpb(x::Base.BitSigned, b::Integer) = ndigits0zpb(unsigned(abs(x)), Int(b))
ndigits0zpb(x::Base.BitUnsigned, b::Integer) = ndigits0zpb(x, Int(b))
ndigits0zpb(x::Bool, b::Integer) = x % Int

# The suffix "0z" means that the output is 0 on input zero (cf. #16841)
"""
Expand Down
5 changes: 5 additions & 0 deletions test/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ end
@test ndigits(9, 0x2) == 4
@test ndigits(0x9, 0x2) == 4

# ndigits is defined for Bool
@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, b) for b in [-20:-2;2:20] for x in [true, false])

@test bin('3') == "110011"
@test bin('3',7) == "0110011"
@test bin(3) == "11"
Expand Down

0 comments on commit 5375620

Please sign in to comment.