vile / scheme / complex_compensation.v
16 lines · 14 sloc · 553 bytes · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import math
4
5// Recover product rounding errors only near cancellation. The C backend's
6// math.fma rounds each product residual once. Division retains its checks on
7// all four normalized products before calling this helper, keeping these
8// residuals in the normal range even when the final quotient is tiny.
9fn compensated_product_sum(a f64, b f64, c f64, d f64) f64 {
10 p, q := a * b, c * d
11 s := p + q
12 if math.abs(s) < 0.01 * math.max(math.abs(p), math.abs(q)) {
13 return s + (math.fma(a, b, -p) + math.fma(c, d, -q))
14 }
15 return s
16}
17