Skip to content

Commit

Permalink
deprecate hex2num (=> reinterpret) & num2hex (=> bits)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed Jun 3, 2017
1 parent 4908f49 commit c5bc5b4
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 70 deletions.
18 changes: 18 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,24 @@ end
@deprecate srand(filename::AbstractString, n::Integer=4) srand(read!(filename, Array{UInt32}(Int(n))))
@deprecate MersenneTwister(filename::AbstractString) srand(MersenneTwister(0), read!(filename, Array{UInt32}(Int(4))))

@deprecate num2hex(n::Union{Bool, Base.BitReal}) bits(n, hex)

"""
hex2num(str)
Convert a hexadecimal string to the floating point number it represents.
This function, which is inherently type-unstable, returns a `Float16`,
a `Float32`, or a `Float64` depending on the length of the string `str`
(note that this length must hence be no greater than 16).
"""
function hex2num(s::AbstractString)
depwarn("hex2num(s) is deprecated, use reinterpret(T::Type, s)", :hex2num)
l = length(s)
l > 16 && throw(ArgumentError("the length of the passed string must be <= 16, got $l"))
reinterpret(l <= 4 ? Float16 : l <= 8 ? Float32 : Float64, s)
end
export hex2num

# END 0.7 deprecations

# BEGIN 1.0 deprecations
Expand Down
2 changes: 0 additions & 2 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,6 @@ export
gamma,
gcd,
gcdx,
hex2num,
hypot,
imag,
inv,
Expand Down Expand Up @@ -378,7 +377,6 @@ export
nextpow2,
nextprod,
numerator,
num2hex,
one,
oneunit,
powermod,
Expand Down
14 changes: 0 additions & 14 deletions base/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,6 @@ maxintfloat() = maxintfloat(Float64)

isinteger(x::AbstractFloat) = (x - trunc(x) == 0)

"""
hex2num(str)
Convert a hexadecimal string to the floating point number it represents.
This function, which is inherently type-unstable, returns a `Float16`,
a `Float32`, or a `Float64` depending on the length of the string `str`
(note that this length must hence be no greater than 16).
"""
function hex2num(s::AbstractString)
l = length(s)
l > 16 && throw(ArgumentError("the length of the passed string must be <= 16, got $l"))
hex2num(l <= 4 ? Float16 : l <= 8 ? Float32 : Float64, s)
end

"""
round([T,] x, [digits, [base]], [r::RoundingMode])
Expand Down
4 changes: 4 additions & 0 deletions base/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ reinterpret(::Type{Unsigned}, ::Type{Bool}) = UInt8
reinterpret(::Type{ Signed}, ::Type{Bool}) = Int8
reinterpret(::Type{Unsigned}, x::Bool) = x % UInt8
reinterpret(::Type{ Signed}, x::Bool) = x % Int8
reinterpret(::Type{Unsigned}, ::Type{Char}) = UInt32
reinterpret(::Type{ Signed}, ::Type{Char}) = Int32
reinterpret(::Type{Unsigned}, x::Char) = unsigned(x)
reinterpret(::Type{ Signed}, x::Char) = signed(x)

"""
unsigned(T::Type) -> UnsignedType
Expand Down
51 changes: 19 additions & 32 deletions base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -504,38 +504,21 @@ function hex(x::Unsigned, pad::Int, neg::Bool)
end

"""
num2hex(f)
reinterpret(T::Type, str::AbstractString, b=16)
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`](@ref) which does the opposite conversion.
Interprets a string as the bit representation of a
number of type `T`, encoded in base `b`.
```jldoctest
julia> num2hex(Int64(4))
"0000000000000004"
julia> num2hex(2.2)
"400199999999999a"
```
"""
num2hex(n::Union{Bool,BitReal}) = hex(reinterpret(Unsigned, n), sizeof(n)*2)

"""
hex2num(T::Type, str)
Interprets a hexadecimal string as the bit representation of a
number of type `T`. See also [`num2hex`](@ref).
```jldoctest
julia> hex2num(Float64, "400199999999999a")
julia> reinterpret(Float64, "400199999999999a")
2.2
julia> hex2num(Int32, "fffffffe")
julia> reinterpret(Int32, "fffffffe")
-2
```
"""
hex2num(::Type{T}, s::AbstractString) where {T<:Union{Bool,BitReal}} =
reinterpret(T, parse(reinterpret(Unsigned, T), s, 16))
reinterpret(::Type{T}, s::AbstractString, b=16) where {T<:Union{Bool,Char,BitReal}} =
reinterpret(T, parse(reinterpret(Unsigned, T), s, b))

const base36digits = ['0':'9';'a':'z']
const base62digits = ['0':'9';'A':'Z';'a':'z']
Expand Down Expand Up @@ -626,25 +609,29 @@ Convert an integer to a decimal string, optionally specifying a number of digits
dec

"""
bits(n)
bits(n, fmt=bin)
A string giving the literal bit representation of a number.
See also the [`num2hex`](@ref) function, which is similar but
gives a hexadecimal string.
The `fmt` function, which can be `bin` or `hex`,
determines the format used to represent those bits
respectively as a binary or hexadecimal string.
```jldoctest
julia> bits(4)
"0000000000000000000000000000000000000000000000000000000000000100"
julia> bits(4, hex)
"0000000000000004"
julia> bits(2.2)
"0100000000000001100110011001100110011001100110011001100110011010"
julia> bits(2.2, hex)
"400199999999999a"
```
"""
bits(x::Union{Bool,Int8,UInt8}) = bin(reinterpret(UInt8,x),8)
bits(x::Union{Int16,UInt16,Float16}) = bin(reinterpret(UInt16,x),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)
bits(n::Union{Bool,Char,BitReal}, fmt::Union{typeof(bin), typeof(hex)}=bin) =
fmt(reinterpret(Unsigned, n), sizeof(n)*(fmt === bin ? 8 : 2))

"""
digits([T<:Integer], n::Integer, base::T=10, pad::Integer=1)
Expand Down
6 changes: 2 additions & 4 deletions test/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ for elty in (Float16,Float32,Float64)
@test !isinteger(elty(NaN))
end

# num2hex, hex2num
# bits, reinterpret
for elty in (Float16,Float32,Float64), _ = 1:10
x = rand(elty)
@test hex2num(num2hex(x)) x
@test hex2num(elty, num2hex(x)) x
@test reinterpret(elty, bits(x, hex)) x
end
@test_throws ArgumentError hex2num(String(rand(['a':'f';'0':'1'], rand(17:100))))

# round
for elty in (Float32,Float64)
Expand Down
10 changes: 5 additions & 5 deletions test/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ end

@test hex(12) == "c"
@test hex(-12, 3) == "-00c"
@test num2hex(1243) == (Int == Int32 ? "000004db" : "00000000000004db")
@test num2hex(-1243) == (Int == Int32 ? "fffffb25" : "fffffffffffffb25")
@test num2hex(true) == "01"
@test num2hex(false) == "00"
@test bits(1243, hex) == (Int == Int32 ? "000004db" : "00000000000004db")
@test bits(-1243, hex) == (Int == Int32 ? "fffffb25" : "fffffffffffffb25")
@test bits(true, hex) == "01"
@test bits(false, hex) == "00"

for elty in Base.BitInteger_types, _ = 1:10
x = rand(elty)
@test hex2num(elty, num2hex(x)) == x
@test reinterpret(elty, bits(x, hex)) == x
end

@test base(2, 5, 7) == "0000101"
Expand Down
26 changes: 13 additions & 13 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -386,19 +386,19 @@ end
@test base(12,typemin(Int128)) == "-2a695925806818735399a37a20a31b3534a8"
@test base(12,typemax(Int128)) == "2a695925806818735399a37a20a31b3534a7"

@test hex2num("3ff0000000000000") == 1.
@test hex2num("bff0000000000000") == -1.
@test hex2num("4000000000000000") == 2.
@test hex2num("7ff0000000000000") == Inf
@test hex2num("fff0000000000000") == -Inf
@test isnan(hex2num("7ff8000000000000"))
@test isnan(hex2num("fff8000000000000"))
@test hex2num("3f800000") == 1.0f0
@test hex2num("bf800000") == -1.0f0
@test hex2num("7f800000") == Inf32
@test hex2num("ff800000") == -Inf32
@test isnan(hex2num("7fc00000"))
@test isnan(hex2num("ffc00000"))
@test reinterpret(Float64, "3ff0000000000000") == 1.
@test reinterpret(Float64, "bff0000000000000") == -1.
@test reinterpret(Float64, "4000000000000000") == 2.
@test reinterpret(Float64, "7ff0000000000000") == Inf
@test reinterpret(Float64, "fff0000000000000") == -Inf
@test isnan(reinterpret(Float64, "7ff8000000000000"))
@test isnan(reinterpret(Float64, "fff8000000000000"))
@test reinterpret(Float32, "3f800000") == 1.0f0
@test reinterpret(Float32, "bf800000") == -1.0f0
@test reinterpret(Float32, "7f800000") == Inf32
@test reinterpret(Float32, "ff800000") == -Inf32
@test isnan(reinterpret(Float32, "7fc00000"))
@test isnan(reinterpret(Float32, "ffc00000"))

# floating-point printing
@test repr(1.0) == "1.0"
Expand Down

0 comments on commit c5bc5b4

Please sign in to comment.