Skip to content

Commit

Permalink
implement digits(T, n, base, pad); allow base == typemax(T) + 1
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanKarpinski committed Oct 15, 2015
1 parent 632689c commit b30d623
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions base/docs/helpdb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4256,9 +4256,9 @@ Return a list of immediate subtypes of DataType `T`. Note that all currently loa
subtypes

doc"""
digits(n, [base], [pad])
digits([T], n, [base], [pad])
Returns an array of the digits of `n` in the given base, optionally padded with zeros to a specified size. More significant digits are at higher indexes, such that `n == sum([digits[k]*base^(k-1) for k=1:length(digits)])`.
Returns an array with element type `T` (default `Int`) of the digits of `n` in the given base, optionally padded with zeros to a specified size. More significant digits are at higher indexes, such that `n == sum([digits[k]*base^(k-1) for k=1:length(digits)])`.
"""
digits

Expand Down
12 changes: 6 additions & 6 deletions base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -308,16 +308,16 @@ bits(x::Union{Char,Int32,UInt32,Float32}) = bin(reinterpret(UInt32,x),32)
bits(x::Union{Int64,UInt64,Float64}) = bin(reinterpret(UInt64,x),64)
bits(x::Union{Int128,UInt128}) = bin(reinterpret(UInt128,x),128)

function digits{T<:Integer}(n::Integer, base::T=10, pad::Integer=1)
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)
2 <= base || throw(ArgumentError("base must be ≥ 2, got $base"))
m = max(pad,ndigits0z(n,base))
a = zeros(T,m)
digits!(a, n, base)
return a
digits!(zeros(T, max(pad, ndigits0z(n,base))), n, base)
end

function digits!{T<:Integer}(a::AbstractArray{T,1}, n::Integer, base::T=10)
function digits!{T<:Integer}(a::AbstractArray{T,1}, n::Integer, base::Integer=10)
2 <= base || throw(ArgumentError("base must be ≥ 2, got $base"))
base - 1 <= typemax(T) || throw(ArgumentError("type $T too small for base $base"))
for i = 1:length(a)
a[i] = rem(n, base)
n = div(n, base)
Expand Down

0 comments on commit b30d623

Please sign in to comment.