Skip to content

Commit 95e9441

Browse files
committed
Changes from review feedback.
1 parent d2f6811 commit 95e9441

File tree

4 files changed

+39
-18
lines changed

4 files changed

+39
-18
lines changed

Sources/ComplexModule/Documentation.docc/Complex.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,24 @@
66

77
A `Complex` value is represented with two `RealType` values, corresponding to
88
the real and imaginary parts of the number:
9+
910
```swift
1011
let z = Complex(1,-1) // 1 - i
1112
let re = z.real // 1
1213
let im = z.imaginary // -1
1314
```
15+
1416
All `Complex` numbers with a non-finite component is treated as a single
1517
"point at infinity," with infinite magnitude and indeterminant phase. Thus,
1618
the real and imaginary parts of an infinity are nan.
19+
1720
```swift
1821
let w = Complex<Double>.infinity
1922
w == -w // true
2023
let re = w.real // .nan
2124
let im = w.imag // .nan
2225
```
26+
2327
See <doc:Infinity> for more details.
2428

2529
- ``init(_:_:)``

Sources/ComplexModule/Documentation.docc/ComplexModule.md

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Types and operations for working with complex numbers.
77
The `Complex` type is generic over an associated `RealType`; complex numbers
88
are represented as two `RealType` values, the real and imaginary parts of the
99
number.
10+
1011
```
1112
let z = Complex<Double>(1, 2)
1213
let re = z.real
@@ -32,32 +33,42 @@ map them into Swift types by converting pointers.
3233
Because the real numbers are a subset of the complex numbers, many
3334
languages support arithmetic with mixed real and complex operands.
3435
For example, C allows the following:
36+
3537
```c
3638
#include <complex.h>
3739
double r = 1;
3840
double complex z = CMPLX(0, 2); // 2i
3941
double complex w = r + z; // 1 + 2i
4042
```
41-
The `Complex` type does not provide such mixed operators. There are two
42-
reasons for this choice. First, Swift generally avoids mixed-type
43-
arithmetic, period. Second, mixed-type arithmetic operators lead to
44-
undesirable behavior in common expressions when combined with literal
45-
type inference. Consider the following example:
43+
44+
The `Complex` type does not provide such mixed operators:
45+
4646
```swift
47-
let a: Double = 1
48-
let b = 2*a
47+
let r = 1.0
48+
let z = Complex(imaginary: 2.0)
49+
let w = r + z // error: binary operator '+' cannot be applied to operands of type 'Double' and 'Complex<Double>'
4950
```
50-
If we had a heterogeneous `*` operator defined, then if there's no prevailing
51-
type context (i.e. we aren't in an extension on some type), the expression
52-
`2*a` is ambiguous; `2` could be either a `Double` or `Complex<Double>`. In
53-
a `Complex` context, the situation is even worse: `2*a` is inferred to have
54-
type `Complex`.
55-
56-
Therefore, the `Complex` type does not have these operators. In order to write
57-
the example from C above, you would use an explicit conversion:
51+
52+
In order to write the example from C above in Swift, you have to perform an
53+
explicit conversion:
54+
5855
```swift
59-
import ComplexModule
6056
let r = 1.0
61-
let z = Complex<Double>(0, 2)
62-
let w = Complex(r) + z
57+
let z = Complex(imaginary: 2.0)
58+
let w = Complex(r) + z // OK
59+
```
60+
61+
There are two reasons for this choice. Most importantly, Swift generally avoids
62+
mixed-type arithmetic. Second, if we _did_ provide such heterogeneous operators,
63+
it would lead to undesirable behavior in common expressions when combined with
64+
literal type inference. Consider the following example:
65+
66+
```swift
67+
let a: Double = 1
68+
let b = 2*a
6369
```
70+
71+
`b` ought to have type `Double`, but if we did have a Complex-by-Real `*`
72+
operation, `2*a` would either be ambiguous (if there were no type context),
73+
or be inferred to have type `Complex<Double>` (if the expression appeared
74+
in the context of an extension defined on `Compex`).

Sources/ComplexModule/Documentation.docc/Infinity.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,21 @@ In particular, complex multiplication is the most common operation performed
1919
with a complex type, and one would like to be able to use the usual naive
2020
arithmetic implementation, consisting of four real multiplications and two
2121
real additions:
22+
2223
```
2324
(a + bi) * (c + di) = (ac - bd) + (ad + bc)i
2425
```
26+
2527
`Complex` can use this implementation, because we do not differentiate between
2628
infinities and NaN values. C and C++, by contrast, cannot use this
2729
implementation by default, because, for example:
30+
2831
```
2932
(1 + ∞i) * (0 - 2i) = (1*0 - ∞*(-2)) + (1*(-2) + ∞*0)i
3033
= (0 - ∞) + (-2 + nan)i
3134
= -∞ + nan i
3235
```
36+
3337
`Complex` treats this as "infinity", which is the correct result. C and C++
3438
treat it as a nan value, however, which is incorrect; infinity multiplied
3539
by a non-zero number should be infinity. Thus, C and C++ (by default) must

Sources/ComplexModule/Documentation.docc/Magnitude.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ However, there are good reasons to make a different choice:
6262

6363
For these reasons, the `magnitude` property of `Complex` binds the
6464
maximum norm:
65+
6566
```swift
6667
Complex(2, 3).magnitude // 3
6768
Complex(-1, 0.5).magnitude // 1
@@ -92,6 +93,7 @@ The `length` property takes special care to produce an accurate answer,
9293
even when the value is poorly-scaled. The naive expression for `length`
9394
would be `sqrt(x*x + y*y)`, but this can overflow or underflow even when
9495
the final result should be a finite number.
96+
9597
```swift
9698
// Suppose that length were implemented like this:
9799
extension Complex {

0 commit comments

Comments
 (0)