Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

// request.cf · coarse context

A page that knows where it met you.

Only coarse request metadata is shown. This demo does not display or persist visitor IP addresses.

Country
US
Cloudflare location
CMH
Connection
HTTP/2
Language
Not provided

Ray ID: a237ac8b4a36a9c0

Jump to content

Summation algorithm

From Wikipedia, the free encyclopedia

A summation algorithm is an algorithm that computes the sum of a finite list of numbers . It is especially relevant in floating-point arithmetic where the associative property does not hold like it does in (mathematical) real numbers, rational numbers, fixed-point numbers, and unsigned integers, so that the order of calculation can affect the final result. A closely related problem is the calculation of dot products, both of which have numerous proposed algorithms that differ in terms of simplicity, speed (single-thread and parallelized), and accuracy..[1]

Background

[edit]

Floating-point works similarly to a scientific notation with a limited range on the exponent and a limited number of digits on the significand (left part, mantissa). When numbers of very different magnitudes (absolute values) are added together, the result may be unchanged from the one with the larger magnitude, making any information in the smaller one lost. When numbers of very similar magnitudes but of opposite signs are added together, cancellation results, with all but the most inaccurate digits being 0 in the result, with potentially catastrophic consequences for precision.[2]:102[3]

More concretely, the summation operator "+" of floating-point arithmetic is correctly-rounded, i.e. it has to match the rounding mode currently set. In the case of the two "to nearest" modes, the error is guaranteed to be  0.5 ulp, and for the other modes  1 ulp; in all cases, it is deterministic. A ulp is a unit in the last place: the magnitude difference represented by the last digit of the mantissa changing from a 0 to 1, and its own magnitude is determined by the exponent.[4]:§2.1 The error incurred by floating-point rules, relative to the ideal of infinite-precision arithmetic, is called the roundoff error. For the basic case of calculating versus its floating-point version , an algorithm called 2Sum gives both and the exact error . 2Sum forms the basis of many accurate summation algorithms.[2]

The intrinsic sensitivity of asummation problem to errors, regardless of how it is computed, is called its condition number and is defined as . The inherent error from machine precision is notated as .[5]

Naive summation

[edit]

Naive summation works simply by going over the numbers one-by-one, calculating

function sumNaive(L)
  var sum = 0.0
  for i = 1 to L.length do
    sum = sum + L[i]
return sum

The worst-case error for a list L of length n is , corresponding to a case where the rounding errors all add up in the same direction. The average error, corresponding to a random walk of roundoff errors, is .[6]

Naive summation also has a batched version, blocked summation, which entails dividing L into a number of smaller lists, summing each of them naively, then summing the sums naively. This is usually done to allow parallel computation. It also reduces the error growth by a factor of 1/b.[7]

Precise summation

[edit]

On the other extreme of speed-accuracy tradeoff, the correctly-rounded may be computed. The traditional method for doing so is Shewchuk's method, an adaptive simplification of arbitrary-precision arithmetic. This can take anywhere from linear (optimistic, and near typical) to quadratic time complexity and anywhere from constant to linear space complexity.[8]

Pairwise summation

[edit]

One idea for reducing the occurrence of roundoff errors is by making sure most additions happen between values of similar magnitudes. One way of doing so is rearranging the addition into a binary tree of operations, known as pairwise summation. This method takes no additional addition operations compared to the naive method. The worst-case error is and the average-case error is .[9]

Pairwise summation can run in parallel to a factor of given light modification.[10]

Compensated summation

[edit]

Another way to reducing the occurrence of roundoff errors is to make use of the aforementioned 2Sum routine, a compensated summation:[11]

function KahanBabushkaNeumaierSum2(input)
    var sum = 0.0
    var c = 0.0
    for i = 1 to input.length do
        var y = input[i] + c
        (sum,c) = 2Sum(sum,y)
    next i

    return sum

The typical error bound is .[6]

Compensated summation can be made parallel by using a conversion similar to the change from naive to block summation: split the list into several (m) pieces, compute a small number of sub-sums using the compensated method in parallel, then sum them up together using the regular compensated method.[12]

Hybrid methods

[edit]

Depending on the nature of the data, a mixture of summation methods can be used. For example, the FABsum ("fast and accurate block summation") uses a block structure, also dividing te input into m pieces. The m sub-sums are to be generated using a fast method such as the naive one. The sub-sums are then added together using a more accurate method. The error bound is , where b is the block size (i.e. ) and u is the unit roundoff (). As a result, a reasonable block size such as 128 or 256 can end up doing the great majority of the summations in a "fast" way, while achieving a constant error magnitude comparable to compensated summation.[13]

Library functions

[edit]

Python includes the precise summation routine as math.fsum.[14] A few ports are available.[15] ECMAScript 2026 includes the analogous Math.sumPrecise().[16]

A compensated summation routine (Neumaier) is imcluded in CPython as sum since version 3.12 and higher.[17]

In the Julia language, the default implementation of the sum function does pairwise summation for high accuracy with good performance.[18]

References

[edit]
  1. Rump, Siegfried M; Ogita, Takeshi; Oishi, Shin’Ichi (2005-11-13). ACCURATE FLOATING-POINT SUMMATION. Faculty for Information- and Communication Sciences, Hamburg University of Technology.
  2. 1 2 Muller, Jean-Michel; Brunie, Nicolas; de Dinechin, Florent; Jeannerod, Claude-Pierre; Joldes, Mioara; Lefèvre, Vincent; Melquiond, Guillaume; Revol, Nathalie; Torres, Serge (2018). Handbook of Floating-Point Arithmetic (2nd ed.). Gewerbestrasse 11, 6330 Cham, Switzerland: Birkhäuser. doi:10.1007/978-3-319-76526-6. ISBN 978-3-319-76525-9.{{cite book}}: CS1 maint: location (link)
  3. Goldberg, David (March 1991). "What every computer scientist should know about floating-point arithmetic". ACM Computing Surveys. 23 (1). New York, NY, United States: Association for Computing Machinery: 5–48. doi:10.1145/103162.103163. ISSN 0360-0300. S2CID 222008826. Retrieved 2020-09-17.
  4. IEEE Computer Society (22 July 2019). IEEE Standard for Floating-Point Arithmetic. IEEE STD 754-2019. IEEE. pp. 1–82. doi:10.1109/IEEESTD.2019.8766229. ISBN 978-1-5044-5924-2. IEEE Std 754-2019.
  5. Trefethen, Lloyd N.; Bau, David (1997). Numerical Linear Algebra. Philadelphia: SIAM. ISBN 978-0-89871-361-9.
  6. 1 2 Higham, Nicholas J. (1993), "The accuracy of floating point summation", SIAM Journal on Scientific Computing, 14 (4): 783–799, Bibcode:1993SJSC...14..783H, CiteSeerX 10.1.1.43.3535, doi:10.1137/0914050, S2CID 14071038.
  7. Blanchard, Pierre; Higham, Nicholas J.; Mary, Theo (January 2020). "A Class of Fast and Accurate Summation Algorithms". SIAM Journal on Scientific Computing. 42 (3): A1541–A1557. doi:10.1137/19M1257780.
  8. Richard Shewchuk, Jonathan (October 1997). "Adaptive Precision Floating-Point Arithmetic and Fast Robust Geometric Predicates". Discrete & Computational Geometry. 18 (3): 305–363. doi:10.1007/PL00009321.
  9. Manfred Tasche and Hansmartin Zeuner Handbook of Analytic-Computational Methods in Applied Mathematics Boca Raton, FL: CRC Press, 2000).
  10. Dalton, Barnaby; Wang, Amy; Blainey, Bob (16 February 2014). SIMDizing pairwise sums: a summation algorithm balancing accuracy with throughput. 2014 Workshop on Workshop on Programming Models for SIMD/Vector Processing - WPMVP ’14. pp. 65–70. doi:10.1145/2568058.2568070.
  11. Neumaier, A. (1974). "Rundungsfehleranalyse einiger Verfahren zur Summation endlicher Summen" [Rounding Error Analysis of Some Methods for Summing Finite Sums] (PDF). Zeitschrift für Angewandte Mathematik und Mechanik (in German). 54 (1): 39–51. Bibcode:1974ZaMM...54...39N. doi:10.1002/zamm.19740540106. Archived from the original (PDF) on 2015-09-21.
  12. Dmitruk, Beata; Stpiczyński, Przemysław (2023). "Parallel Vectorized Implementations of Compensated Summation Algorithms". Parallel Processing and Applied Mathematics. 13827: 63–74. doi:10.1007/978-3-031-30445-3_6.
  13. Blanchard, Pierre; Higham, Nicholas J.; Mary, Theo (January 2020). "A Class of Fast and Accurate Summation Algorithms". SIAM Journal on Scientific Computing. 42 (3): A1541–A1557. doi:10.1137/19M1257780.
  14. "fsum - Rust". docs.rs.
  15. "Draft ECMA-262 ECMAScript® 2027 Language Specification". July 17, 2026. 21.3.2.34 Math.sumPrecise [...] The value of sum can be computed without arbitrary-precision arithmetic by a variety of algorithms. One such is the "Grow-Expansion" algorithm given in Adaptive Precision Floating-Point Arithmetic and Fast Robust Geometric Predicates by Jonathan Richard Shewchuk. A more recent algorithm is given in "Fast exact summation using small and large superaccumulators".
  16. RFC: use pairwise summation for sum, cumsum, and cumprod, github.com/JuliaLang/julia pull request #4039 (August 2013).
[edit]