From 01a01ef063c6d6e7d81cd663e85cc4af034585f1 Mon Sep 17 00:00:00 2001 From: Denis Barucic Date: Mon, 21 Mar 2022 13:06:29 +0100 Subject: [PATCH] MPFR: Fix `round(Integer, big(Inf))` (#44676) It also fixes `round(Integer, big(NaN))`. Solves #44662 (cherry picked from commit ecf3558c94898ddd4272b319d3405cf7256c6db7) --- base/mpfr.jl | 9 ++++++++- test/mpfr.jl | 4 ++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/base/mpfr.jl b/base/mpfr.jl index e85f281619ac0..60f59cdb0af7e 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 a1039a7c5a810..1a0a0041bf94e 100644 --- a/test/mpfr.jl +++ b/test/mpfr.jl @@ -653,6 +653,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"