Skip to content

Commit e8be24d

Browse files
committed
ndigits: make base and pad keywords
1 parent 80db579 commit e8be24d

File tree

8 files changed

+49
-44
lines changed

8 files changed

+49
-44
lines changed

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,8 @@ Deprecated or removed
13001300
(along with other utilities mostly used at the interactive prompt, such as `edit`
13011301
and `less`) ([#27635]).
13021302

1303+
* `ndigits(n, b, [pad])` is deprecated in favor of `ndigits(n, base=b, pad=pad)` ([#27908]).
1304+
13031305
Command-line option changes
13041306
---------------------------
13051307

base/deprecated.jl

+3
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,9 @@ end
14421442
@deprecate digits(n, base, pad) digits(n, base = base, pad = pad)
14431443
@deprecate digits(T, n, base, pad) digits(T, n, base = base, pad = pad)
14441444

1445+
#27908
1446+
@deprecate ndigits(n, base=10, pad=1) ndigits(n, base=base, pad=pad)
1447+
14451448
@deprecate print_with_color(color, args...; kwargs...) printstyled(args...; kwargs..., color=color)
14461449

14471450
@deprecate base(b, n) string(n, base = b)

base/gmp.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ function string(n::BigInt; base::Integer = 10, pad::Integer = 1)
600600
base < 0 && return Base._base(Int(base), n, pad, (base>0) & (n.size<0))
601601
2 <= base <= 62 || throw(ArgumentError("base must be 2 ≤ base ≤ 62, got $base"))
602602
iszero(n) && pad < 1 && return ""
603-
nd1 = ndigits(n, base)
603+
nd1 = ndigits(n, base=base)
604604
nd = max(nd1, pad)
605605
sv = Base.StringVector(nd + isneg(n))
606606
GC.@preserve sv MPZ.get_str!(pointer(sv) + nd - nd1, base, n)
@@ -618,7 +618,7 @@ function ndigits0zpb(x::BigInt, b::Integer)
618618
MPZ.sizeinbase(x, b)
619619
else
620620
# non-base 2 mpz_sizeinbase might return an answer 1 too big
621-
# use property that log(b, x) < ndigits(x, b) <= log(b, x) + 1
621+
# use property that log(b, x) < ndigits(x, base=b) <= log(b, x) + 1
622622
n = MPZ.sizeinbase(x, 2)
623623
lb = log2(b) # assumed accurate to <1ulp (true for openlibm)
624624
q,r = divrem(n,lb)
@@ -636,8 +636,8 @@ end
636636

637637
# below, ONE is always left-shifted by at least one digit, so a new BigInt is
638638
# allocated, which can be safely mutated
639-
prevpow2(x::BigInt) = -2 <= x <= 2 ? x : flipsign!(ONE << (ndigits(x, 2) - 1), x)
640-
nextpow2(x::BigInt) = count_ones_abs(x) <= 1 ? x : flipsign!(ONE << ndigits(x, 2), x)
639+
prevpow2(x::BigInt) = -2 <= x <= 2 ? x : flipsign!(ONE << (ndigits(x, base=2) - 1), x)
640+
nextpow2(x::BigInt) = count_ones_abs(x) <= 1 ? x : flipsign!(ONE << ndigits(x, base=2), x)
641641

642642
Base.checked_abs(x::BigInt) = abs(x)
643643
Base.checked_neg(x::BigInt) = -x

base/intfuncs.jl

+16-16
Original file line numberDiff line numberDiff line change
@@ -454,10 +454,6 @@ ndigits0z(x::BitSigned) = ndigits0z(unsigned(abs(x)))
454454

455455
ndigits0z(x::Integer) = ndigits0zpb(x, 10)
456456

457-
# TODO (when keywords args are fast): rename to ndigits and make pad a keyword
458-
ndigits10(x::Integer, pad::Int=1) = max(pad, ndigits0z(x))
459-
ndigits(x::Integer) = iszero(x) ? 1 : ndigits0z(x)
460-
461457
## ndigits with specified base ##
462458

463459
# The suffix "nb" stands for "negative base"
@@ -510,7 +506,7 @@ ndigits0zpb(x::Bool, b::Integer) = x % Int
510506
ndigits0z(n::Integer, b::Integer=10)
511507
512508
Return 0 if `n == 0`, otherwise compute the number of digits in
513-
integer `n` written in base `b` (i.e. equal to `ndigits(n, b)`
509+
integer `n` written in base `b` (i.e. equal to `ndigits(n, base=b)`
514510
in this case).
515511
The base `b` must not be in `[-1, 0, 1]`.
516512
@@ -519,7 +515,7 @@ The base `b` must not be in `[-1, 0, 1]`.
519515
julia> Base.ndigits0z(0, 16)
520516
0
521517
522-
julia> Base.ndigits(0, 16)
518+
julia> Base.ndigits(0, base=16)
523519
1
524520
525521
julia> Base.ndigits0z(0)
@@ -540,29 +536,33 @@ function ndigits0z(x::Integer, b::Integer)
540536
elseif b > 1
541537
ndigits0zpb(x, b)
542538
else
543-
throw(DomainError(b, "The base `b` must not be in `[-1, 0, 1]`."))
539+
throw(DomainError(b, "The base must not be in `[-1, 0, 1]`."))
544540
end
545541
end
546542

547543
"""
548-
ndigits(n::Integer, b::Integer=10)
544+
ndigits(n::Integer; base::Integer=10, pad::Integer=1)
549545
550-
Compute the number of digits in integer `n` written in base `b`.
551-
The base `b` must not be in `[-1, 0, 1]`.
546+
Compute the number of digits in integer `n` written in base `base`
547+
(`base` must not be in `[-1, 0, 1]`), optionally padded with zeros
548+
to a specified size (the result will never be less than `pad`).
552549
553550
# Examples
554551
```jldoctest
555552
julia> ndigits(12345)
556553
5
557554
558-
julia> ndigits(1022, 16)
555+
julia> ndigits(1022, base=16)
559556
3
560557
561-
julia> string(1022, base = 16)
558+
julia> string(1022, base=16)
562559
"3fe"
560+
561+
julia> ndigits(123, pad=5)
562+
5
563563
```
564564
"""
565-
ndigits(x::Integer, b::Integer, pad::Int=1) = max(pad, ndigits0z(x, b))
565+
ndigits(x::Integer; base::Integer=10, pad::Int=1) = max(pad, ndigits0z(x, base))
566566

567567
## integer to string functions ##
568568

@@ -591,7 +591,7 @@ function oct(x::Unsigned, pad::Int, neg::Bool)
591591
end
592592

593593
function dec(x::Unsigned, pad::Int, neg::Bool)
594-
i = neg + ndigits10(x, pad)
594+
i = neg + ndigits(x, base=10, pad=pad)
595595
a = StringVector(i)
596596
while i > neg
597597
a[i] = '0'+rem(x,10)
@@ -622,7 +622,7 @@ function _base(b::Int, x::Integer, pad::Int, neg::Bool)
622622
(x >= 0) | (b < 0) || throw(DomainError(x, "For negative `x`, `b` must be negative."))
623623
2 <= abs(b) <= 62 || throw(ArgumentError("base must satisfy 2 ≤ abs(base) ≤ 62, got $b"))
624624
digits = abs(b) <= 36 ? base36digits : base62digits
625-
i = neg + ndigits(x, b, pad)
625+
i = neg + ndigits(x, base=b, pad=pad)
626626
a = StringVector(i)
627627
@inbounds while i > neg
628628
if b > 0
@@ -732,7 +732,7 @@ digits(n::Integer; base::Integer = 10, pad::Integer = 1) =
732732
digits(typeof(base), n, base = base, pad = pad)
733733

734734
function digits(T::Type{<:Integer}, n::Integer; base::Integer = 10, pad::Integer = 1)
735-
digits!(zeros(T, ndigits(n, base, pad)), n, base = base)
735+
digits!(zeros(T, ndigits(n, base=base, pad=pad)), n, base=base)
736736
end
737737

738738
"""

base/printf.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ decode_HEX(x::Integer) = decode_hex(x,HEX_symbols)
917917

918918
function decode(b::Int, x::BigInt)
919919
neg = x.size < 0
920-
pt = Base.ndigits(x, abs(b))
920+
pt = Base.ndigits(x, base=abs(b))
921921
digits = DIGITSs[Threads.threadid()]
922922
length(digits) < pt+1 && resize!(digits, pt+1)
923923
neg && (x.size = -x.size)

stdlib/Random/src/generation.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ end
309309
function Sampler(::AbstractRNG, r::AbstractUnitRange{BigInt}, ::Repetition)
310310
m = last(r) - first(r)
311311
m < 0 && throw(ArgumentError("range must be non-empty"))
312-
nd = ndigits(m, 2)
312+
nd = ndigits(m, base=2)
313313
nlimbs, highbits = divrem(nd, 8*sizeof(Limb))
314314
highbits > 0 && (nlimbs += 1)
315315
mask = highbits == 0 ? ~zero(Limb) : one(Limb)<<highbits - one(Limb)

test/bigint.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,12 @@ end
301301
@test !any(ndigits_mismatch, 8192:9999)
302302
end
303303
# The following should not crash (#16579)
304-
ndigits(big(rand(Int)), rand(63:typemax(Int)))
305-
ndigits(big(rand(Int)), big(2)^rand(2:999))
304+
ndigits(big(rand(Int)), base=rand(63:typemax(Int)))
305+
ndigits(big(rand(Int)), base=big(2)^rand(2:999))
306306

307307
for x in big.([-20:20; rand(Int)])
308308
for _base in -1:1
309-
@test_throws DomainError ndigits(x, _base)
309+
@test_throws DomainError ndigits(x, base=_base)
310310
end
311311
end
312312

test/intfuncs.jl

+19-19
Original file line numberDiff line numberDiff line change
@@ -104,42 +104,42 @@ end
104104
end
105105
@testset "ndigits/ndigits0z" begin
106106
@testset "issue #8266" begin
107-
@test ndigits(-15, 10) == 2
108-
@test ndigits(-15, -10) == 2
109-
@test ndigits(-1, 10) == 1
110-
@test ndigits(-1, -10) == 2
111-
@test ndigits(2, 10) == 1
112-
@test ndigits(2, -10) == 1
113-
@test ndigits(10, 10) == 2
114-
@test ndigits(10, -10) == 3
115-
@test ndigits(17, 10) == 2
116-
@test ndigits(17, -10) == 3
117-
@test ndigits(unsigned(17), -10) == 3
107+
@test ndigits(-15, base=10) == 2
108+
@test ndigits(-15, base=-10) == 2
109+
@test ndigits(-1, base=10) == 1
110+
@test ndigits(-1, base=-10) == 2
111+
@test ndigits(2, base=10) == 1
112+
@test ndigits(2, base=-10) == 1
113+
@test ndigits(10, base=10) == 2
114+
@test ndigits(10, base=-10) == 3
115+
@test ndigits(17, base=10) == 2
116+
@test ndigits(17, base=-10) == 3
117+
@test ndigits(unsigned(17), base=-10) == 3
118118

119-
@test ndigits(146, -3) == 5
119+
@test ndigits(146, base=-3) == 5
120120
end
121121
let (n, b) = rand(Int, 2)
122122
-1 <= b <= 1 && (b = 2) # invalid bases
123-
@test ndigits(n) == ndigits(big(n)) == ndigits(n, 10)
124-
@test ndigits(n, b) == ndigits(big(n), b)
123+
@test ndigits(n) == ndigits(big(n)) == ndigits(n, base=10)
124+
@test ndigits(n, base=b) == ndigits(big(n), base=b)
125125
end
126126

127127
for b in -1:1
128-
@test_throws DomainError ndigits(rand(Int), b)
128+
@test_throws DomainError ndigits(rand(Int), base=b)
129129
end
130130
@test ndigits(Int8(5)) == ndigits(5)
131131

132132
# issue #19367
133-
@test ndigits(Int128(2)^64, 256) == 9
133+
@test ndigits(Int128(2)^64, base=256) == 9
134134

135135
# test unsigned bases
136-
@test ndigits(9, 0x2) == 4
137-
@test ndigits(0x9, 0x2) == 4
136+
@test ndigits(9, base=0x2) == 4
137+
@test ndigits(0x9, base=0x2) == 4
138138

139139
# ndigits is defined for Bool
140140
@test iszero([Base.ndigits0z(false, b) for b in [-20:-2;2:20]])
141141
@test all(n -> n == 1, Base.ndigits0z(true, b) for b in [-20:-2;2:20])
142-
@test all(n -> n == 1, ndigits(x, b) for b in [-20:-2;2:20] for x in [true, false])
142+
@test all(n -> n == 1, ndigits(x, base=b) for b in [-20:-2;2:20] for x in [true, false])
143143
end
144144
@testset "bin/oct/dec/hex/bits" begin
145145
@test string(UInt32('3'), base = 2) == "110011"

0 commit comments

Comments
 (0)