Summation function to minimize rounding issues

In summary, the conversation discusses a C++ class called "NUM" that is used for summation of doubles with minimal rounding errors. It uses an array of 2048 doubles indexed by exponent and has functions for clearing the array, adding a number, and returning the sum. The conversation also includes a link to a zip file with source code for an example test. Additionally, the conversation mentions a CPU by a company called Futjitsu that natively supports variable floating point precision with correct rounding. However, the speaker expresses caution in using this technology for production code without extensive testing.
  • #1
rcgldr
Homework Helper
8,855
632
This is a C++ class to be used for summation of doubles (floating point). It uses an array of 2048 doubles indexed by exponent to minimize rounding errors by only adding numbers that have the same exponent.

NUM::NUM - array is cleared out when an instance of NUM is created
NUM::clear() - clears out the array
NUM::addnum() - add a number to the array
NUM::getsum() - returns the current sum of the array

link to zip of source file with example test code that adds 1./5. 4,294,967,295 (2^32-1) times and displays a sum using normal addition and using the SUM class.

http://rcgldr.net/misc/sum.zip

This is the key part of the SUM class, the addnum() function:

Code:
void SUM::addnum(double d)      // add a number into the array
{
size_t i;

    while(1){
//      i = exponent of d
        i = ((size_t)((*(unsigned long long *)&d)>>52))&0x7ff;
        if(i == 0x7ff){ // max exponent, could be overflow
            asum[i] += d;
            return;
        }
        if(this->asum[i] == 0){ // if empty slot store d
            asum[i] = d;
            return;
        }
        d += asum[i];           // else add slot to d, clear slot
        asum[i] = 0.;           // and continue until empty slot
    }
}
 
  • Like
Likes jim mcnamara
Technology news on Phys.org
  • #2
https://www.extremetech.com/computing/272558-japan-tests-silicon-for-exascale-computing-in-2021Futjitsu has a line of CPU's (One for historical SPARC: SPARC64 VIIIfx ), the newest ones are mentioned in the article above. CPU natively supports a variable floating point precision - with correct rounding. It would be interesting to read the white paper behind this effort.

While what you provide is very useful, I would never put it to work with production code without really extensive testing. Which means, in practice, I could not deploy it. Floating point is the bane of financial calculations, as you obviously know.
 

1. What is the summation function used for?

The summation function is used to add up a series of numbers. It is often used in mathematical and scientific calculations.

2. Why is the summation function important for minimizing rounding issues?

The summation function helps to reduce rounding errors that can occur when adding multiple numbers together. It does this by keeping track of the accumulated sum, rather than rounding at each step of the calculation.

3. How does the summation function work?

The summation function works by taking in a series of numbers, adding them together, and then returning the total sum. It can be represented mathematically as Σ (sigma) and is often used in conjunction with a mathematical expression to indicate a series of terms to be summed.

4. Are there any limitations to using the summation function to minimize rounding issues?

While the summation function is helpful in reducing rounding errors, it is not a foolproof method. It may still encounter issues if there are a large number of terms or if the numbers being added are extremely small or large.

5. Can the summation function be used for other purposes besides minimizing rounding issues?

Yes, the summation function has many other applications in mathematics and science. It can be used to calculate areas under curves, approximate integrals, and even represent infinite series. It is a versatile tool in a scientist's arsenal.

Similar threads

  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
735
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
2
Views
11K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
12
Views
3K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
15
Views
3K
  • Programming and Computer Science
Replies
5
Views
2K
Back
Top