sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1513773 - in /sis/branches/0.3: ./ core/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java core/sis-utility/src/test/java/org/apache/sis/measure/AngleFormatTest.java
Date Wed, 14 Aug 2013 09:28:24 GMT
Author: desruisseaux
Date: Wed Aug 14 09:28:23 2013
New Revision: 1513773

URL: http://svn.apache.org/r1513773
Log:
Merged the more robust fix for SIS-120.

Modified:
    sis/branches/0.3/   (props changed)
    sis/branches/0.3/core/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java
    sis/branches/0.3/core/sis-utility/src/test/java/org/apache/sis/measure/AngleFormatTest.java

Propchange: sis/branches/0.3/
------------------------------------------------------------------------------
  Merged /sis/branches/JDK7:r1513771

Modified: sis/branches/0.3/core/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/0.3/core/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java?rev=1513773&r1=1513772&r2=1513773&view=diff
==============================================================================
--- sis/branches/0.3/core/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java
[UTF-8] (original)
+++ sis/branches/0.3/core/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java
[UTF-8] Wed Aug 14 09:28:23 2013
@@ -809,40 +809,33 @@ scan:   for (int i=0; i<length;) {
             }
             return toAppendTo;
         }
-        double degrees = angle;
         /*
          * Computes the numerical values of minutes and seconds fields.
          * If those fiels are not written, then store NaN.
          */
+        double degrees = angle;
         double minutes = NaN;
         double seconds = NaN;
         if (minutesFieldWidth != 0 && !isNaN(angle)) {
             minutes = abs(degrees - (degrees = truncate(degrees))) * 60;
+            final double p = pow10(fractionFieldWidth);
             if (secondsFieldWidth != 0) {
                 seconds = (minutes - (minutes = truncate(minutes))) * 60;
-                /*
-                 * Correction for rounding errors.
-                 */
-                final double puissance = pow10(fractionFieldWidth);
-                seconds = rint(seconds * puissance) / puissance;
-                final double correction = truncate(seconds / 60);
-                seconds -= correction * 60;
-                minutes += correction;
+                seconds = rint(seconds * p) / p; // Correction for rounding errors.
+                if (seconds >= 60) { // We do not expect > 60 (only == 60), but let
be safe.
+                    seconds = 0;
+                    minutes++;
+                }
             } else {
-                final double puissance = pow10(fractionFieldWidth);
-                minutes = rint(minutes * puissance) / puissance;
-            }
-            final double correction = truncate(minutes / 60);
-            if (correction != 0) {
-                /*
-                 * We really need to NOT perform the addition if correction == 0, even if
its look like
-                 * mathematically equivalent, because we want to preserve the sign of 'degrees'
in case
-                 * of negative zero. This is because in Java, -0.0 + 0 == +0.0 while we really
need to
-                 * keep the negative sign in -0.0.
-                 */
-                minutes -= correction * 60;
-                degrees += correction;
+                minutes = rint(minutes * p) / p; // Correction for rounding errors.
             }
+            if (minutes >= 60) { // We do not expect > 60 (only == 60), but let be
safe.
+                minutes = 0;
+                degrees += Math.signum(angle);
+            }
+            // Note: a previous version was doing a unconditional addition to the 'degrees'
variable,
+            // in the form 'degrees += correction'. However -0.0 + 0 == +0.0, while we really
need to
+            // preserve the sign of negative zero. See [SIS-120].
         }
         /*
          * At this point the 'degrees', 'minutes' and 'seconds' variables contain the final
values

Modified: sis/branches/0.3/core/sis-utility/src/test/java/org/apache/sis/measure/AngleFormatTest.java
URL: http://svn.apache.org/viewvc/sis/branches/0.3/core/sis-utility/src/test/java/org/apache/sis/measure/AngleFormatTest.java?rev=1513773&r1=1513772&r2=1513773&view=diff
==============================================================================
--- sis/branches/0.3/core/sis-utility/src/test/java/org/apache/sis/measure/AngleFormatTest.java
[UTF-8] (original)
+++ sis/branches/0.3/core/sis-utility/src/test/java/org/apache/sis/measure/AngleFormatTest.java
[UTF-8] Wed Aug 14 09:28:23 2013
@@ -135,6 +135,18 @@ public final strictfp class AngleFormatT
     }
 
     /**
+     * Tests values that have to be rounded, especially the values near zero.
+     */
+    @Test
+    @DependsOnMethod("testDegreeMinutesSeconds")
+    public void testRounding() {
+        final AngleFormat f = new AngleFormat("DD°MM′SS.sss″", Locale.CANADA);
+        assertEquals( "01°00′00.000″", f.format(new Angle(+(59 + (59.9999 / 60)) / 60)));
+        assertEquals("-01°00′00.000″", f.format(new Angle(-(59 + (59.9999 / 60)) / 60)));
+        assertEquals("-00°59′59.999″", f.format(new Angle(-(59 + (59.9988 / 60)) / 60)));
+    }
+
+    /**
      * Tests with optional digits.
      */
     @Test



Mime
View raw message