You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Anirudh Joshi (Jira)" <ji...@apache.org> on 2023/07/21 21:53:00 UTC

[jira] [Updated] (NUMBERS-200) Wrong output when adding two Sum instances containing special values

     [ https://issues.apache.org/jira/browse/NUMBERS-200?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Anirudh Joshi updated NUMBERS-200:
----------------------------------
    Fix Version/s: 1.1
                       (was: 1.2)

> Wrong output when adding two Sum instances containing special values
> --------------------------------------------------------------------
>
>                 Key: NUMBERS-200
>                 URL: https://issues.apache.org/jira/browse/NUMBERS-200
>             Project: Commons Numbers
>          Issue Type: Bug
>          Components: core
>            Reporter: Anirudh Joshi
>            Priority: Blocker
>             Fix For: 1.1
>
>
> Here's some setup code before describing the bug :
> Two sum instances are created and assigned Double.POSITIVE_INFINITY as follows:
> {code:java}
> Sum sum1 = Sum.create();
> Sum sum2 = Sum.create();
> sum1.add(Double.POSITIVE_INFINITY);
> sum2.add(Double.POSITIVE_INFINITY);{code}
> The `sum` and `comp` fields of both `sum` and `sum2` hold the values `Double.POSITIVE_INFINITY` and `Double.NaN` repectively. 
> sum2 is added to sum1 as:
> {code:java}
> sum1.add(sum2);
> {code}
> The `Sum add(Sum other)` returns the value returned by `Sum add(double s, double c)` method.
> In the Sum add(double s, double c) method, there's a bug in the return statement : 
> {code:java}
> return add(s).add(c); {code}
> When add(c) is executed, c (i.e. sum2.c) which has a value Double.NaN, is causing the sum field of sum1 to be Double.NaN. This is due to :
> {code:java}
> Sum add(final double t) { // t (= c)
>     final double newSum = sum + t; // Inf + Nan = NaN
>     comp += ExtendedPrecision.twoSumLow(sum, t, newSum); // NaN
>     sum = newSum; // NaN 
>     return this;
> } {code}
> Hence, the chaining of add operations should be removed.
> The fix would be in the `Sum add(double s, double c)` method as :
> {code:java}
> Sum add(double s, double c) {
>     add(s);
>     if (!(Double.isNan(comp) && Double.isInfinite(sum))) {
>         add(c);
>     }
>     return this;
> }{code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)