Skip to content

Commit

Permalink
Merge pull request #40686 from bicycle1885/nan-sign
Browse files Browse the repository at this point in the history
show sign for NaNs if required
  • Loading branch information
quinnj authored May 3, 2021
2 parents ef45879 + 2ceebfd commit e3adc39
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 101 deletions.
33 changes: 2 additions & 31 deletions base/ryu/exp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,10 @@ function writeexp(buf, pos, v::T,
@assert 0 < pos <= length(buf)
startpos = pos
x = Float64(v)
neg = signbit(x)
pos = append_sign(x, plus, space, buf, pos)

# special cases
if x == 0
if neg
buf[pos] = UInt8('-')
pos += 1
elseif plus
buf[pos] = UInt8('+')
pos += 1
elseif space
buf[pos] = UInt8(' ')
pos += 1
end
buf[pos] = UInt8('0')
pos += 1
if precision > 0
Expand All @@ -41,16 +32,6 @@ function writeexp(buf, pos, v::T,
buf[pos + 2] = UInt8('N')
return pos + 3
elseif !isfinite(x)
if neg
buf[pos] = UInt8('-')
pos += 1
elseif plus
buf[pos] = UInt8('+')
pos += 1
elseif space
buf[pos] = UInt8(' ')
pos += 1
end
buf[pos] = UInt8('I')
buf[pos + 1] = UInt8('n')
buf[pos + 2] = UInt8('f')
Expand All @@ -70,16 +51,6 @@ function writeexp(buf, pos, v::T,
end
nonzero = false
precision += 1
if neg
buf[pos] = UInt8('-')
pos += 1
elseif plus
buf[pos] = UInt8('+')
pos += 1
elseif space
buf[pos] = UInt8(' ')
pos += 1
end
digits = 0
printedDigits = 0
availableDigits = 0
Expand Down
33 changes: 2 additions & 31 deletions base/ryu/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,10 @@ function writefixed(buf, pos, v::T,
@assert 0 < pos <= length(buf)
startpos = pos
x = Float64(v)
neg = signbit(x)
pos = append_sign(x, plus, space, buf, pos)

# special cases
if x == 0
if neg
buf[pos] = UInt8('-')
pos += 1
elseif plus
buf[pos] = UInt8('+')
pos += 1
elseif space
buf[pos] = UInt8(' ')
pos += 1
end
buf[pos] = UInt8('0')
pos += 1
if precision > 0
Expand All @@ -40,16 +31,6 @@ function writefixed(buf, pos, v::T,
buf[pos + 2] = UInt8('N')
return pos + 3
elseif !isfinite(x)
if neg
buf[pos] = UInt8('-')
pos += 1
elseif plus
buf[pos] = UInt8('+')
pos += 1
elseif space
buf[pos] = UInt8(' ')
pos += 1
end
buf[pos] = UInt8('I')
buf[pos + 1] = UInt8('n')
buf[pos + 2] = UInt8('f')
Expand All @@ -68,16 +49,6 @@ function writefixed(buf, pos, v::T,
m2 = (Int64(1) << 52) | mant
end
nonzero = false
if neg
buf[pos] = UInt8('-')
pos += 1
elseif plus
buf[pos] = UInt8('+')
pos += 1
elseif space
buf[pos] = UInt8(' ')
pos += 1
end
if e2 >= -52
idx = e2 < 0 ? 0 : indexforexp(e2)
p10bits = pow10bitsforindex(idx)
Expand Down
45 changes: 12 additions & 33 deletions base/ryu/shortest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ function writeshortest(buf::Vector{UInt8}, pos, x::T,
precision=-1, expchar=UInt8('e'), padexp=false, decchar=UInt8('.'),
typed=false, compact=false) where {T}
@assert 0 < pos <= length(buf)
neg = signbit(x)
# special cases
if x == 0
if typed && x isa Float16
Expand All @@ -243,17 +242,7 @@ function writeshortest(buf::Vector{UInt8}, pos, x::T,
buf[pos + 7] = UInt8('(')
pos += 8
end

if neg
buf[pos] = UInt8('-')
pos += 1
elseif plus
buf[pos] = UInt8('+')
pos += 1
elseif space
buf[pos] = UInt8(' ')
pos += 1
end
pos = append_sign(x, plus, space, buf, pos)
buf[pos] = UInt8('0')
pos += 1
if hash
Expand Down Expand Up @@ -290,6 +279,7 @@ function writeshortest(buf::Vector{UInt8}, pos, x::T,
end
return pos
elseif isnan(x)
pos = append_sign(x, plus, space, buf, pos)
buf[pos] = UInt8('N')
buf[pos + 1] = UInt8('a')
buf[pos + 2] = UInt8('N')
Expand All @@ -304,22 +294,20 @@ function writeshortest(buf::Vector{UInt8}, pos, x::T,
end
return pos + 3 + (typed && x isa Union{Float32, Float16} ? 2 : 0)
elseif !isfinite(x)
if neg
buf[pos] = UInt8('-')
end
buf[pos + neg] = UInt8('I')
buf[pos + neg + 1] = UInt8('n')
buf[pos + neg + 2] = UInt8('f')
pos = append_sign(x, plus, space, buf, pos)
buf[pos] = UInt8('I')
buf[pos + 1] = UInt8('n')
buf[pos + 2] = UInt8('f')
if typed
if x isa Float32
buf[pos + neg + 3] = UInt8('3')
buf[pos + neg + 4] = UInt8('2')
buf[pos + 3] = UInt8('3')
buf[pos + 4] = UInt8('2')
elseif x isa Float16
buf[pos + neg + 3] = UInt8('1')
buf[pos + neg + 4] = UInt8('6')
buf[pos + 3] = UInt8('1')
buf[pos + 4] = UInt8('6')
end
end
return pos + neg + 3 + (typed && x isa Union{Float32, Float16} ? 2 : 0)
return pos + 3 + (typed && x isa Union{Float32, Float16} ? 2 : 0)
end

output, nexp = reduce_shortest(x, compact ? 999_999 : nothing)
Expand All @@ -335,16 +323,7 @@ function writeshortest(buf::Vector{UInt8}, pos, x::T,
buf[pos + 7] = UInt8('(')
pos += 8
end
if neg
buf[pos] = UInt8('-')
pos += 1
elseif plus
buf[pos] = UInt8('+')
pos += 1
elseif space
buf[pos] = UInt8(' ')
pos += 1
end
pos = append_sign(x, plus, space, buf, pos)

olength = decimallength(output)
exp_form = true
Expand Down
14 changes: 14 additions & 0 deletions base/ryu/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,20 @@ Compute `(m * mul) >> j % 10^9` where `mul = mula + mulb<<64 + mulc<<128`, and `
return (v % UInt32) - UInt32(1000000000) * shifted
end

@inline function append_sign(x, plus, space, buf, pos)
if signbit(x) && !isnan(x) # suppress minus sign for signaling NaNs
buf[pos] = UInt8('-')
pos += 1
elseif plus
buf[pos] = UInt8('+')
pos += 1
elseif space
buf[pos] = UInt8(' ')
pos += 1
end
return pos
end

@inline function append_n_digits(olength, digits, buf, pos)
i = 0
while digits >= 10000
Expand Down
12 changes: 6 additions & 6 deletions stdlib/Printf/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ end
@test (Printf.@sprintf "%f" -Inf) == "-Inf"
@test (Printf.@sprintf "%+f" -Inf) == "-Inf"
@test (Printf.@sprintf "%f" NaN) == "NaN"
@test (Printf.@sprintf "%+f" NaN) == "NaN"
@test (Printf.@sprintf "% f" NaN) == "NaN"
@test (Printf.@sprintf "% #f" NaN) == "NaN"
@test (Printf.@sprintf "%+f" NaN) == "+NaN"
@test (Printf.@sprintf "% f" NaN) == " NaN"
@test (Printf.@sprintf "% #f" NaN) == " NaN"
@test (Printf.@sprintf "%e" big"Inf") == "Inf"
@test (Printf.@sprintf "%e" big"NaN") == "NaN"

Expand Down Expand Up @@ -163,9 +163,9 @@ end
@test (Printf.@sprintf "%e" -Inf) == "-Inf"
@test (Printf.@sprintf "%+e" -Inf) == "-Inf"
@test (Printf.@sprintf "%e" NaN) == "NaN"
@test (Printf.@sprintf "%+e" NaN) == "NaN"
@test (Printf.@sprintf "% e" NaN) == "NaN"
@test (Printf.@sprintf "% #e" NaN) == "NaN"
@test (Printf.@sprintf "%+e" NaN) == "+NaN"
@test (Printf.@sprintf "% e" NaN) == " NaN"
@test (Printf.@sprintf "% #e" NaN) == " NaN"
@test (Printf.@sprintf "%e" big"Inf") == "Inf"
@test (Printf.@sprintf "%e" big"NaN") == "NaN"

Expand Down

2 comments on commit e3adc39

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily package evaluation, I will reply here when finished:

@nanosoldier runtests(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your package evaluation job has completed - possible new issues were detected. A full report can be found here. cc @maleadt

Please sign in to comment.