Skip to content

Commit 3c086d6

Browse files
JeffBezansonKristofferC
authored andcommitted
make ndigits more generic (#30384)
(cherry picked from commit 2c71488)
1 parent 9a40122 commit 3c086d6

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

base/intfuncs.jl

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -445,15 +445,18 @@ ndigits0znb(x::Unsigned, b::Integer) = ndigits0znb(-signed(fld(x, -b)), b) + (x
445445
ndigits0znb(x::Bool, b::Integer) = x % Int
446446

447447
# The suffix "pb" stands for "positive base"
448-
# TODO: allow b::Integer
449-
function ndigits0zpb(x::Base.BitUnsigned, b::Int)
448+
function ndigits0zpb(x::Integer, b::Integer)
450449
# precondition: b > 1
451450
x == 0 && return 0
452-
b < 0 && return ndigits0znb(signed(x), b)
453-
b == 2 && return sizeof(x)<<3 - leading_zeros(x)
454-
b == 8 && return (sizeof(x)<<3 - leading_zeros(x) + 2) ÷ 3
455-
b == 16 && return sizeof(x)<<1 - leading_zeros(x)>>2
456-
b == 10 && return ndigits0z(x)
451+
b = Int(b)
452+
x = abs(x)
453+
if x isa Base.BitInteger
454+
x = unsigned(x)
455+
b == 2 && return sizeof(x)<<3 - leading_zeros(x)
456+
b == 8 && return (sizeof(x)<<3 - leading_zeros(x) + 2) ÷ 3
457+
b == 16 && return sizeof(x)<<1 - leading_zeros(x)>>2
458+
b == 10 && return ndigits0z(x)
459+
end
457460

458461
d = 0
459462
while x > typemax(Int)
@@ -471,8 +474,6 @@ function ndigits0zpb(x::Base.BitUnsigned, b::Int)
471474
return d
472475
end
473476

474-
ndigits0zpb(x::Base.BitSigned, b::Integer) = ndigits0zpb(unsigned(abs(x)), Int(b))
475-
ndigits0zpb(x::Base.BitUnsigned, b::Integer) = ndigits0zpb(x, Int(b))
476477
ndigits0zpb(x::Bool, b::Integer) = x % Int
477478

478479
# The suffix "0z" means that the output is 0 on input zero (cf. #16841)

0 commit comments

Comments
 (0)