Skip to content

Commit 4d1f7a2

Browse files
committed
See if we can remove mathematically iffy Complex rounding modes
1 parent d90fa45 commit 4d1f7a2

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

base/complex.jl

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,19 +1077,22 @@ end
10771077
#Rounding complex numbers
10781078
#Requires two different RoundingModes for the real and imaginary components
10791079
"""
1080-
round(z::Complex[, RoundingModeReal, [RoundingModeImaginary]])
1081-
round(z::Complex[, RoundingModeReal, [RoundingModeImaginary]]; digits=0, base=10)
1082-
round(z::Complex[, RoundingModeReal, [RoundingModeImaginary]]; sigdigits, base=10)
1080+
round(z::Complex[, RoundingMode])
1081+
round(z::Complex[, RoundingMode]; digits=0, base=10)
1082+
round(z::Complex[, RoundingMode]; sigdigits=0, base=10)
1083+
round(z::Complex, RoundingModeReal, RoundingModeImaginary;)
1084+
round(z::Complex, RoundingModeReal, RoundingModeImaginary; digits=0, base=10)
1085+
round(z::Complex, RoundingModeReal, RoundingModeImaginary; sigdigits, base=10)
10831086
10841087
Return the nearest integral value of the same type as the complex-valued `z` to `z`,
1085-
breaking ties using the specified [`RoundingMode`](@ref)s. The first
1086-
[`RoundingMode`](@ref) is used for rounding the real components while the
1087-
second is used for rounding the imaginary components.
1088+
breaking ties using the specified [`RoundingMode`](@ref)s. When provided a single
1089+
[`RoundingMode`](@ref), the mode must be one of `RoundNearest`, `RoundFromZero`, `RoundToZero`,
1090+
or `RoundNearestTiesAway`, since these are the modes which do not rely on a total ordering which
1091+
the complex numbers lack.
10881092
1089-
1090-
`RoundingModeReal` and `RoundingModeImaginary` default to [`RoundNearest`](@ref),
1091-
which rounds to the nearest integer, with ties (fractional values of 0.5)
1092-
being rounded to the nearest even integer.
1093+
Complex numbers can also be passed two rounding modes, one for the real part and another for
1094+
the imaginary part. In this case, the Complex number's components will be rounded seperately
1095+
using the provided rounding modes, and combined back into a Complex number.
10931096
10941097
# Example
10951098
```jldoctest
@@ -1106,7 +1109,13 @@ julia> round(3.14159 + 4.512im; sigdigits = 3)
11061109
3.14 + 4.51im
11071110
```
11081111
"""
1109-
function round(z::Complex, rr::RoundingMode=RoundNearest, ri::RoundingMode=rr; kwargs...)
1112+
function round(z::Complex, rr::RoundingMode; kwargs...)
1113+
if rr in (RoundNearest, RoundFromZero, RoundToZero, RoundNearestTiesAway)
1114+
return round(z, rr, rr; kwargs...)
1115+
end
1116+
error("Rounding Mode $rr not supported for Complex numbers. Use round(z, $rr, $rr)")
1117+
end
1118+
function round(z::Complex, rr::RoundingMode, ri::RoundingMode; kwargs...)
11101119
Complex(round(real(z), rr; kwargs...),
11111120
round(imag(z), ri; kwargs...))
11121121
end

0 commit comments

Comments
 (0)