1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// src/accum.rs
//! Compensated accumulation of sums and dot products.
//!
//! # Numerical argument
//!
//! [`Accum`] implements `Dot2` (and its special case `Sum2`) from
//! T. Ogita, S. M. Rump, S. Oishi, "Accurate Sum and Dot Product",
//! SIAM J. Sci. Comput. 26(6), 2005, Algorithm 5.3. Each product is split
//! into the rounded product and its exact rounding error with `two_product`,
//! and each running-sum step is split the same way with `two_sum`; the
//! rounding errors are accumulated separately and added once at the end.
//!
//! The paper proves that, barring overflow and underflow, the result `r` of
//! summing `m` terms satisfies
//!
//! ```text
//! |r - exact| <= eps * |exact| + gamma_m^2 * sum(|terms|)
//! ```
//!
//! with `eps = 2^-53` and `gamma_m = m * eps / (1 - m * eps)`. That is the
//! accuracy of a computation carried out in twice the working precision and
//! then rounded once. The second term is of order `m^2 * eps^2`, which is
//! negligible next to `eps * sum(|terms|)` (the bound for a plain sum) for any
//! realistic `m`. The shoelace area of a polygon far from the origin is the
//! motivating case: the individual products are huge, the true area is small,
//! and a plain `f64` sum loses most or all of its significant digits to
//! cancellation, while `Dot2` does not.
//!
//! The bound does not hold if a `two_product` overflows (its Veltkamp split
//! multiplies by `2^27 + 1`, so operands above about `1.3e300` overflow) or
//! if a product's rounding error underflows (products below about `1e-292`).
//! In the overflow case the result is non-finite, not a finite wrong value:
//! an infinity can only become finite again through `inf - inf`, which is NaN.
//! In the underflow case the error terms are lost, so the result degrades to
//! a plain sum's accuracy at an absolute scale below `1e-292`.
use ;
/// Running compensated sum of products and values: Ogita–Rump–Oishi `Dot2`.
///
/// # What it guarantees
///
/// **An approximate magnitude with a proven bound. It certifies no sign by
/// itself.** T. Ogita, S. M. Rump, S. Oishi, "Accurate Sum and Dot Product",
/// SIAM J. Sci. Comput. 26(6), 2005, Algorithm 5.3, proves that after `m`
/// terms (each [`add`](Accum::add) or [`add_product`](Accum::add_product)
/// is one term) the result `r` of [`value`](Accum::value) satisfies
///
/// ```text
/// |r − exact| ≤ ε·|exact| + γ_m²·Σ|terms|, ε = 2⁻⁵³, γ_m = m·ε / (1 − m·ε)
/// ```
///
/// which is the accuracy of a computation in twice the working precision,
/// rounded once. When `|exact|` is far smaller than `Σ|terms|`, which is
/// the cancellation this type exists for, the second term can exceed
/// `|exact|`, and then the sign of `r` is not the sign of the exact sum. A caller that
/// needs the sign must prove it separately. `ops::area` does: its
/// certification test compares `|r|` against four times the bound above and
/// falls back to exact expansion arithmetic when the test fails. That test is
/// private to `ops::area`; this type does not perform it.
///
/// # Where the bound fails
///
/// - **Overflow.** `two_product`'s Veltkamp split multiplies by `2²⁷ + 1`, so
/// operands above about `1.3e300` overflow. The result is then non-finite,
/// **never a finite wrong value**. An infinity becomes finite again only
/// through `inf − inf`, which is NaN.
/// - **Underflow.** When a product's rounding error underflows (products
/// below about `1e-292`) the error term is lost, and the result degrades
/// to a plain sum's accuracy at that absolute scale.
///
/// The module documentation carries the full argument.
// ======================================================================
// Tests
// ======================================================================
// test-lint-posture: see the workspace lint comment in Cargo.toml