Skip to content

make ndigits more generic #30384

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

Merged
merged 1 commit into from
Dec 19, 2018
Merged
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
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