Skip to content

Commit 832e46d

Browse files
authored
Ryu: make sure adding zeros does not overwrite trailing dot (#51254)
Fix #43129
1 parent 5d82d80 commit 832e46d

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

base/ryu/exp.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ function writeexp(buf, pos, v::T,
147147
end
148148
roundUp = 0
149149
if lastDigit != 5
150-
roundUp = lastDigit > 5
150+
roundUp = lastDigit > 5 ? 1 : 0
151151
else
152152
rexp = precision - e
153153
requiredTwos = -e2 - rexp

base/ryu/fixed.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function writefixed(buf, pos, v::T,
3838
mant = bits & MANTISSA_MASK
3939
exp = Int((bits >> 52) & EXP_MASK)
4040

41-
if exp == 0
41+
if exp == 0 # subnormal
4242
e2 = 1 - 1023 - 52
4343
m2 = mant
4444
else
@@ -53,7 +53,7 @@ function writefixed(buf, pos, v::T,
5353
i = len - 1
5454
while i >= 0
5555
j = p10bits - e2
56-
#=@inbounds=# mula, mulb, mulc = POW10_SPLIT[POW10_OFFSET[idx + 1] + i + 1]
56+
mula, mulb, mulc = POW10_SPLIT[POW10_OFFSET[idx + 1] + i + 1]
5757
digits = mulshiftmod1e9(m2 << 8, mula, mulb, mulc, j + 8)
5858
if nonzero
5959
pos = append_nine_digits(digits, buf, pos)
@@ -103,7 +103,7 @@ function writefixed(buf, pos, v::T,
103103
end
104104
break
105105
end
106-
#=@inbounds=# mula, mulb, mulc = POW10_SPLIT_2[p + 1]
106+
mula, mulb, mulc = POW10_SPLIT_2[p + 1]
107107
digits = mulshiftmod1e9(m2 << 8, mula, mulb, mulc, j + 8)
108108
if i < blocks - 1
109109
pos = append_nine_digits(digits, buf, pos)
@@ -118,11 +118,11 @@ function writefixed(buf, pos, v::T,
118118
k += 1
119119
end
120120
if lastDigit != 5
121-
roundUp = lastDigit > 5
121+
roundUp = lastDigit > 5 ? 1 : 0
122122
else
123123
requiredTwos = -e2 - precision - 1
124124
trailingZeros = requiredTwos <= 0 || (requiredTwos < 60 && pow2(m2, requiredTwos))
125-
roundUp = trailingZeros ? 2 : 1
125+
roundUp = trailingZeros ? 2 : 1 # 2 means round only if odd
126126
end
127127
if maximum > 0
128128
pos = append_c_digits(maximum, digits, buf, pos)
@@ -137,13 +137,13 @@ function writefixed(buf, pos, v::T,
137137
while true
138138
roundPos -= 1
139139
if roundPos == (startpos - 1) || (buf[roundPos] == UInt8('-')) || (plus && buf[roundPos] == UInt8('+')) || (space && buf[roundPos] == UInt8(' '))
140+
buf[pos] = UInt8('0')
140141
buf[roundPos + 1] = UInt8('1')
141142
if dotPos > 1
142143
buf[dotPos] = UInt8('0')
143144
buf[dotPos + 1] = decchar
144145
hasfractional = true
145146
end
146-
buf[pos] = UInt8('0')
147147
pos += 1
148148
break
149149
end

test/ryu.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,11 @@ end # Float16
558558
@test Ryu.writefixed(1.25e+5, 1, false, false, false, UInt8('.'), true) == "125000"
559559
@test Ryu.writefixed(1.25e+5, 2, false, false, false, UInt8('.'), true) == "125000"
560560
end
561+
562+
@test Ryu.writefixed(100.0-eps(100.0), 0, false, false, true, UInt8('.'), false) == "100."
563+
@test Ryu.writefixed(-100.0+eps(-100.0), 0, false, false, true, UInt8('.'), false) == "-100."
564+
@test Ryu.writefixed(100.0-eps(100.0), 1, false, false, true, UInt8('.'), false) == "100.0"
565+
@test Ryu.writefixed(-100.0+eps(-100.0), 1, false, false, true, UInt8('.'), false) == "-100.0"
561566
end # fixed
562567

563568
@testset "Ryu.writeexp" begin

0 commit comments

Comments
 (0)