Skip to content

Commit

Permalink
Merge pull request #15083 from JuliaLang/sb/hexfloat-int
Browse files Browse the repository at this point in the history
Adds %a methods for printing integers, fixes #14409.
  • Loading branch information
simonbyrne committed Feb 16, 2016
2 parents dad974e + 2d37f68 commit b432395
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
8 changes: 8 additions & 0 deletions base/printf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,14 @@ function ini_hex(x::SmallFloatingPoint, symbols::Array{UInt8,1})
end
end

function ini_hex(x::Integer)
len,pt,neg = decode_hex(x)
pt = (len-1)<<2
len,pt,neg
end

# not implemented
ini_hex(x::Integer,ndigits::Int) = throw(MethodError(ini_hex,(x,ndigits)))

#BigFloat
fix_dec(out, d::BigFloat, flags::ASCIIString, width::Int, precision::Int, c::Char) = bigfloat_printf(out, d, flags, width, precision, c)
Expand Down
9 changes: 8 additions & 1 deletion test/printf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# printf
# int
@test (@sprintf "%d" typemax(Int64)) == "9223372036854775807"
@test (@sprintf "%a" typemax(Int64)) == "0x7.fffffffffffffffp+60"

for (fmt, val) in (("%i", "42"),
("%u", "42"),
("Test: %i", "Test: 42"),
Expand All @@ -13,7 +15,10 @@ for (fmt, val) in (("%i", "42"),
("% i", " 42"),
("%+i", "+42"),
("%4i", " 42"),
("%-4i", "42 "))
("%-4i", "42 "),
("%a","0x2.ap+4"),
("%20a"," 0x2.ap+4"),
("%-20a","0x2.ap+4 "))
@test( @eval(@sprintf($fmt, 42) == $val))
end

Expand Down Expand Up @@ -80,6 +85,8 @@ end

# hex float
@test (@sprintf "%a" 1.5) == "0x1.8p+0"
@test (@sprintf "%a" 1.5f0) == "0x1.8p+0"
@test (@sprintf "%a" big"1.5") == "0x1.8p+0"
@test (@sprintf "%#.0a" 1.5) == "0x2.p+0"
@test (@sprintf "%+30a" 1/3) == " +0x1.5555555555555p-2"

Expand Down

0 comments on commit b432395

Please sign in to comment.