changeset 8931:b10f8415798c

Trying to avoid symptoms of == double comparison
author gernotbelger
date Tue, 06 Mar 2018 17:04:17 +0100
parents f972e1da4a63
children 8731c3dabb56
files artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhcalculation/DischargeValuesFinder.java
diffstat 1 files changed, 20 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhcalculation/DischargeValuesFinder.java	Tue Mar 06 17:02:45 2018 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhcalculation/DischargeValuesFinder.java	Tue Mar 06 17:04:17 2018 +0100
@@ -17,12 +17,17 @@
 import org.dive4elements.river.artifacts.model.WQKms;
 import org.dive4elements.river.utils.DoubleUtil;
 
+import gnu.trove.TDoubleDoubleHashMap;
+
 /**
  * @author Gernot Belger
  */
 public final class DischargeValuesFinder {
 
     private final UnivariateRealFunction qInterpolator;
+
+    private final TDoubleDoubleHashMap exactValues;
+
     private final QKms qKms;
 
     /**
@@ -42,6 +47,14 @@
     public DischargeValuesFinder(final QKms qKms) {
         this.qKms = qKms;
         this.qInterpolator = qKms == null ? null : DoubleUtil.getLinearInterpolator(qKms.allKms(), qKms.allQs());
+
+        this.exactValues = new TDoubleDoubleHashMap(qKms.size());
+
+        for (int i = 0; i < qKms.size(); i++) {
+            final double station = qKms.getKm(i);
+            final double discharge = qKms.getQ(i);
+            this.exactValues.put(station, discharge);
+        }
     }
 
     /**
@@ -56,6 +69,13 @@
     }
 
     public double getDischarge(final double station) throws FunctionEvaluationException {
+
+        // IMPORTANT: we first try to retreive the exact value if it is present, to avoid rounding changes due to interpolation.
+        // This is important because in the WaterlevelExporter code, these values are double-compared (with '==' ...) in order
+        // to find the corresponding main-value.
+        if (this.exactValues.contains(station))
+            return this.exactValues.get(station);
+
         return this.qInterpolator.value(station);
     }
 }
\ No newline at end of file

http://dive4elements.wald.intevation.org