sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1513771 - in /sis/branches/JDK7/core/sis-utility/src: main/java/org/apache/sis/measure/AngleFormat.java test/java/org/apache/sis/measure/AngleFormatTest.java
Date Wed, 14 Aug 2013 09:20:44 GMT
Author: desruisseaux
Date: Wed Aug 14 09:20:43 2013
New Revision: 1513771

URL: http://svn.apache.org/r1513771
Log:
More robust correction to SIS-120.

Modified:
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java
    sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/measure/AngleFormatTest.java

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java?rev=1513771&r1=1513770&r2=1513771&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java
[UTF-8] Wed Aug 14 09:20:43 2013
@@ -915,31 +915,24 @@ public class AngleFormat extends Format 
         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 p = pow10(fractionFieldWidth);
-                seconds = rint(seconds * p) / p;
-                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 p = pow10(fractionFieldWidth);
-                minutes = rint(minutes * p) / p;
-            }
-            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.copySign(1, 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].
         }
         /*
          * Avoid formatting values like 12.01°N as 12°36″N because of the risk of confusion.

Modified: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/measure/AngleFormatTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/measure/AngleFormatTest.java?rev=1513771&r1=1513770&r2=1513771&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/measure/AngleFormatTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/measure/AngleFormatTest.java
[UTF-8] Wed Aug 14 09:20:43 2013
@@ -154,6 +154,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 minutes and seconds fields.
      */
     @Test



Mime
View raw message