Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
2 changes: 2 additions & 0 deletions base/float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion base/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions test/rounding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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.234))
@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
Expand Down