Skip to content

Commit

Permalink
rename ndigits0z to ndigits0zpb
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed Jun 17, 2016
1 parent 298b383 commit ecd6480
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
6 changes: 3 additions & 3 deletions base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -518,7 +518,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
Expand Down
22 changes: 16 additions & 6 deletions base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,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))
Expand All @@ -219,13 +220,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
Expand Down Expand Up @@ -324,7 +334,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)
Expand Down

0 comments on commit ecd6480

Please sign in to comment.