Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine signif into round, use keyword args for digits/sigdigits #26670

Merged
merged 20 commits into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix complex
  • Loading branch information
simonbyrne committed Apr 3, 2018
commit f9337f6e552fc7e24631a2481ea3ea55916fba5c
15 changes: 6 additions & 9 deletions base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,9 @@ atanh(z::Complex) = atanh(float(z))
#Rounding complex numbers
#Requires two different RoundingModes for the real and imaginary components
"""
round(z, RoundingModeReal, RoundingModeImaginary)
round(z::Complex[, RoundingModeReal, [RoundingModeImaginary]])
round(z::Complex[, RoundingModeReal, [RoundingModeImaginary]]; digits=, base=10)
round(z::Complex[, RoundingModeReal, [RoundingModeImaginary]]; sigdigits=, base=10)

Return the nearest integral value of the same type as the complex-valued `z` to `z`,
breaking ties using the specified [`RoundingMode`](@ref)s. The first
Expand All @@ -954,16 +956,11 @@ julia> round(3.14 + 4.5im)
3.0 + 4.0im
```
"""
function round(z::Complex{<:AbstractFloat}, ::RoundingMode{MR}, ::RoundingMode{MI}) where {MR,MI}
Complex(round(real(z), RoundingMode{MR}()),
round(imag(z), RoundingMode{MI}()))
function round(z::Complex, rr::RoundingMode=RoundNearest, ri::RoundingMode=rr; digits=nothing, sigdigits=nothing, base=10)
Complex(round(real(z), rr; digits=digits, sigdigits=sigdigits, base=base),
round(imag(z), ri; digits=digits, sigdigits=sigdigits, base=base))
end
round(z::Complex) = Complex(round(real(z)), round(imag(z)))

function round(z::Complex, digits::Integer; base::Integer = 10)
Complex(round(real(z), digits, base = base),
round(imag(z), digits, base = base))
end

float(z::Complex{<:AbstractFloat}) = z
float(z::Complex) = Complex(float(real(z)), float(imag(z)))
Expand Down
4 changes: 2 additions & 2 deletions test/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -893,13 +893,13 @@ end
end

@testset "round and float, PR #8291" begin
@test round(Complex(1.125, 0.875), 2) == Complex(1.12, 0.88)
@test round(Complex(1.125, 0.875), digits=2) == Complex(1.12, 0.88)
@test round(Complex(1.5, 0.5), RoundDown, RoundUp) == Complex(1.0, 1.0)
@test round.([1:5;] .+ im) == [1:5;] .+ im
@test round.([1:5;] .+ 0.5im) == [1.0:5.0;]

@test float(Complex(1, 2)) == Complex(1.0, 2.0)
@test round(float(Complex(π, ℯ)),3) == Complex(3.142, 2.718)
@test round(float(Complex(π, ℯ)), digits=3) == Complex(3.142, 2.718)
end

@testset "ComplexF16 arithmetic, PR #10003" begin
Expand Down