Skip to content

Commit

Permalink
Merge pull request #40496 from bicycle1885/fix-printf-a
Browse files Browse the repository at this point in the history
fix %a/%A format for zeros
  • Loading branch information
quinnj authored Apr 15, 2021
2 parents b673576 + 17ede14 commit 691cf74
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
5 changes: 4 additions & 1 deletion stdlib/Printf/src/Printf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ const __BIG_FLOAT_MAX__ = 8192
x = round(x, sigdigits=prec)
newpos = Ryu.writeshortest(buf, pos, x, plus, space, hash, prec, T == Val{'g'} ? UInt8('e') : UInt8('E'), true, UInt8('.'))
elseif T == Val{'a'} || T == Val{'A'}
x, neg = x < 0 ? (-x, true) : (x, false)
x, neg = x < 0 || x === -Base.zero(x) ? (-x, true) : (x, false)
newpos = pos
if neg
buf[newpos] = UInt8('-')
Expand Down Expand Up @@ -456,6 +456,8 @@ const __BIG_FLOAT_MAX__ = 8192
buf[newpos] = UInt8('0')
newpos += 1
if prec > 0
buf[newpos] = UInt8('.')
newpos += 1
while prec > 0
buf[newpos] = UInt8('0')
newpos += 1
Expand All @@ -465,6 +467,7 @@ const __BIG_FLOAT_MAX__ = 8192
buf[newpos] = T <: Val{'a'} ? UInt8('p') : UInt8('P')
buf[newpos + 1] = UInt8('+')
buf[newpos + 2] = UInt8('0')
newpos += 3
else
if prec > -1
s, p = frexp(x)
Expand Down
3 changes: 3 additions & 0 deletions stdlib/Printf/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ end
@testset "%a" begin

# hex float
@test (Printf.@sprintf "%a" 0.0) == "0x0p+0"
@test (Printf.@sprintf "%a" -0.0) == "-0x0p+0"
@test (Printf.@sprintf "%.3a" 0.0) == "0x0.000p+0"
@test (Printf.@sprintf "%a" 1.5) == "0x1.8p+0"
@test (Printf.@sprintf "%a" 1.5f0) == "0x1.8p+0"
@test (Printf.@sprintf "%a" big"1.5") == "0x1.8p+0"
Expand Down

0 comments on commit 691cf74

Please sign in to comment.