Skip to content

Commit 16944c3

Browse files
barucdenKristofferC
authored andcommitted
MPFR: Fix round(Integer, big(Inf)) (#44676)
It also fixes `round(Integer, big(NaN))`. Solves #44662 (cherry picked from commit ecf3558)
1 parent 008432c commit 16944c3

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

base/mpfr.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,14 @@ function round(::Type{T}, x::BigFloat, r::Union{RoundingMode, MPFRRoundingMode})
294294
end
295295
return unsafe_trunc(T, res)
296296
end
297-
round(::Type{BigInt}, x::BigFloat, r::Union{RoundingMode, MPFRRoundingMode}) = _unchecked_cast(BigInt, x, r)
297+
298+
function round(::Type{BigInt}, x::BigFloat, r::Union{RoundingMode, MPFRRoundingMode})
299+
clear_flags()
300+
res = _unchecked_cast(BigInt, x, r)
301+
had_range_exception() && throw(InexactError(:round, BigInt, x))
302+
return res
303+
end
304+
298305
round(::Type{T}, x::BigFloat, r::RoundingMode) where T<:Union{Signed, Unsigned} =
299306
invoke(round, Tuple{Type{<:Union{Signed, Unsigned}}, BigFloat, Union{RoundingMode, MPFRRoundingMode}}, T, x, r)
300307
round(::Type{BigInt}, x::BigFloat, r::RoundingMode) =

test/mpfr.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,10 @@ end
667667
@test typeof(round(Int64, x)) == Int64 && round(Int64, x) == 42
668668
@test typeof(round(Int, x)) == Int && round(Int, x) == 42
669669
@test typeof(round(UInt, x)) == UInt && round(UInt, x) == 0x2a
670+
671+
# Issue #44662
672+
@test_throws InexactError round(Integer, big(Inf))
673+
@test_throws InexactError round(Integer, big(NaN))
670674
end
671675
@testset "string representation" begin
672676
str = "1.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012"

0 commit comments

Comments
 (0)