Skip to content

Commit

Permalink
MPFR: Fix round(Integer, big(Inf)) (#44676)
Browse files Browse the repository at this point in the history
It also fixes `round(Integer, big(NaN))`.

Solves #44662
  • Loading branch information
barucden authored Mar 21, 2022
1 parent feb7b77 commit ecf3558
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 8 additions & 1 deletion base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down
4 changes: 4 additions & 0 deletions test/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit ecf3558

Please sign in to comment.