Skip to content

Commit

Permalink
Ryu: make sure adding zeros does not overwrite trailing dot (#51254)
Browse files Browse the repository at this point in the history
Fix #43129
  • Loading branch information
vtjnash authored Sep 12, 2023
1 parent 5d82d80 commit 832e46d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion base/ryu/exp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ function writeexp(buf, pos, v::T,
end
roundUp = 0
if lastDigit != 5
roundUp = lastDigit > 5
roundUp = lastDigit > 5 ? 1 : 0
else
rexp = precision - e
requiredTwos = -e2 - rexp
Expand Down
12 changes: 6 additions & 6 deletions base/ryu/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function writefixed(buf, pos, v::T,
mant = bits & MANTISSA_MASK
exp = Int((bits >> 52) & EXP_MASK)

if exp == 0
if exp == 0 # subnormal
e2 = 1 - 1023 - 52
m2 = mant
else
Expand All @@ -53,7 +53,7 @@ function writefixed(buf, pos, v::T,
i = len - 1
while i >= 0
j = p10bits - e2
#=@inbounds=# mula, mulb, mulc = POW10_SPLIT[POW10_OFFSET[idx + 1] + i + 1]
mula, mulb, mulc = POW10_SPLIT[POW10_OFFSET[idx + 1] + i + 1]
digits = mulshiftmod1e9(m2 << 8, mula, mulb, mulc, j + 8)
if nonzero
pos = append_nine_digits(digits, buf, pos)
Expand Down Expand Up @@ -103,7 +103,7 @@ function writefixed(buf, pos, v::T,
end
break
end
#=@inbounds=# mula, mulb, mulc = POW10_SPLIT_2[p + 1]
mula, mulb, mulc = POW10_SPLIT_2[p + 1]
digits = mulshiftmod1e9(m2 << 8, mula, mulb, mulc, j + 8)
if i < blocks - 1
pos = append_nine_digits(digits, buf, pos)
Expand All @@ -118,11 +118,11 @@ function writefixed(buf, pos, v::T,
k += 1
end
if lastDigit != 5
roundUp = lastDigit > 5
roundUp = lastDigit > 5 ? 1 : 0
else
requiredTwos = -e2 - precision - 1
trailingZeros = requiredTwos <= 0 || (requiredTwos < 60 && pow2(m2, requiredTwos))
roundUp = trailingZeros ? 2 : 1
roundUp = trailingZeros ? 2 : 1 # 2 means round only if odd
end
if maximum > 0
pos = append_c_digits(maximum, digits, buf, pos)
Expand All @@ -137,13 +137,13 @@ function writefixed(buf, pos, v::T,
while true
roundPos -= 1
if roundPos == (startpos - 1) || (buf[roundPos] == UInt8('-')) || (plus && buf[roundPos] == UInt8('+')) || (space && buf[roundPos] == UInt8(' '))
buf[pos] = UInt8('0')
buf[roundPos + 1] = UInt8('1')
if dotPos > 1
buf[dotPos] = UInt8('0')
buf[dotPos + 1] = decchar
hasfractional = true
end
buf[pos] = UInt8('0')
pos += 1
break
end
Expand Down
5 changes: 5 additions & 0 deletions test/ryu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,11 @@ end # Float16
@test Ryu.writefixed(1.25e+5, 1, false, false, false, UInt8('.'), true) == "125000"
@test Ryu.writefixed(1.25e+5, 2, false, false, false, UInt8('.'), true) == "125000"
end

@test Ryu.writefixed(100.0-eps(100.0), 0, false, false, true, UInt8('.'), false) == "100."
@test Ryu.writefixed(-100.0+eps(-100.0), 0, false, false, true, UInt8('.'), false) == "-100."
@test Ryu.writefixed(100.0-eps(100.0), 1, false, false, true, UInt8('.'), false) == "100.0"
@test Ryu.writefixed(-100.0+eps(-100.0), 1, false, false, true, UInt8('.'), false) == "-100.0"
end # fixed

@testset "Ryu.writeexp" begin
Expand Down

0 comments on commit 832e46d

Please sign in to comment.