[PATCH] Fixed: avoiding NaN exception in BigDecimal rounding

Wald Commits scm-commit at wald.intevation.org
Tue Aug 7 14:07:06 CEST 2018


# HG changeset patch
# User mschaefer
# Date 1533643609 -7200
# Node ID 8ae7137b67d7b6efa2b51b54966a5584d081dbe9
# Parent  a9e6b0a75bde1e7ffa681ad329f5bec02fc9895a
Fixed: avoiding NaN exception in BigDecimal rounding

diff -r a9e6b0a75bde -r 8ae7137b67d7 artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepth/FlowDepthUtils.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepth/FlowDepthUtils.java	Tue Aug 07 12:43:53 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepth/FlowDepthUtils.java	Tue Aug 07 14:06:49 2018 +0200
@@ -10,6 +10,7 @@
 package org.dive4elements.river.artifacts.sinfo.flowdepth;
 
 import org.dive4elements.river.artifacts.model.Calculation;
+import org.dive4elements.river.utils.Formatter;
 
 /**
  * @author Gernot Belger
@@ -55,4 +56,13 @@
         /* >= 1998 */
         return 3;
     }
+
+    /**
+     * Calculates a flow depth, rounded to the active scale
+     */
+    public static double calcFlowDepth(final double wst, final double bedHeight) {
+        if (Double.isNaN(wst) || Double.isInfinite(wst) || Double.isNaN(bedHeight) || Double.isInfinite(bedHeight))
+            return Math.max(wst - bedHeight, 0.0);
+        return Math.max(Formatter.roundFlowDepth(wst).subtract(Formatter.roundFlowDepth(bedHeight)).doubleValue(), 0.0);
+    }
 }
\ No newline at end of file
diff -r a9e6b0a75bde -r 8ae7137b67d7 artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepthminmax/FlowDepthMinMaxCalculation.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepthminmax/FlowDepthMinMaxCalculation.java	Tue Aug 07 12:43:53 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepthminmax/FlowDepthMinMaxCalculation.java	Tue Aug 07 14:06:49 2018 +0200
@@ -35,7 +35,6 @@
 import org.dive4elements.river.artifacts.states.WaterlevelData;
 import org.dive4elements.river.artifacts.states.WaterlevelFetcher;
 import org.dive4elements.river.model.River;
-import org.dive4elements.river.utils.Formatter;
 
 /**
  * @author Gernot Belger
@@ -135,8 +134,8 @@
                 final double maxBedHeightValue = bedHeight.getMaxBedHeight(station);
                 final double meanBedHeight = bedHeight.getMeanBedHeight(station);
 
-                final double minFlowDepth = Math.max(Formatter.roundFlowDepth(wst).subtract(Formatter.roundFlowDepth(maxBedHeightValue)).doubleValue(), 0.0);
-                final double maxFlowDepth = Math.max(Formatter.roundFlowDepth(wst).subtract(Formatter.roundFlowDepth(minBedHeightValue)).doubleValue(), 0.0);
+                final double minFlowDepth = FlowDepthUtils.calcFlowDepth(wst, maxBedHeightValue);
+                final double maxFlowDepth = FlowDepthUtils.calcFlowDepth(wst, minBedHeightValue);
 
                 // REMARK: access the location once only during calculation
                 final String location = riverInfoProvider.getLocation(station);
diff -r a9e6b0a75bde -r 8ae7137b67d7 artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixLineCalculator.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixLineCalculator.java	Tue Aug 07 12:43:53 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixLineCalculator.java	Tue Aug 07 14:06:49 2018 +0200
@@ -183,6 +183,8 @@
      * Calculates the salix value
      */
     private double calcSalix(final double mhw, final double mw) {
+        if (Double.isNaN(mw) || Double.isInfinite(mw) || Double.isNaN(mhw) || Double.isInfinite(mhw))
+            return mw; // preserving NaN or Infinity
         return Formatter.roundW(mhw).subtract(SALIX_DISTANCE).subtract(Formatter.roundW(mw)).doubleValue();
     }
 
@@ -190,6 +192,8 @@
      * Calculates the inverse MW-MNW difference
      */
     private double calcMwmnw(final double mw, final double mnw) {
+        if (Double.isNaN(mw) || Double.isInfinite(mw) || Double.isNaN(mnw) || Double.isInfinite(mnw))
+            return mnw - mw; // preserving NaN or Inifinity
         return Formatter.roundW(mnw).subtract(Formatter.roundW(mw)).doubleValue();
     }
 
diff -r a9e6b0a75bde -r 8ae7137b67d7 artifacts/src/main/java/org/dive4elements/river/utils/Formatter.java
--- a/artifacts/src/main/java/org/dive4elements/river/utils/Formatter.java	Tue Aug 07 12:43:53 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/utils/Formatter.java	Tue Aug 07 14:06:49 2018 +0200
@@ -385,6 +385,10 @@
         return Formatter.getFormatter(context, FLOWDEPTH_MAX_DIGITS, FLOWDEPTH_MAX_DIGITS);
     }
 
+    /**
+     * Decimal half even rounding of a flow depth value
+     * (throws an exception for NaN or Infinity)
+     */
     public static BigDecimal roundFlowDepth(final double value) {
         return BigDecimal.valueOf(value).setScale(FLOWDEPTH_MAX_DIGITS, RoundingMode.HALF_EVEN);
     }
@@ -393,6 +397,10 @@
         return Formatter.getFormatter(context, 2, 2);
     }
 
+    /**
+     * Decimal half even rounding of a W value
+     * (throws an exception for NaN or Infinity)
+     */
     public static BigDecimal roundW(final double value) {
         return BigDecimal.valueOf(value).setScale(WATERLEVEL_W_MAX_DIGITS, RoundingMode.HALF_EVEN);
     }


More information about the Dive4Elements-commits mailing list