Skip to content

Commit

Permalink
make ndigits more generic (#30384)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson authored Dec 19, 2018
1 parent eddcbd7 commit 2c71488
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -445,15 +445,18 @@ ndigits0znb(x::Unsigned, b::Integer) = ndigits0znb(-signed(fld(x, -b)), b) + (x
ndigits0znb(x::Bool, b::Integer) = x % Int

# The suffix "pb" stands for "positive base"
# TODO: allow b::Integer
function ndigits0zpb(x::Base.BitUnsigned, b::Int)
function ndigits0zpb(x::Integer, b::Integer)
# precondition: b > 1
x == 0 && return 0
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)
b = Int(b)
x = abs(x)
if x isa Base.BitInteger
x = unsigned(x)
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)
end

d = 0
while x > typemax(Int)
Expand All @@ -471,8 +474,6 @@ function ndigits0zpb(x::Base.BitUnsigned, b::Int)
return d
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

0 comments on commit 2c71488

Please sign in to comment.