Skip to content

Commit ef3e42e

Browse files
committed
deprecate hex2num (=> reinterpret) & num2hex (=> bits)
1 parent 4908f49 commit ef3e42e

File tree

9 files changed

+61
-72
lines changed

9 files changed

+61
-72
lines changed

base/deprecated.jl

+18
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,24 @@ end
13501350
@deprecate srand(filename::AbstractString, n::Integer=4) srand(read!(filename, Array{UInt32}(Int(n))))
13511351
@deprecate MersenneTwister(filename::AbstractString) srand(MersenneTwister(0), read!(filename, Array{UInt32}(Int(4))))
13521352

1353+
@deprecate num2hex(n::Union{Bool, Base.BitReal}) bits(n, hex)
1354+
1355+
"""
1356+
hex2num(str)
1357+
1358+
Convert a hexadecimal string to the floating point number it represents.
1359+
This function, which is inherently type-unstable, returns a `Float16`,
1360+
a `Float32`, or a `Float64` depending on the length of the string `str`
1361+
(note that this length must hence be no greater than 16).
1362+
"""
1363+
function hex2num(s::AbstractString)
1364+
depwarn("hex2num(s) is deprecated, use reinterpret(T::Type, s)", :hex2num)
1365+
l = length(s)
1366+
l > 16 && throw(ArgumentError("the length of the passed string must be <= 16, got $l"))
1367+
reinterpret(l <= 4 ? Float16 : l <= 8 ? Float32 : Float64, s)
1368+
end
1369+
export hex2num
1370+
13531371
# END 0.7 deprecations
13541372

13551373
# BEGIN 1.0 deprecations

base/exports.jl

-2
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ export
340340
gamma,
341341
gcd,
342342
gcdx,
343-
hex2num,
344343
hypot,
345344
imag,
346345
inv,
@@ -378,7 +377,6 @@ export
378377
nextpow2,
379378
nextprod,
380379
numerator,
381-
num2hex,
382380
one,
383381
oneunit,
384382
powermod,

base/floatfuncs.jl

-14
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,6 @@ maxintfloat() = maxintfloat(Float64)
2929

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

32-
"""
33-
hex2num(str)
34-
35-
Convert a hexadecimal string to the floating point number it represents.
36-
This function, which is inherently type-unstable, returns a `Float16`,
37-
a `Float32`, or a `Float64` depending on the length of the string `str`
38-
(note that this length must hence be no greater than 16).
39-
"""
40-
function hex2num(s::AbstractString)
41-
l = length(s)
42-
l > 16 && throw(ArgumentError("the length of the passed string must be <= 16, got $l"))
43-
hex2num(l <= 4 ? Float16 : l <= 8 ? Float32 : Float64, s)
44-
end
45-
4632
"""
4733
round([T,] x, [digits, [base]], [r::RoundingMode])
4834

base/int.jl

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ reinterpret(::Type{Unsigned}, ::Type{Bool}) = UInt8
3636
reinterpret(::Type{ Signed}, ::Type{Bool}) = Int8
3737
reinterpret(::Type{Unsigned}, x::Bool) = x % UInt8
3838
reinterpret(::Type{ Signed}, x::Bool) = x % Int8
39+
reinterpret(::Type{Unsigned}, ::Type{Char}) = UInt32
40+
reinterpret(::Type{ Signed}, ::Type{Char}) = Int32
41+
reinterpret(::Type{Unsigned}, x::Char) = unsigned(x)
42+
reinterpret(::Type{ Signed}, x::Char) = signed(x)
3943

4044
"""
4145
unsigned(T::Type) -> UnsignedType

base/intfuncs.jl

+19-32
Original file line numberDiff line numberDiff line change
@@ -504,38 +504,21 @@ function hex(x::Unsigned, pad::Int, neg::Bool)
504504
end
505505

506506
"""
507-
num2hex(f)
507+
reinterpret(T::Type, str::AbstractString, b=16)
508508
509-
A hexadecimal string of the binary representation of a number.
510-
See also the [`bits`](@ref) function, which is similar but gives
511-
a binary string, and [`hex2num`](@ref) which does the opposite conversion.
509+
Interprets a string as the bit representation of a
510+
number of type `T`, encoded in base `b`.
512511
513512
```jldoctest
514-
julia> num2hex(Int64(4))
515-
"0000000000000004"
516-
517-
julia> num2hex(2.2)
518-
"400199999999999a"
519-
```
520-
"""
521-
num2hex(n::Union{Bool,BitReal}) = hex(reinterpret(Unsigned, n), sizeof(n)*2)
522-
523-
"""
524-
hex2num(T::Type, str)
525-
526-
Interprets a hexadecimal string as the bit representation of a
527-
number of type `T`. See also [`num2hex`](@ref).
528-
529-
```jldoctest
530-
julia> hex2num(Float64, "400199999999999a")
513+
julia> reinterpret(Float64, "400199999999999a")
531514
2.2
532515
533-
julia> hex2num(Int32, "fffffffe")
516+
julia> reinterpret(Int32, "fffffffe")
534517
-2
535518
```
536519
"""
537-
hex2num(::Type{T}, s::AbstractString) where {T<:Union{Bool,BitReal}} =
538-
reinterpret(T, parse(reinterpret(Unsigned, T), s, 16))
520+
reinterpret(::Type{T}, s::AbstractString, b=16) where {T<:Union{Bool,Char,BitReal}} =
521+
reinterpret(T, parse(reinterpret(Unsigned, T), s, b))
539522

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

628611
"""
629-
bits(n)
612+
bits(n, fmt=bin)
630613
631614
A string giving the literal bit representation of a number.
632-
See also the [`num2hex`](@ref) function, which is similar but
633-
gives a hexadecimal string.
615+
The `fmt` function, which can be `bin` or `hex`,
616+
determines the format used to represent those bits
617+
respectively as a binary or hexadecimal string.
634618
635619
```jldoctest
636620
julia> bits(4)
637621
"0000000000000000000000000000000000000000000000000000000000000100"
638622
623+
julia> bits(4, hex)
624+
"0000000000000004"
625+
639626
julia> bits(2.2)
640627
"0100000000000001100110011001100110011001100110011001100110011010"
628+
629+
julia> bits(2.2, hex)
630+
"400199999999999a"
641631
```
642632
"""
643-
bits(x::Union{Bool,Int8,UInt8}) = bin(reinterpret(UInt8,x),8)
644-
bits(x::Union{Int16,UInt16,Float16}) = bin(reinterpret(UInt16,x),16)
645-
bits(x::Union{Char,Int32,UInt32,Float32}) = bin(reinterpret(UInt32,x),32)
646-
bits(x::Union{Int64,UInt64,Float64}) = bin(reinterpret(UInt64,x),64)
647-
bits(x::Union{Int128,UInt128}) = bin(reinterpret(UInt128,x),128)
633+
bits(n::Union{Bool,Char,BitReal}, fmt::Union{typeof(bin), typeof(hex)}=bin) =
634+
fmt(reinterpret(Unsigned, n), sizeof(n)*(fmt === bin ? 8 : 2))
648635

649636
"""
650637
digits([T<:Integer], n::Integer, base::T=10, pad::Integer=1)

doc/src/stdlib/numbers.md

-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ Base.Math.significand
5858
Base.Math.exponent
5959
Base.complex(::Complex)
6060
Base.bswap
61-
Base.num2hex
62-
Base.hex2num
6361
Base.hex2bytes
6462
Base.bytes2hex
6563
```

test/floatfuncs.jl

+2-4
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,11 @@ for elty in (Float16,Float32,Float64)
3535
@test !isinteger(elty(NaN))
3636
end
3737

38-
# num2hex, hex2num
38+
# bits, reinterpret
3939
for elty in (Float16,Float32,Float64), _ = 1:10
4040
x = rand(elty)
41-
@test hex2num(num2hex(x)) x
42-
@test hex2num(elty, num2hex(x)) x
41+
@test reinterpret(elty, bits(x, hex)) x
4342
end
44-
@test_throws ArgumentError hex2num(String(rand(['a':'f';'0':'1'], rand(17:100))))
4543

4644
# round
4745
for elty in (Float32,Float64)

test/intfuncs.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,14 @@ end
123123

124124
@test hex(12) == "c"
125125
@test hex(-12, 3) == "-00c"
126-
@test num2hex(1243) == (Int == Int32 ? "000004db" : "00000000000004db")
127-
@test num2hex(-1243) == (Int == Int32 ? "fffffb25" : "fffffffffffffb25")
128-
@test num2hex(true) == "01"
129-
@test num2hex(false) == "00"
126+
@test bits(1243, hex) == (Int == Int32 ? "000004db" : "00000000000004db")
127+
@test bits(-1243, hex) == (Int == Int32 ? "fffffb25" : "fffffffffffffb25")
128+
@test bits(true, hex) == "01"
129+
@test bits(false, hex) == "00"
130130

131131
for elty in Base.BitInteger_types, _ = 1:10
132132
x = rand(elty)
133-
@test hex2num(elty, num2hex(x)) == x
133+
@test reinterpret(elty, bits(x, hex)) == x
134134
end
135135

136136
@test base(2, 5, 7) == "0000101"

test/numbers.jl

+13-13
Original file line numberDiff line numberDiff line change
@@ -386,19 +386,19 @@ end
386386
@test base(12,typemin(Int128)) == "-2a695925806818735399a37a20a31b3534a8"
387387
@test base(12,typemax(Int128)) == "2a695925806818735399a37a20a31b3534a7"
388388

389-
@test hex2num("3ff0000000000000") == 1.
390-
@test hex2num("bff0000000000000") == -1.
391-
@test hex2num("4000000000000000") == 2.
392-
@test hex2num("7ff0000000000000") == Inf
393-
@test hex2num("fff0000000000000") == -Inf
394-
@test isnan(hex2num("7ff8000000000000"))
395-
@test isnan(hex2num("fff8000000000000"))
396-
@test hex2num("3f800000") == 1.0f0
397-
@test hex2num("bf800000") == -1.0f0
398-
@test hex2num("7f800000") == Inf32
399-
@test hex2num("ff800000") == -Inf32
400-
@test isnan(hex2num("7fc00000"))
401-
@test isnan(hex2num("ffc00000"))
389+
@test reinterpret(Float64, "3ff0000000000000") == 1.
390+
@test reinterpret(Float64, "bff0000000000000") == -1.
391+
@test reinterpret(Float64, "4000000000000000") == 2.
392+
@test reinterpret(Float64, "7ff0000000000000") == Inf
393+
@test reinterpret(Float64, "fff0000000000000") == -Inf
394+
@test isnan(reinterpret(Float64, "7ff8000000000000"))
395+
@test isnan(reinterpret(Float64, "fff8000000000000"))
396+
@test reinterpret(Float32, "3f800000") == 1.0f0
397+
@test reinterpret(Float32, "bf800000") == -1.0f0
398+
@test reinterpret(Float32, "7f800000") == Inf32
399+
@test reinterpret(Float32, "ff800000") == -Inf32
400+
@test isnan(reinterpret(Float32, "7fc00000"))
401+
@test isnan(reinterpret(Float32, "ffc00000"))
402402

403403
# floating-point printing
404404
@test repr(1.0) == "1.0"

0 commit comments

Comments
 (0)