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

Ryu: make sure adding zeros does not overwrite trailing dot #51254

Merged
merged 1 commit into from
Sep 12, 2023
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
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