changeset 9375:a0a2e68a1e11

Fixed: cm rounding in S-Info flow depth/tkh and U-Info salix via BigDecimal, half_even mode
author mschaefer
date Fri, 03 Aug 2018 17:00:45 +0200
parents 4d61a9642046
children f318359b81a2
files artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepthminmax/FlowDepthMinMaxCalculation.java artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhcalculation/TkhCalculator.java artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixLineCalculator.java artifacts/src/main/java/org/dive4elements/river/utils/Formatter.java
diffstat 4 files changed, 18 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepthminmax/FlowDepthMinMaxCalculation.java	Thu Aug 02 20:14:26 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepthminmax/FlowDepthMinMaxCalculation.java	Fri Aug 03 17:00:45 2018 +0200
@@ -135,8 +135,8 @@
                 final double maxBedHeightValue = bedHeight.getMaxBedHeight(station);
                 final double meanBedHeight = bedHeight.getMeanBedHeight(station);
 
-                final double minFlowDepth = Math.max(Formatter.roundFlowDepth(wst) - Formatter.roundFlowDepth(maxBedHeightValue), 0.0);
-                final double maxFlowDepth = Math.max(Formatter.roundFlowDepth(wst) - Formatter.roundFlowDepth(minBedHeightValue), 0.0);
+                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);
 
                 // REMARK: access the location once only during calculation
                 final String location = riverInfoProvider.getLocation(station);
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhcalculation/TkhCalculator.java	Thu Aug 02 20:14:26 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhcalculation/TkhCalculator.java	Fri Aug 03 17:00:45 2018 +0200
@@ -135,7 +135,7 @@
         if (Double.isNaN(meanBedHeight))
             return TkhCalculateState.NO_BED_HEIGHT;
 
-        final double flowDepth = Formatter.roundFlowDepth(wst) - Formatter.roundFlowDepth(meanBedHeight);
+        final double flowDepth = Formatter.roundFlowDepth(wst).subtract(Formatter.roundFlowDepth(meanBedHeight)).doubleValue();
         row.putValue(SInfoResultType.flowdepth, flowDepth);
 
         final double discharge = this.dischargeProvider.getDischarge(km);
@@ -175,14 +175,14 @@
 
         switch (kind) {
         case starr:
-            row.putValue(SInfoResultType.tkhup, (double) Math.round(tkh));
+            row.putValue(SInfoResultType.tkhup, Math.rint(tkh));
             row.putValue(SInfoResultType.tkhdown, 0.0);
             break;
 
         case mobil:
         default:
-            row.putValue(SInfoResultType.tkhup, (double) Math.round(tkh / 2));
-            row.putValue(SInfoResultType.tkhdown, (double) -(Math.round(tkh / 2)));
+            row.putValue(SInfoResultType.tkhup, Math.rint(tkh / 2));
+            row.putValue(SInfoResultType.tkhdown, -(Math.rint(tkh / 2)));
             break;
         }
 
@@ -230,11 +230,11 @@
 
         switch (tkhKind) {
         case starr:
-            return flowDepth - Math.round(tkhValue) / 100.0;
+            return flowDepth - Math.rint(tkhValue) / 100.0;
 
         case mobil:
         default:
-            return flowDepth - Math.round(tkhValue / 2) / 100.0;
+            return flowDepth - Math.rint(tkhValue / 2) / 100.0;
         }
     }
 }
\ No newline at end of file
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixLineCalculator.java	Thu Aug 02 20:14:26 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixLineCalculator.java	Fri Aug 03 17:00:45 2018 +0200
@@ -9,6 +9,7 @@
  */
 package org.dive4elements.river.artifacts.uinfo.salix;
 
+import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -42,6 +43,7 @@
  */
 final class SalixLineCalculator {
 
+    private static final BigDecimal SALIX_DISTANCE = new BigDecimal("2.31");
     private final List<ResultRow> rows = new ArrayList<>();
 
     private final RiverInfoProvider riverInfoProvider;
@@ -181,14 +183,14 @@
      * Calculates the salix value
      */
     private double calcSalix(final double mhw, final double mw) {
-        return Formatter.roundW(mhw) - 2.31 - Formatter.roundW(mw);
+        return Formatter.roundW(mhw).subtract(SALIX_DISTANCE).subtract(Formatter.roundW(mw)).doubleValue();
     }
 
     /**
      * Calculates the inverse MW-MNW difference
      */
     private double calcMwmnw(final double mw, final double mnw) {
-        return Formatter.roundW(mnw) - Formatter.roundW(mw);
+        return Formatter.roundW(mnw).subtract(Formatter.roundW(mw)).doubleValue();
     }
 
     /**
--- a/artifacts/src/main/java/org/dive4elements/river/utils/Formatter.java	Thu Aug 02 20:14:26 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/utils/Formatter.java	Fri Aug 03 17:00:45 2018 +0200
@@ -8,6 +8,8 @@
 
 package org.dive4elements.river.utils;
 
+import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.text.DateFormat;
 import java.text.DecimalFormat;
 import java.text.NumberFormat;
@@ -96,11 +98,9 @@
     // OTHER
     public static final int CSV_DIAGRAM_DATA_MAX_DIGITS = 3;
     public static final int CSV_DIAGRAM_DATA_MIN_DIGITS = 3;
-    private static final double W_ROUND_MULT = 100.0;
 
     // S-INFO
     public static final int FLOWDEPTH_MAX_DIGITS = 2;
-    private static final double FLOWDEPTH_ROUND_MULT = 100.0;
 
     /**
      * Creates a localized NumberFormatter with given range of decimal digits.
@@ -385,20 +385,16 @@
         return Formatter.getFormatter(context, FLOWDEPTH_MAX_DIGITS, FLOWDEPTH_MAX_DIGITS);
     }
 
-    public static double roundFlowDepth(final double value) {
-        if (Double.isNaN(value))
-            return Double.NaN;
-        return Math.round(value * FLOWDEPTH_ROUND_MULT) / FLOWDEPTH_ROUND_MULT;
+    public static BigDecimal roundFlowDepth(final double value) {
+        return BigDecimal.valueOf(value).setScale(FLOWDEPTH_MAX_DIGITS, RoundingMode.HALF_EVEN);
     }
 
     public static NumberFormat getW(final CallContext context) {
         return Formatter.getFormatter(context, 2, 2);
     }
 
-    public static double roundW(final double value) {
-        if (Double.isNaN(value))
-            return Double.NaN;
-        return Math.round(value * W_ROUND_MULT) / W_ROUND_MULT;
+    public static BigDecimal roundW(final double value) {
+        return BigDecimal.valueOf(value).setScale(WATERLEVEL_W_MAX_DIGITS, RoundingMode.HALF_EVEN);
     }
 
     /**

http://dive4elements.wald.intevation.org