<p>One possible approach is to compute the sum using a <em>superaccumulator</em>: this is an algorithm for computing exact sums of floating point numbers. Although these ideas have been around for a while, the term is a relatively new one.</p>

<p>In some sense, you can think of it as an extension of Kahan summation, where the sequential sum is stored as an array of values, rather than just a pair. The main challenge then becomes figuring out how to allocate the precision amongst the various values.</p>

<p>Some relevant papers and code:</p>

<ul>
<li><p>Y. K. Zhu and W. B. Hayes. "Algorithm 908: Online Exact Summation of Floating-Point Streams". <em>ACM Transactions on Mathematical Software</em> (ACM TOMS), 37(3):37:1-37:13, September 2010. doi: <a href="http://dx.doi.org/10.1145/1824801.1824815">10.1145/1824801.1824815</a></p>

<ul>
<li>Unfortunately the paper and code are behind a paywall, but this appears to be <a href="https://github.com/aseldawy/sumn/tree/master/908">the C++ code</a>.</li>
</ul></li>
<li><p>R. M. Neal, "Fast Exact Summation using Small and Large Superaccumulators". 2015. arXiv: <a href="https://arxiv.org/abs/1505.05571">1505.05571</a></p>

<ul>
<li><a href="https://arxiv.org/src/1505.05571v1">C code available</a></li>
</ul></li>
<li><p>M. T. Goodrich, A. Eldawy "Parallel Algorithms for Summing Floating-Point Numbers". 2016. arXiv: <a href="https://arxiv.org/abs/1605.05436">1605.05436</a></p>

<ul>
<li><a href="https://github.com/aseldawy/sumn">Java code of this and the above</a></li>
</ul></li>
</ul>
comparing two sums of floating point values in C or C++
<p>Assume You're given two sets of floating point variables implemented according to IEEE754, meant to be treated as exact values calculated according to formulae present in standard. All legal values are possible. The amount of variables in set may be any natural number.</p>

<p>What would be a good way to compare exact, in mathematical sense, sums of values represented by said variables. Due to domain's nature, the problem can easily be represented as comparing a single sum to zero. You can disregard the possibility of presence of NaNs or Infinities, as it is irrelevant to core problem. (Those values can be checked for easily and independently, and acted upon in a manner suiting particular application of this problem.)</p>

<p>A naive approach would be to simply sum and compare, or sum values of one set and subtract values of another.</p>

<pre><code>    bool compare(const std::vector&lt;float&gt;&amp; lhs, const std::vector&lt;float&gt;&amp; rhs)
    {
        float lSum = 0.0f;
        for (auto value : lhs)
        {
            lSum += value;
        }
        float rSum = 0.0f;
        for (auto value : rhs)
        {
            rSum += value;
        }

        return lSum &lt; rSum;
    }
</code></pre>

<p>Quite obviously there are problems with naive approach, as mentioned in various other questions regarding floating point arithmetic. Most of the problems are related to two difficulties:</p>

<ul>
<li>result of addition of floating point values differs depending on order</li>
<li><p>certain orders of addition of certain sets of values may result in intermediate overflow (intermediate result of calculations goes beyond range supported by available data type)</p>

<pre><code>float small = strtof("0x1.0p-126", NULL);
float big = strtof("0x1.8p126", NULL);

std::cout &lt;&lt; std::hexfloat &lt;&lt; small + big - big &lt;&lt; std::endl;
std::cout &lt;&lt; std::hexfloat &lt;&lt; (big-2*small) + (big-small) + big - (big+small) - (big+2*small) &lt;&lt; std::endl;
</code></pre>

<p>This code will result in <code>0</code> and <code>inf</code>; this illustrates how ordering affects the result. Hopefully, also that the problem of ordering is non-trivial.</p>

<pre><code>float prev;
float curr = 0.0f;

do
{
    prev = curr;
    curr += strtof("0x1.0p-126", NULL);
} while (prev != curr);

std::cout &lt;&lt; std::hexfloat &lt;&lt; curr &lt;&lt; std::endl;
</code></pre></li>
</ul>

<p>This code, given sufficient time to actually finish computing, would result in <code>0x1.000000p-102</code>, not, as could be naively expected, <code>0x1.fffffep127</code> (Change of curr initialization to `strtof("0x1.fff000p-103") would be advised to actually observe this.); this illustrates how proportion between intermediate results of addition and particular addends affects the result.</p>

<p>A lot has been said about obtaining best precision, eg. <a href="https://stackoverflow.com/questions/6699066/in-which-order-should-floats-be-added-to-get-the-most-precise-result">this question</a>.</p>

<p>The problem at hand differs in that we do not want to maximize precision, but we have a well-defined function that needs to be implemented exactly.</p>

<p>While for some the idea that it may be useful exercise seems controversial at best, consider the following scenario: comparison between those value sets could be a cornerstone of other operations performed on entire datasets independently in various environments. Synchronized, flawless operation of some systems may depend on this comparison being well defined and deterministically implemented, irregardless of addends order and particular architecture implementing IEEE754 or not. </p>

<p>This, or just curiosity.</p>

<p>In the discussion, <a href="https://en.wikipedia.org/wiki/Kahan_summation_algorithm" rel="nofollow noreferrer">Kahan summation algorithm</a> has been mentioned as relevant. However this algorithm is a reasonable attempt at minimizing error. It neither guarantees correct sign of result, nor is independent of the order of operations (to at least guarantee consistent, if wrong, result, for permutations of sets).</p>

<p>One of the most obvious solutions would be to employ/implement fixed point arithmetic using sufficient amount of bits to represent every possible operand value exactly and keep exact intermediate result.</p>

<p>Perhaps however this can be done using only floating point arithmetic in a manner that guarantees correct sign of result. If so, the problem of overflow (as illustrated in one of the examples above) needs to be addressed in solution, as this question has particular technical aspect.</p>

<p>(What follows is original question.)</p>

<p><em>I have two sets of multiple floating point (float or double) values. I want to provide a perfect answer to the question, which set has larger sum. Because of artifacts in floating point arithmetic, in some corner cases the result of naive approach may be wrong, depending on order of operations. Not to mention simple sum can result in overflow.
I can't provide any effort on my side, because all I have is vague ideas, all of them complicated and not convincing.</em></p>