From d64ea8ee1accb50057286b207269f9e5f1ccd121 Mon Sep 17 00:00:00 2001 From: sam0410 Date: Thu, 20 Dec 2018 04:21:02 +0530 Subject: [PATCH 1/2] Add missing methods to round --- base/complex.jl | 3 ++- base/float.jl | 2 ++ base/rational.jl | 3 ++- test/complex.jl | 13 +++++++++++++ test/rounding.jl | 10 ++++++++++ 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/base/complex.jl b/base/complex.jl index 7cf266f2bc4de..51852133e4db9 100644 --- a/base/complex.jl +++ b/base/complex.jl @@ -1043,7 +1043,8 @@ function round(z::Complex, rr::RoundingMode=RoundNearest, ri::RoundingMode=rr; k Complex(round(real(z), rr; kwargs...), round(imag(z), ri; kwargs...)) end - +round(::Type{Complex{T}}, x::Complex) where {T<:Real} = Complex(round(T, x.re), round(T, x.im)) +round(::Type{Complex{T}}, x::Real) where {T<:Real} = Complex(round(T, x)) float(z::Complex{<:AbstractFloat}) = z float(z::Complex) = Complex(float(real(z)), float(imag(z))) diff --git a/base/float.jl b/base/float.jl index cb4000b51fac2..cecb20a3a4924 100644 --- a/base/float.jl +++ b/base/float.jl @@ -318,6 +318,8 @@ trunc(::Type{Integer}, x::Float64) = trunc(Int,x) floor(::Type{T}, x::AbstractFloat) where {T<:Integer} = trunc(T,round(x, RoundDown)) ceil(::Type{T}, x::AbstractFloat) where {T<:Integer} = trunc(T,round(x, RoundUp)) round(::Type{T}, x::AbstractFloat) where {T<:Integer} = trunc(T,round(x, RoundNearest)) +round(::Type{T}, x::Float16) where {T<:Integer} = round(T, Float32(x)) +round(::Type{T}, x::Real) where {T<:Real} = T(round(x)) round(x::Float64, r::RoundingMode{:ToZero}) = trunc_llvm(x) round(x::Float32, r::RoundingMode{:ToZero}) = trunc_llvm(x) diff --git a/base/rational.jl b/base/rational.jl index a998787f69685..c1d1665e786d0 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -454,6 +454,7 @@ end trunc(::Type{T}, x::Rational) where {T} = round(T, x, RoundToZero) floor(::Type{T}, x::Rational) where {T} = round(T, x, RoundDown) ceil(::Type{T}, x::Rational) where {T} = round(T, x, RoundUp) +round(::Type{T}, x::Rational) where {T<:Real} = round(T, x, RoundNearest) round(x::Rational, r::RoundingMode=RoundNearest) = round(typeof(x), x, r) @@ -464,7 +465,7 @@ function round(::Type{T}, x::Rational{Tr}, r::RoundingMode=RoundNearest) where { convert(T, div(numerator(x), denominator(x), r)) end -function round(::Type{T}, x::Rational{Bool}, ::RoundingMode=RoundNearest) where T +function round(::Type{T}, x::Rational{Bool}, ::RoundingMode=RoundNearest) where {T<:Real} if denominator(x) == false && (T <: Integer) throw(DivideError()) end diff --git a/test/complex.jl b/test/complex.jl index 56c446896bf53..6ccc65827718e 100644 --- a/test/complex.jl +++ b/test/complex.jl @@ -963,6 +963,19 @@ end @test round(float(Complex(π, ℯ)), digits=3) == Complex(3.142, 2.718) end +@testset "For issue #24021" begin + for T in (Int16, Int32, Int64, Int128), U in (Float32, Float64) + a = round(Complex{T}, Complex{U}(2.9, 1.1)) + b = round(Complex{U}, Complex{T}(3, 1)) + @test a.im isa T + @test b.im isa U + @test a == Complex(3,1) + @test b == Complex(3,1) + @test round(Complex{U}, T(3)) == Complex(3,0) + @test round(Complex{T}, U(2.9)) == Complex(3,0) + end +end + @testset "ComplexF16 arithmetic, PR #10003" begin @test Float16(1)+Float16(1)im === ComplexF16(1, 1) @test Float16(1)-Float16(1)im === Float16(1)+Float16(-1)im === ComplexF16(1, -1) diff --git a/test/rounding.jl b/test/rounding.jl index e4c51212e81fa..f1189eb0f37bd 100644 --- a/test/rounding.jl +++ b/test/rounding.jl @@ -77,6 +77,16 @@ end end end end + +@testset "For issue #24021" begin + types = [Float16, Float32, Float64, BigFloat, Int16, Int32, Int64, BigInt] + for T in types, S in types[1:4] + x = round(T, S(1.1)) + @test x == 1 + @test x isa T + end +end + @testset "fenv" begin @test Base.Rounding.from_fenv(Base.Rounding.to_fenv(RoundNearest)) == RoundNearest @test Base.Rounding.from_fenv(Base.Rounding.to_fenv(RoundToZero)) == RoundToZero From 4c1757151138c7c95c809b9a736394886504d666 Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Tue, 17 Aug 2021 09:31:39 -0400 Subject: [PATCH 2/2] pointless change to trigger CI --- test/rounding.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/rounding.jl b/test/rounding.jl index f1189eb0f37bd..4d46cf4714db5 100644 --- a/test/rounding.jl +++ b/test/rounding.jl @@ -81,7 +81,7 @@ end @testset "For issue #24021" begin types = [Float16, Float32, Float64, BigFloat, Int16, Int32, Int64, BigInt] for T in types, S in types[1:4] - x = round(T, S(1.1)) + x = round(T, S(1.234)) @test x == 1 @test x isa T end