Skip to content

Commit 3aea11e

Browse files
authored
Fix overflow when rounding (#42033)
1 parent d0d895e commit 3aea11e

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

base/floatfuncs.jl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,16 @@ function _round_invstep(x, invstep, r::RoundingMode)
166166
return y
167167
end
168168

169+
# round x to multiples of 1/(invstepsqrt^2)
170+
# Using square root of step prevents overflowing
171+
function _round_invstepsqrt(x, invstepsqrt, r::RoundingMode)
172+
y = round((x * invstepsqrt) * invstepsqrt, r) / invstepsqrt / invstepsqrt
173+
if !isfinite(y)
174+
return x
175+
end
176+
return y
177+
end
178+
169179
# round x to multiples of step
170180
function _round_step(x, step, r::RoundingMode)
171181
# TODO: use div with rounding mode
@@ -186,10 +196,15 @@ function _round_digits(x, r::RoundingMode, digits::Integer, base)
186196
fx = float(x)
187197
if digits >= 0
188198
invstep = oftype(fx, base)^digits
189-
_round_invstep(fx, invstep, r)
199+
if isfinite(invstep)
200+
return _round_invstep(fx, invstep, r)
201+
else
202+
invstepsqrt = oftype(fx, base)^oftype(fx, digits/2)
203+
return _round_invstepsqrt(fx, invstepsqrt, r)
204+
end
190205
else
191206
step = oftype(fx, base)^-digits
192-
_round_step(fx, step, r)
207+
return _round_step(fx, step, r)
193208
end
194209
end
195210

test/floatfuncs.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,23 @@ end
119119
@test round(Float32(1.2), sigdigits=5) === Float32(1.2)
120120
@test round(Float16(0.6), sigdigits=2) === Float16(0.6)
121121
@test round(Float16(1.1), sigdigits=70) === Float16(1.1)
122+
123+
# issue 37171
124+
@test round(9.87654321e-308, sigdigits = 1) 1.0e-307
125+
@test round(9.87654321e-308, sigdigits = 2) 9.9e-308
126+
@test round(9.87654321e-308, sigdigits = 3) 9.88e-308
127+
@test round(9.87654321e-308, sigdigits = 4) 9.877e-308
128+
@test round(9.87654321e-308, sigdigits = 5) 9.8765e-308
129+
@test round(9.87654321e-308, sigdigits = 6) 9.87654e-308
130+
@test round(9.87654321e-308, sigdigits = 7) 9.876543e-308
131+
@test round(9.87654321e-308, sigdigits = 8) 9.8765432e-308
132+
@test round(9.87654321e-308, sigdigits = 9) 9.87654321e-308
133+
@test round(9.87654321e-308, sigdigits = 10) 9.87654321e-308
134+
@test round(9.87654321e-308, sigdigits = 11) 9.87654321e-308
135+
136+
@inferred round(Float16(1.), sigdigits=2)
137+
@inferred round(Float32(1.), sigdigits=2)
138+
@inferred round(Float64(1.), sigdigits=2)
122139
end
123140

124141
@testset "literal pow matches runtime pow matches optimized pow" begin

0 commit comments

Comments
 (0)