Skip to content

Commit fb2fd0b

Browse files
vtjnashKristofferC
authored andcommitted
Ryu: make sure adding zeros does not overwrite trailing dot (#51254)
Fix #43129 (cherry picked from commit 832e46d)
1 parent 65a12ef commit fb2fd0b

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
@@ -176,7 +176,7 @@ function writeexp(buf, pos, v::T,
176176
end
177177
roundUp = 0
178178
if lastDigit != 5
179-
roundUp = lastDigit > 5
179+
roundUp = lastDigit > 5 ? 1 : 0
180180
else
181181
rexp = precision - e
182182
requiredTwos = -e2 - rexp

base/ryu/fixed.jl

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

60-
if exp == 0
60+
if exp == 0 # subnormal
6161
e2 = 1 - 1023 - 52
6262
m2 = mant
6363
else
@@ -82,7 +82,7 @@ function writefixed(buf, pos, v::T,
8282
i = len - 1
8383
while i >= 0
8484
j = p10bits - e2
85-
#=@inbounds=# mula, mulb, mulc = POW10_SPLIT[POW10_OFFSET[idx + 1] + i + 1]
85+
mula, mulb, mulc = POW10_SPLIT[POW10_OFFSET[idx + 1] + i + 1]
8686
digits = mulshiftmod1e9(m2 << 8, mula, mulb, mulc, j + 8)
8787
if nonzero
8888
pos = append_nine_digits(digits, buf, pos)
@@ -132,7 +132,7 @@ function writefixed(buf, pos, v::T,
132132
end
133133
break
134134
end
135-
#=@inbounds=# mula, mulb, mulc = POW10_SPLIT_2[p + 1]
135+
mula, mulb, mulc = POW10_SPLIT_2[p + 1]
136136
digits = mulshiftmod1e9(m2 << 8, mula, mulb, mulc, j + 8)
137137
if i < blocks - 1
138138
pos = append_nine_digits(digits, buf, pos)
@@ -147,11 +147,11 @@ function writefixed(buf, pos, v::T,
147147
k += 1
148148
end
149149
if lastDigit != 5
150-
roundUp = lastDigit > 5
150+
roundUp = lastDigit > 5 ? 1 : 0
151151
else
152152
requiredTwos = -e2 - precision - 1
153153
trailingZeros = requiredTwos <= 0 || (requiredTwos < 60 && pow2(m2, requiredTwos))
154-
roundUp = trailingZeros ? 2 : 1
154+
roundUp = trailingZeros ? 2 : 1 # 2 means round only if odd
155155
end
156156
if maximum > 0
157157
pos = append_c_digits(maximum, digits, buf, pos)
@@ -166,13 +166,13 @@ function writefixed(buf, pos, v::T,
166166
while true
167167
roundPos -= 1
168168
if roundPos == (startpos - 1) || (buf[roundPos] == UInt8('-')) || (plus && buf[roundPos] == UInt8('+')) || (space && buf[roundPos] == UInt8(' '))
169+
buf[pos] = UInt8('0')
169170
buf[roundPos + 1] = UInt8('1')
170171
if dotPos > 1
171172
buf[dotPos] = UInt8('0')
172173
buf[dotPos + 1] = decchar
173174
hasfractional = true
174175
end
175-
buf[pos] = UInt8('0')
176176
pos += 1
177177
break
178178
end

test/ryu.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,11 @@ end # Float16
553553
@test Ryu.writefixed(1.25e+5, 1, false, false, false, UInt8('.'), true) == "125000"
554554
@test Ryu.writefixed(1.25e+5, 2, false, false, false, UInt8('.'), true) == "125000"
555555
end
556+
557+
@test Ryu.writefixed(100.0-eps(100.0), 0, false, false, true, UInt8('.'), false) == "100."
558+
@test Ryu.writefixed(-100.0+eps(-100.0), 0, false, false, true, UInt8('.'), false) == "-100."
559+
@test Ryu.writefixed(100.0-eps(100.0), 1, false, false, true, UInt8('.'), false) == "100.0"
560+
@test Ryu.writefixed(-100.0+eps(-100.0), 1, false, false, true, UInt8('.'), false) == "-100.0"
556561
end # fixed
557562

558563
@testset "Ryu.writeexp" begin

0 commit comments

Comments
 (0)