| 1 | module scheme |
| 2 | |
| 3 | import 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. |
| 9 | fn 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 | |