sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 01/03: Minor accuracy improvement.
Date Thu, 07 Feb 2019 15:56:42 GMT
This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit 026ddd00686fe76d6e38462123d3422633408e55
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Thu Feb 7 15:03:14 2019 +0100

    Minor accuracy improvement.
---
 .../main/java/org/apache/sis/math/Statistics.java  | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/core/sis-utility/src/main/java/org/apache/sis/math/Statistics.java b/core/sis-utility/src/main/java/org/apache/sis/math/Statistics.java
index 2bde844..7e10ec3 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/math/Statistics.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/math/Statistics.java
@@ -27,6 +27,7 @@ import org.apache.sis.util.ArgumentChecks;
 import static java.lang.Math.*;
 import static java.lang.Double.NaN;
 import static java.lang.Double.isNaN;
+import static java.lang.Double.isFinite;
 import static java.lang.Double.doubleToLongBits;
 
 
@@ -254,15 +255,22 @@ public class Statistics implements DoubleConsumer, LongConsumer, Cloneable,
Seri
      */
     @Override
     public void accept(final double sample) {
-        if (isNaN(sample)) {
+        if (isFinite(sample)) {
+            real(sample);
+        } else if (isNaN(sample)) {
             countNaN++;
         } else {
             real(sample);
+            sum = sample;                           // Replace NaN by the infinite value.
+            squareSum = Double.POSITIVE_INFINITY;
+            squareLowBits = lowBits = 0;
         }
     }
 
     /**
      * Implementation of {@link #accept(double)} for real (non-NaN) numbers.
+     *
+     * @see org.apache.sis.internal.util.DoubleDouble#addKahan(double)
      */
     private void real(double sample) {
         /*
@@ -296,8 +304,14 @@ public class Statistics implements DoubleConsumer, LongConsumer, Cloneable,
Seri
      * @see #combine(Statistics)
      */
     @Override
-    public void accept(final long sample) {
-        real(sample);
+    public void accept(long sample) {
+        double y = sample;
+        real(y);
+        sample -= (long) y;
+        if (sample != 0) {
+            y = sample + lowBits;
+            lowBits = y + (sum - (sum += y));
+        }
     }
 
     /**
@@ -393,7 +407,7 @@ public class Statistics implements DoubleConsumer, LongConsumer, Cloneable,
Seri
     }
 
     /**
-     * Equivalents to <code>{@link #maximum() maximum} - {@link #minimum() minimum}</code>.
+     * Equivalents to {@linkplain #maximum() maximum} - {@linkplain #minimum() minimum}.
      * If no samples were added, then returns {@link Double#NaN NaN}.
      *
      * @return the span of sample values, or NaN if none.


Mime
View raw message