diff --git a/base/mpfr.jl b/base/mpfr.jl index c85531856f5c9..cd8c8092e0720 100644 --- a/base/mpfr.jl +++ b/base/mpfr.jl @@ -294,7 +294,14 @@ function round(::Type{T}, x::BigFloat, r::Union{RoundingMode, MPFRRoundingMode}) end return unsafe_trunc(T, res) end -round(::Type{BigInt}, x::BigFloat, r::Union{RoundingMode, MPFRRoundingMode}) = _unchecked_cast(BigInt, x, r) + +function round(::Type{BigInt}, x::BigFloat, r::Union{RoundingMode, MPFRRoundingMode}) + clear_flags() + res = _unchecked_cast(BigInt, x, r) + had_range_exception() && throw(InexactError(:round, BigInt, x)) + return res +end + round(::Type{T}, x::BigFloat, r::RoundingMode) where T<:Union{Signed, Unsigned} = invoke(round, Tuple{Type{<:Union{Signed, Unsigned}}, BigFloat, Union{RoundingMode, MPFRRoundingMode}}, T, x, r) round(::Type{BigInt}, x::BigFloat, r::RoundingMode) = diff --git a/test/mpfr.jl b/test/mpfr.jl index 86c7d345f49fd..54ec018327459 100644 --- a/test/mpfr.jl +++ b/test/mpfr.jl @@ -667,6 +667,10 @@ end @test typeof(round(Int64, x)) == Int64 && round(Int64, x) == 42 @test typeof(round(Int, x)) == Int && round(Int, x) == 42 @test typeof(round(UInt, x)) == UInt && round(UInt, x) == 0x2a + + # Issue #44662 + @test_throws InexactError round(Integer, big(Inf)) + @test_throws InexactError round(Integer, big(NaN)) end @testset "string representation" begin str = "1.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012"