From 901d363b0cc99fad98a5f7a41f3f67f064714a0c Mon Sep 17 00:00:00 2001 From: Rafael Fourquet Date: Fri, 17 Jun 2016 19:29:39 +0530 Subject: [PATCH] rename ndigits0z to ndigits0zpb --- base/gmp.jl | 6 +++--- base/intfuncs.jl | 22 ++++++++++++++++------ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/base/gmp.jl b/base/gmp.jl index 26db033879c7d..1d45bb99b789f 100644 --- a/base/gmp.jl +++ b/base/gmp.jl @@ -8,8 +8,8 @@ import Base: *, +, -, /, <, <<, >>, >>>, <=, ==, >, >=, ^, (~), (&), (|), ($), binomial, cmp, convert, div, divrem, factorial, fld, gcd, gcdx, lcm, mod, ndigits, promote_rule, rem, show, isqrt, string, powermod, sum, trailing_zeros, trailing_ones, count_ones, base, tryparse_internal, - bin, oct, dec, hex, isequal, invmod, prevpow2, nextpow2, ndigits0z, - ndigits0znb, widen, signed, unsafe_trunc, trunc + bin, oct, dec, hex, isequal, invmod, prevpow2, nextpow2, ndigits0zpb, + widen, signed, unsafe_trunc, trunc if Clong == Int32 typealias ClongMax Union{Int8, Int16, Int32} @@ -525,7 +525,7 @@ function base(b::Integer, n::BigInt) unsafe_wrap(String, p, true) end -function ndigits0z(x::BigInt, b::Integer=10) +function ndigits0zpb(x::BigInt, b::Integer=10) b < 2 && throw(DomainError()) x.size == 0 && return 0 # for consistency with other ndigits0z methods if ispow2(b) && 2 <= b <= 62 # GMP assumes b is in this range diff --git a/base/intfuncs.jl b/base/intfuncs.jl index bc8a19c178911..350dda898f32c 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -233,7 +233,8 @@ end ndigits0znb(n::Unsigned, b::Integer) = ndigits0znb(signed(n), b) -function ndigits0z(n::Unsigned, b::Int) +# The suffix "pb" stands for "positive base" +function ndigits0zpb(n::Unsigned, b::Int) # precondition: b > 1 d = 0 b == 2 && return (sizeof(n)<<3-leading_zeros(n)) @@ -252,13 +253,22 @@ function ndigits0z(n::Unsigned, b::Int) return d end -ndigits0z(x::Integer, b::Integer) = ndigits0z(unsigned(abs(x)), Int(b)) +ndigits0zpb(x::Integer, b::Integer) = ndigits0zpb(unsigned(abs(x)), Int(b)) -function ndigits(x::Integer, b::Integer) +# The suffix "0z" means that the ouput is 0 on input zero (cf. #16841) +""" + ndigits0z(n::Integer, b::Integer=10) + +Return 0 if `n == 0`, otherwise compute the number of digits in number +`n` written in base `b`. +""" +ndigits0z(x::Integer, b::Integer) = ndigits(x, b, false) + +function ndigits(x::Integer, b::Integer, min1::Bool=true) if b < -1 - x == 0 ? 1 : ndigits0znb(x, b) + x == 0 ? min1%Int : ndigits0znb(x, b) elseif b > 1 - x == 0 ? 1 : ndigits0z(x, b) + x == 0 ? min1%Int : ndigits0zpb(x, b) else throw(DomainError()) end @@ -357,7 +367,7 @@ bits(x::Union{Int128,UInt128}) = bin(reinterpret(UInt128,x),128) digits{T<:Integer}(n::Integer, base::T=10, pad::Integer=1) = digits(T, n, base, pad) function digits{T<:Integer}(::Type{T}, n::Integer, base::Integer=10, pad::Integer=1) - digits!(zeros(T, max(pad, ndigits(n,base))), n, base) + digits!(zeros(T, max(pad, ndigits0z(n,base))), n, base) end function digits!{T<:Integer}(a::AbstractArray{T,1}, n::Integer, base::Integer=10)