Skip to content

Commit 61a8b91

Browse files
committed
Some formatting cleanup around code blocks.
1 parent c3757dc commit 61a8b91

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

Sources/ComplexModule/Documentation.docc/Magnitude.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,16 @@ However, there are good reasons to make a different choice:
5050
- Even when special care is used, the Euclidean and taxicab norms are
5151
not necessarily representable. Both can be infinite even for finite
5252
numbers.
53+
5354
```swift
5455
let big = Double.greatestFiniteMagnitude
5556
let z = Complex(big, big)
5657
```
58+
5759
The taxicab norm of `z` would be `big + big`, which overflows; the
5860
Euclidean norm would be `sqrt(2) * big`, which also overflows.
59-
6061
But the maximum norm is always equal to the magnitude of either `real`
61-
or `imaginary`, so it is necessarily representable if `z` is finite.
62+
or `imaginary`, so it is necessarily representable.
6263
- The ∞-norm is the choice of established computational libraries, like
6364
BLAS and LAPACK.
6465

@@ -113,11 +114,13 @@ z.naiveLength // Inf
113114
// or underflow:
114115
w.naiveLength // 0
115116
```
117+
116118
Instead, `length` is implemented using a two-step algorithm. First we
117119
compute `lengthSquared`, which is `x*x + y*y`. If this is a normal
118120
number (meaning that no overflow or underflow has occured), we can safely
119121
return its square root. Otherwise, we redo the computation with a more
120122
careful computation, which avoids spurious under- or overflow:
123+
121124
```swift
122125
let z = Complex<Float>(1e20, 1e20)
123126
let w = Complex<Float>(1e-24, 1e-24)

Sources/RealModule/Documentation.docc/Augmented.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,29 @@ or the result is exact.
99
Consider multiplying two Doubles. A Double has 53 significand bits, so their
1010
product could be up to 106 bits wide before it is rounded to a Double result.
1111
So up to 53 of those 106 bits will be "lost" in that process:
12+
1213
```swift
1314
let a = 1.0 + .ulpOfOne // 1 + 2⁻⁵²
1415
let b = 1.0 - .ulpOfOne // 1 - 2⁻⁵²
1516
let c = a * b // 1 - 2⁻¹⁰⁴ before rounding, rounds to 1.0
1617
```
18+
1719
Sometimes it is necessary to preserve some or all of those low-order bits;
1820
maybe a subsequent subtraction cancels most of the high-order bits, and so
1921
the low-order part of the product suddenly becomes significant:
22+
2023
```swift
2124
let result = 1 - c // exactly zero, but "should be" 2⁻¹⁰⁴
2225
```
26+
2327
Augmented arithmetic is a building-block that library writers can use to
2428
handle cases like this more carefully. For the example above, one might
2529
compute:
30+
2631
```swift
2732
let (head, tail) = Augmented.product(a,b)
2833
```
34+
2935
`head` is then 1.0 and `tail` is -2⁻¹⁰⁴, so no information has been lost.
3036
Of course, the result is now split across two Doubles instead of one, but the
3137
information in `tail` can be carried forward into future computations.

Sources/RealModule/Documentation.docc/Relaxed.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ floating-point to allow reassociation of arithmetic and FMA formation.
77

88
Because of rounding, and the arithmetic rules for infinity and NaN values,
99
floating-point addition and multiplication are not associative:
10+
1011
```swift
1112
let ε = Double.leastNormalMagnitude
1213
let sumLeft = (-1 + 1) + ε // 0 + ε = ε
@@ -16,6 +17,7 @@ let ∞ = Double.infinity
1617
let productLeft =* ε) * // 0 * ∞ = .nan
1718
let productRight = ε ** ) // ε * ∞ = ∞
1819
```
20+
1921
For some algorithms, the distinction between these results is incidental; for
2022
some others it is critical to their correct function. Because of this,
2123
compilers cannot freely change the order of reductions, which prevents some
@@ -26,6 +28,7 @@ If you know that you are in a case where the order of elements being summed
2628
or multiplied is incidental, the Relaxed operations give you a mechanism
2729
to communicate that to the compiler and unlock these optimizations. For
2830
example, consider the following two functions:
31+
2932
```swift
3033
func sum(array: [Float]) -> Float {
3134
array.reduce(0, +)
@@ -35,13 +38,14 @@ func relaxedSum(array: [Float]) -> Float {
3538
array.reduce(0, Relaxed.sum)
3639
}
3740
```
41+
3842
when called on an array with 1000 elements in a Release build, `relaxedSum`
3943
is about 8x faster than `sum` on Apple M2, with a similar speedup on Intel
4044
processors, without the need for any unsafe code or flags.
4145

4246
### multiplyAdd
4347

44-
In addition to `Relaxed.sum` and `Relaxed.product`, `Relaxed` provides the
48+
In addition to `sum` and `product`, `Relaxed` provides the
4549
``multiplyAdd(_:_:_:)`` operation, which communciates to the compiler that
4650
it is allowed to replace separate multiply and add operations with a single
4751
_fused multiply-add_ instruction if its cost model indicates that it would

0 commit comments

Comments
 (0)