@@ -1077,19 +1077,22 @@ end
1077
1077
# Rounding complex numbers
1078
1078
# Requires two different RoundingModes for the real and imaginary components
1079
1079
"""
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)
1083
1086
1084
1087
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.
1088
1092
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.
1093
1096
1094
1097
# Example
1095
1098
```jldoctest
@@ -1106,7 +1109,13 @@ julia> round(3.14159 + 4.512im; sigdigits = 3)
1106
1109
3.14 + 4.51im
1107
1110
```
1108
1111
"""
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... )
1110
1119
Complex (round (real (z), rr; kwargs... ),
1111
1120
round (imag (z), ri; kwargs... ))
1112
1121
end
0 commit comments