From 130c9961790f93706fb793ff19f5836be9b2b856 Mon Sep 17 00:00:00 2001 From: Rafael Fourquet Date: Mon, 2 Jul 2018 19:51:35 +0200 Subject: [PATCH 1/2] ndigits: make base and pad keywords --- NEWS.md | 2 ++ base/deprecated.jl | 4 ++++ base/gmp.jl | 8 +++---- base/intfuncs.jl | 32 +++++++++++++-------------- base/printf.jl | 2 +- stdlib/Random/src/generation.jl | 2 +- test/bigint.jl | 6 +++--- test/intfuncs.jl | 38 ++++++++++++++++----------------- 8 files changed, 50 insertions(+), 44 deletions(-) diff --git a/NEWS.md b/NEWS.md index 61609b4539632..a4336b90d2277 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1300,6 +1300,8 @@ Deprecated or removed (along with other utilities mostly used at the interactive prompt, such as `edit` and `less`) ([#27635]). + * `ndigits(n, b, [pad])` is deprecated in favor of `ndigits(n, base=b, pad=pad)` ([#27908]). + Command-line option changes --------------------------- diff --git a/base/deprecated.jl b/base/deprecated.jl index b3a0b62e37167..aed58a8c1d783 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1443,6 +1443,10 @@ end @deprecate digits(n, base, pad) digits(n, base = base, pad = pad) @deprecate digits(T, n, base, pad) digits(T, n, base = base, pad = pad) +#27908 +@deprecate ndigits(n, base) ndigits(n, base=base) +@deprecate ndigits(n, base, pad) ndigits(n, base=base, pad=pad) + @deprecate print_with_color(color, args...; kwargs...) printstyled(args...; kwargs..., color=color) @deprecate base(b, n) string(n, base = b) diff --git a/base/gmp.jl b/base/gmp.jl index b5bd32631a8c1..2063fd9328e60 100644 --- a/base/gmp.jl +++ b/base/gmp.jl @@ -600,7 +600,7 @@ function string(n::BigInt; base::Integer = 10, pad::Integer = 1) base < 0 && return Base._base(Int(base), n, pad, (base>0) & (n.size<0)) 2 <= base <= 62 || throw(ArgumentError("base must be 2 ≤ base ≤ 62, got $base")) iszero(n) && pad < 1 && return "" - nd1 = ndigits(n, base) + nd1 = ndigits(n, base=base) nd = max(nd1, pad) sv = Base.StringVector(nd + isneg(n)) GC.@preserve sv MPZ.get_str!(pointer(sv) + nd - nd1, base, n) @@ -618,7 +618,7 @@ function ndigits0zpb(x::BigInt, b::Integer) MPZ.sizeinbase(x, b) else # non-base 2 mpz_sizeinbase might return an answer 1 too big - # use property that log(b, x) < ndigits(x, b) <= log(b, x) + 1 + # use property that log(b, x) < ndigits(x, base=b) <= log(b, x) + 1 n = MPZ.sizeinbase(x, 2) lb = log2(b) # assumed accurate to <1ulp (true for openlibm) q,r = divrem(n,lb) @@ -636,8 +636,8 @@ end # below, ONE is always left-shifted by at least one digit, so a new BigInt is # allocated, which can be safely mutated -prevpow2(x::BigInt) = -2 <= x <= 2 ? x : flipsign!(ONE << (ndigits(x, 2) - 1), x) -nextpow2(x::BigInt) = count_ones_abs(x) <= 1 ? x : flipsign!(ONE << ndigits(x, 2), x) +prevpow2(x::BigInt) = -2 <= x <= 2 ? x : flipsign!(ONE << (ndigits(x, base=2) - 1), x) +nextpow2(x::BigInt) = count_ones_abs(x) <= 1 ? x : flipsign!(ONE << ndigits(x, base=2), x) Base.checked_abs(x::BigInt) = abs(x) Base.checked_neg(x::BigInt) = -x diff --git a/base/intfuncs.jl b/base/intfuncs.jl index d3ba2356cfb1f..4852aba4702f0 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -454,10 +454,6 @@ ndigits0z(x::BitSigned) = ndigits0z(unsigned(abs(x))) ndigits0z(x::Integer) = ndigits0zpb(x, 10) -# TODO (when keywords args are fast): rename to ndigits and make pad a keyword -ndigits10(x::Integer, pad::Int=1) = max(pad, ndigits0z(x)) -ndigits(x::Integer) = iszero(x) ? 1 : ndigits0z(x) - ## ndigits with specified base ## # The suffix "nb" stands for "negative base" @@ -510,7 +506,7 @@ ndigits0zpb(x::Bool, b::Integer) = x % Int ndigits0z(n::Integer, b::Integer=10) Return 0 if `n == 0`, otherwise compute the number of digits in -integer `n` written in base `b` (i.e. equal to `ndigits(n, b)` +integer `n` written in base `b` (i.e. equal to `ndigits(n, base=b)` in this case). The base `b` must not be in `[-1, 0, 1]`. @@ -519,7 +515,7 @@ The base `b` must not be in `[-1, 0, 1]`. julia> Base.ndigits0z(0, 16) 0 -julia> Base.ndigits(0, 16) +julia> Base.ndigits(0, base=16) 1 julia> Base.ndigits0z(0) @@ -540,29 +536,33 @@ function ndigits0z(x::Integer, b::Integer) elseif b > 1 ndigits0zpb(x, b) else - throw(DomainError(b, "The base `b` must not be in `[-1, 0, 1]`.")) + throw(DomainError(b, "The base must not be in `[-1, 0, 1]`.")) end end """ - ndigits(n::Integer, b::Integer=10) + ndigits(n::Integer; base::Integer=10, pad::Integer=1) -Compute the number of digits in integer `n` written in base `b`. -The base `b` must not be in `[-1, 0, 1]`. +Compute the number of digits in integer `n` written in base `base` +(`base` must not be in `[-1, 0, 1]`), optionally padded with zeros +to a specified size (the result will never be less than `pad`). # Examples ```jldoctest julia> ndigits(12345) 5 -julia> ndigits(1022, 16) +julia> ndigits(1022, base=16) 3 -julia> string(1022, base = 16) +julia> string(1022, base=16) "3fe" + +julia> ndigits(123, pad=5) +5 ``` """ -ndigits(x::Integer, b::Integer, pad::Int=1) = max(pad, ndigits0z(x, b)) +ndigits(x::Integer; base::Integer=10, pad::Int=1) = max(pad, ndigits0z(x, base)) ## integer to string functions ## @@ -591,7 +591,7 @@ function oct(x::Unsigned, pad::Int, neg::Bool) end function dec(x::Unsigned, pad::Int, neg::Bool) - i = neg + ndigits10(x, pad) + i = neg + ndigits(x, base=10, pad=pad) a = StringVector(i) while i > neg a[i] = '0'+rem(x,10) @@ -622,7 +622,7 @@ function _base(b::Int, x::Integer, pad::Int, neg::Bool) (x >= 0) | (b < 0) || throw(DomainError(x, "For negative `x`, `b` must be negative.")) 2 <= abs(b) <= 62 || throw(ArgumentError("base must satisfy 2 ≤ abs(base) ≤ 62, got $b")) digits = abs(b) <= 36 ? base36digits : base62digits - i = neg + ndigits(x, b, pad) + i = neg + ndigits(x, base=b, pad=pad) a = StringVector(i) @inbounds while i > neg if b > 0 @@ -732,7 +732,7 @@ digits(n::Integer; base::Integer = 10, pad::Integer = 1) = digits(typeof(base), n, base = base, pad = pad) function digits(T::Type{<:Integer}, n::Integer; base::Integer = 10, pad::Integer = 1) - digits!(zeros(T, ndigits(n, base, pad)), n, base = base) + digits!(zeros(T, ndigits(n, base=base, pad=pad)), n, base=base) end """ diff --git a/base/printf.jl b/base/printf.jl index d53eddbf141ae..5010503949819 100644 --- a/base/printf.jl +++ b/base/printf.jl @@ -917,7 +917,7 @@ decode_HEX(x::Integer) = decode_hex(x,HEX_symbols) function decode(b::Int, x::BigInt) neg = x.size < 0 - pt = Base.ndigits(x, abs(b)) + pt = Base.ndigits(x, base=abs(b)) digits = DIGITSs[Threads.threadid()] length(digits) < pt+1 && resize!(digits, pt+1) neg && (x.size = -x.size) diff --git a/stdlib/Random/src/generation.jl b/stdlib/Random/src/generation.jl index 7e5c3012f5ef2..db52dc3be69df 100644 --- a/stdlib/Random/src/generation.jl +++ b/stdlib/Random/src/generation.jl @@ -309,7 +309,7 @@ end function Sampler(::AbstractRNG, r::AbstractUnitRange{BigInt}, ::Repetition) m = last(r) - first(r) m < 0 && throw(ArgumentError("range must be non-empty")) - nd = ndigits(m, 2) + nd = ndigits(m, base=2) nlimbs, highbits = divrem(nd, 8*sizeof(Limb)) highbits > 0 && (nlimbs += 1) mask = highbits == 0 ? ~zero(Limb) : one(Limb)< 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 all(n -> n == 1, ndigits(x, base=b) for b in [-20:-2;2:20] for x in [true, false]) end @testset "bin/oct/dec/hex/bits" begin @test string(UInt32('3'), base = 2) == "110011" From 1791d768cde002956621012088662f7913334794 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Thu, 5 Jul 2018 18:10:06 -0400 Subject: [PATCH 2/2] add missing deprecations for `digits` with no `pad` argument --- base/deprecated.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index aed58a8c1d783..3ca57a97587e4 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1440,8 +1440,10 @@ end @deprecate countlines(x, eol) countlines(x, eol = eol) @deprecate PipeBuffer(data, maxsize) PipeBuffer(data, maxsize = maxsize) @deprecate unsafe_wrap(T, pointer, dims, own) unsafe_wrap(T, pointer, dims, own = own) -@deprecate digits(n, base, pad) digits(n, base = base, pad = pad) -@deprecate digits(T, n, base, pad) digits(T, n, base = base, pad = pad) +@deprecate digits(n, base) digits(n, base = base) +@deprecate digits(n, base, pad) digits(n, base = base, pad = pad) +@deprecate digits(T::Type{<:Integer}, n, base) digits(T, n, base = base) +@deprecate digits(T::Type{<:Integer}, n, base, pad) digits(T, n, base = base, pad = pad) #27908 @deprecate ndigits(n, base) ndigits(n, base=base)