diff --git a/base/essentials.jl b/base/essentials.jl index 8a419f44e7584..13afc776d9fba 100644 --- a/base/essentials.jl +++ b/base/essentials.jl @@ -146,17 +146,10 @@ Convert a number to an unsigned integer. If the argument is signed, it is reinte unsigned without checking for negative values. See also [`signed`](@ref). - unsigned(T::Type) -> UnsignedType - -Return the return-type of `unsigned(x::T)`, so that `unsigned(x)::unsigned(typeof(x))`. - ```jldoctest julia> unsigned(12) 0x000000000000000c -julia> unsigned(Int64) -UInt64 - julia> unsigned(2.0) 0x0000000000000002 @@ -174,23 +167,17 @@ Convert a number to a signed integer. If the argument is unsigned, it is reinter signed without checking for overflow. See also [`unsigned`](@ref). - signed(T::Type) -> SignedType - -Return the return-type of `signed(x::T)`, so that `signed(x)::signed(typeof(x))`. - ```jldoctest julia> signed(0xc) 12 -julia> signed(UInt64) -Int64 - julia> signed(2.0) 2 julia> signed(2.2) ERROR: InexactError() [...] +``` """ signed(x::UInt) = reinterpret(Int, x) diff --git a/base/int.jl b/base/int.jl index 2c1301e59a799..0c57e351e5f0a 100644 --- a/base/int.jl +++ b/base/int.jl @@ -28,11 +28,34 @@ const BitFloat = Union{BitFloat_types...} const BitReal_types = (BitInteger_types..., BitFloat_types...) const BitReal = Union{BitReal_types...} +## integer signed-ness conversions + reinterpret(::Type{Unsigned}, x::BitInteger) = unsigned(x) reinterpret(::Type{ Signed}, x::BitInteger) = signed(x) +""" + unsigned(T::Type) -> UnsignedType + +Return the return-type of `unsigned(x::T)`, so that `unsigned(x)::unsigned(typeof(x))`. + +```jldoctest +julia> unsigned(Int64) +UInt64 +``` +""" unsigned(::Type{T}) where {T<:Unsigned} = T -signed( ::Type{T}) where {T<:Signed} = T + +""" + signed(T::Type) -> SignedType + +Return the return-type of `signed(x::T)`, so that `signed(x)::signed(typeof(x))`. + +```jldoctest +julia> signed(UInt64) +Int64 +``` +""" +signed(::Type{T}) where {T<:Signed} = T unsigned(::Type{<:Union{Bool,BitFloat}}) = UInt signed( ::Type{<:Union{Bool,BitFloat}}) = Int diff --git a/base/intfuncs.jl b/base/intfuncs.jl index 43e64270c6092..aa5228b298882 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -2,23 +2,23 @@ # this construction is not available when put in int.jl and running in Core for (S,U,F) in zip(BitSigned_types, BitUnsigned_types, - (nothing, Float16, Float32, Float64, nothing)) - @eval begin - unsigned(::Type{$S}) = $U - unsigned(::Type{$U}) = $U - signed( ::Type{$S}) = $S - signed( ::Type{$U}) = $S - reinterpret(::Type{Unsigned}, ::Type{$S}) = $U - reinterpret(::Type{Unsigned}, ::Type{$U}) = $U - reinterpret(::Type{Signed}, ::Type{$S}) = $S - reinterpret(::Type{Signed}, ::Type{$U}) = $S - end - F === nothing && continue - @eval begin - reinterpret(::Type{Unsigned}, ::Type{$F}) = $U - reinterpret(::Type{Signed}, ::Type{$F}) = $S - end + (nothing, Float16, Float32, Float64, nothing)) + @eval begin + unsigned(::Type{$S}) = $U + unsigned(::Type{$U}) = $U + signed( ::Type{$S}) = $S + signed( ::Type{$U}) = $S + reinterpret(::Type{Unsigned}, ::Type{$S}) = $U + reinterpret(::Type{Unsigned}, ::Type{$U}) = $U + reinterpret(::Type{Signed}, ::Type{$S}) = $S + reinterpret(::Type{Signed}, ::Type{$U}) = $S end + F === nothing && continue + @eval begin + reinterpret(::Type{Unsigned}, ::Type{$F}) = $U + reinterpret(::Type{Signed}, ::Type{$F}) = $S + end +end ## number-theoretic functions ## @@ -506,9 +506,9 @@ end """ num2hex(f) -An hexadecimal string of the binary representation of a number. +A hexadecimal string of the binary representation of a number. See also the [`bits`](@ref) function, which is similar but gives -a binary string, and [`hex2num`] which does the opposite conversion. +a binary string, and [`hex2num`](@ref) which does the opposite conversion. ```jldoctest julia> num2hex(Int64(4)) @@ -516,7 +516,6 @@ julia> num2hex(Int64(4)) julia> num2hex(2.2) "400199999999999a" - ``` """ num2hex(n::BitReal) = hex(reinterpret(Unsigned, n), sizeof(n)*2) @@ -632,7 +631,7 @@ dec A string giving the literal bit representation of a number. See also the [`num2hex`](@ref) function, which is similar but -gives an hexadecimal string. +gives a hexadecimal string. ```jldoctest julia> bits(4)