Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Printf %g/%G format fix for Inf/NaN #41650

Merged
merged 2 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions stdlib/Printf/src/Printf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -424,27 +424,31 @@ const __BIG_FLOAT_MAX__ = 8192
elseif T == Val{'f'} || T == Val{'F'}
newpos = Ryu.writefixed(buf, pos, x, prec, plus, space, hash, UInt8('.'))
elseif T == Val{'g'} || T == Val{'G'}
# C11-compliant general format
prec = prec == 0 ? 1 : prec
# format the value in scientific notation and parse the exponent part
exp = let p = Ryu.writeexp(buf, pos, x, prec)
b1, b2, b3, b4 = buf[p-4], buf[p-3], buf[p-2], buf[p-1]
Z = UInt8('0')
if b1 == UInt8('e')
# two-digit exponent
sign = b2 == UInt8('+') ? 1 : -1
exp = 10 * (b3 - Z) + (b4 - Z)
if isinf(x) || isnan(x)
newpos = Ryu.writeshortest(buf, pos, x, plus, space)
else
# C11-compliant general format
prec = prec == 0 ? 1 : prec
# format the value in scientific notation and parse the exponent part
exp = let p = Ryu.writeexp(buf, pos, x, prec)
b1, b2, b3, b4 = buf[p-4], buf[p-3], buf[p-2], buf[p-1]
Z = UInt8('0')
if b1 == UInt8('e')
# two-digit exponent
sign = b2 == UInt8('+') ? 1 : -1
exp = 10 * (b3 - Z) + (b4 - Z)
else
# three-digit exponent
sign = b1 == UInt8('+') ? 1 : -1
exp = 100 * (b2 - Z) + 10 * (b3 - Z) + (b4 - Z)
end
flipsign(exp, sign)
end
if -4 ≤ exp < prec
newpos = Ryu.writefixed(buf, pos, x, prec - (exp + 1), plus, space, hash, UInt8('.'), !hash)
else
# three-digit exponent
sign = b1 == UInt8('+') ? 1 : -1
exp = 100 * (b2 - Z) + 10 * (b3 - Z) + (b4 - Z)
newpos = Ryu.writeexp(buf, pos, x, prec - 1, plus, space, hash, T == Val{'g'} ? UInt8('e') : UInt8('E'), UInt8('.'), !hash)
end
flipsign(exp, sign)
end
if -4 ≤ exp < prec
newpos = Ryu.writefixed(buf, pos, x, prec - (exp + 1), plus, space, hash, UInt8('.'), !hash)
else
newpos = Ryu.writeexp(buf, pos, x, prec - 1, plus, space, hash, T == Val{'g'} ? UInt8('e') : UInt8('E'), UInt8('.'), !hash)
end
elseif T == Val{'a'} || T == Val{'A'}
x, neg = x < 0 || x === -Base.zero(x) ? (-x, true) : (x, false)
Expand Down
9 changes: 9 additions & 0 deletions stdlib/Printf/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ end
@test Printf.@sprintf("%g", 123456.7) == "123457"
@test Printf.@sprintf("%g", 1234567.8) == "1.23457e+06"

# %g regression gh #41631
for (val, res) in ((Inf, "Inf"),
(-Inf, "-Inf"),
(NaN, "NaN"),
(-NaN, "NaN"))
@test Printf.@sprintf("%g", val) == res
@test Printf.@sprintf("%G", val) == res
end

# zeros
@test Printf.@sprintf("%.15g", 0) == "0"
@test Printf.@sprintf("%#.15g", 0) == "0.00000000000000"
Expand Down