sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1402737 - in /sis/branches/JDK7/sis-utility/src: main/java/org/apache/sis/math/MathFunctions.java main/java/org/apache/sis/measure/AngleFormat.java test/java/org/apache/sis/math/MathFunctionsTest.java
Date Sat, 27 Oct 2012 03:45:05 GMT
Author: desruisseaux
Date: Sat Oct 27 03:45:05 2012
New Revision: 1402737

URL: http://svn.apache.org/viewvc?rev=1402737&view=rev
Log:
In fractionDigitsForDelta, the check for the 0.…95 should be optional.

Modified:
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/math/MathFunctions.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java
    sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/math/MathFunctionsTest.java

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/math/MathFunctions.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/math/MathFunctions.java?rev=1402737&r1=1402736&r2=1402737&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/math/MathFunctions.java (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/math/MathFunctions.java Sat
Oct 27 03:45:05 2012
@@ -216,14 +216,16 @@ public final class MathFunctions extends
 
     /**
      * Returns the number of fraction digits needed for formatting in base 10 numbers of
the given
-     * accuracy. For any value {@code accuracy}, this method returns a value <var>n</var>
such as
-     * the difference between adjacent numbers formatted in base 10 with <var>n</var>
fraction
-     * digits will always be equals or smaller than {@code accuracy}. Examples:
+     * accuracy. If the {@code strict} argument is {@code true}, then for any given {@code
accuracy}
+     * this method returns a value <var>n</var> such as the difference between
adjacent numbers
+     * formatted in base 10 with <var>n</var> fraction digits will always be
equals or smaller
+     * than {@code accuracy}. Examples:
      *
      * <ul>
-     *   <li>{@code fractionDigitsForDelta(0.001)} returns 3.</li>
-     *   <li>{@code fractionDigitsForDelta(0.009)} returns 3.</li>
-     *   <li>{@code fractionDigitsForDelta(0.010)} returns 2.</li>
+     *   <li>{@code fractionDigitsForDelta(0.001, true)} returns 3.</li>
+     *   <li>{@code fractionDigitsForDelta(0.009, true)} returns 3.</li>
+     *   <li>{@code fractionDigitsForDelta(0.010, true)} returns 2.</li>
+     *   <li>{@code fractionDigitsForDelta(0.099, true)} returns 3 (special case).</li>
      * </ul>
      *
      * <p>Special cases:</p>
@@ -232,10 +234,11 @@ public final class MathFunctions extends
      *       then this method returns 0.</li>
      *   <li>If {@code accuracy} is greater than 1, then this method returns
      *       the number of "unnecessary" trailing zeros as a negative number.
-     *       For example {@code fractionDigitsForDelta(100)} returns -2.</li>
+     *       For example {@code fractionDigitsForDelta(100, …)} returns -2.</li>
      *   <li>If the first non-zero digits of {@code accuracy} are equal or greater
than 95
-     *       (e.g. 0.00099), then this method increases the number of needed fraction digits
-     *       in order to prevent the rounded number to be collapsed into the next integer
value.
+     *       (e.g. 0.00099) and the {@code strict} argument is {@code true}, then this method
+     *       increases the number of needed fraction digits in order to prevent the rounded
+     *       number to be collapsed into the next integer value.
      *
      *       <blockquote><font size="-1"><b>Example:</b>
      *       If {@code accuracy} is 0.95, then a return value of 1 is not sufficient since
the
@@ -248,15 +251,18 @@ public final class MathFunctions extends
      *
      * <p>Invoking this method is equivalent to computing <code>(int)
      * -{@linkplain Math#floor(double) floor}({@linkplain Math#log10(double) log10}(accuracy))</code>
-     * except for the 0, {@code NaN}, infinities and 0.…95 special cases.</p>
+     * except for the 0, {@code NaN}, infinities and {@code 0.…95} special cases.</p>
      *
      * @param  accuracy The desired accuracy of numbers to format in base 10.
+     * @param  strict {@code true} for checking the {@code 0.…95} special case.
+     *         If {@code false}, then the difference between adjacent formatted numbers is
not
+     *         guaranteed to be smaller than {@code accuracy} in every cases.
      * @return Number of fraction digits needed for formatting numbers with the given accuracy.
      *         May be negative.
      *
      * @see java.text.NumberFormat#setMaximumFractionDigits(int)
      */
-    public static int fractionDigitsForDelta(double accuracy) {
+    public static int fractionDigitsForDelta(double accuracy, final boolean strict) {
         accuracy = Math.abs(accuracy);
         final boolean isFraction = (accuracy < 1);
         /*

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java?rev=1402737&r1=1402736&r2=1402737&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java Sat
Oct 27 03:45:05 2012
@@ -745,7 +745,7 @@ scan:   for (int i=0; i<length;) {
         if (maximumFractionDigits != minimumFractionDigits) {
             if      (secondsFieldWidth != 0) angle *= 3600;
             else if (minutesFieldWidth != 0) angle *=   60;
-            final int n = fractionDigitsForDelta(Math.ulp(angle)) - 1;
+            final int n = fractionDigitsForDelta(Math.ulp(angle), false) - 1;
             if (n < maximumFractionDigits) {
                 maximumFractionDigits = Math.max(minimumFractionDigits, n);
             }

Modified: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/math/MathFunctionsTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/math/MathFunctionsTest.java?rev=1402737&r1=1402736&r2=1402737&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/math/MathFunctionsTest.java
(original)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/math/MathFunctionsTest.java
Sat Oct 27 03:45:05 2012
@@ -88,47 +88,47 @@ public final strictfp class MathFunction
      */
     @Test
     public void testFractionDigitsForDelta() {
-        assertEquals(3, fractionDigitsForDelta(0.001));
-        assertEquals(3, fractionDigitsForDelta(0.009));
-        assertEquals(2, fractionDigitsForDelta(0.010));
-        assertEquals(2, fractionDigitsForDelta(0.015));
-        assertEquals(1, fractionDigitsForDelta(0.100));
-        assertEquals(1, fractionDigitsForDelta(0.125));
-        assertEquals(1, fractionDigitsForDelta(0.949));
-        assertEquals(2, fractionDigitsForDelta(0.994)); // Special case
-        assertEquals(3, fractionDigitsForDelta(0.999)); // Special case
-
-        assertEquals( 0, fractionDigitsForDelta(  1.0));
-        assertEquals( 0, fractionDigitsForDelta(  1.9));
-        assertEquals( 0, fractionDigitsForDelta(  9.1));
-        assertEquals(-1, fractionDigitsForDelta( 10.0));
-        assertEquals(-1, fractionDigitsForDelta( 19.9));
-        assertEquals(-1, fractionDigitsForDelta( 94.9));
-        assertEquals( 0, fractionDigitsForDelta( 99.0)); // Special case
-        assertEquals(-2, fractionDigitsForDelta(100.0));
-        assertEquals(-2, fractionDigitsForDelta(100.1));
-        assertEquals(-1, fractionDigitsForDelta(994.9)); // Special case
-        assertEquals(+1, fractionDigitsForDelta(999.9)); // Special case
-        assertEquals(-3, fractionDigitsForDelta(1000));
+        assertEquals(3, fractionDigitsForDelta(0.001, true));
+        assertEquals(3, fractionDigitsForDelta(0.009, true));
+        assertEquals(2, fractionDigitsForDelta(0.010, true));
+        assertEquals(2, fractionDigitsForDelta(0.015, true));
+        assertEquals(1, fractionDigitsForDelta(0.100, true));
+        assertEquals(1, fractionDigitsForDelta(0.125, true));
+        assertEquals(1, fractionDigitsForDelta(0.949, true));
+        assertEquals(2, fractionDigitsForDelta(0.994, true)); // Special case
+        assertEquals(3, fractionDigitsForDelta(0.999, true)); // Special case
+
+        assertEquals( 0, fractionDigitsForDelta(  1.0, true));
+        assertEquals( 0, fractionDigitsForDelta(  1.9, true));
+        assertEquals( 0, fractionDigitsForDelta(  9.1, true));
+        assertEquals(-1, fractionDigitsForDelta( 10.0, true));
+        assertEquals(-1, fractionDigitsForDelta( 19.9, true));
+        assertEquals(-1, fractionDigitsForDelta( 94.9, true));
+        assertEquals( 0, fractionDigitsForDelta( 99.0, true)); // Special case
+        assertEquals(-2, fractionDigitsForDelta(100.0, true));
+        assertEquals(-2, fractionDigitsForDelta(100.1, true));
+        assertEquals(-1, fractionDigitsForDelta(994.9, true)); // Special case
+        assertEquals(+1, fractionDigitsForDelta(999.9, true)); // Special case
+        assertEquals(-3, fractionDigitsForDelta(1000,  true));
 
         // Tests values out of the POW10 array range.
-        assertEquals(23,  fractionDigitsForDelta(1.0E-23));
-        assertEquals(23,  fractionDigitsForDelta(1.9E-23));
-        assertEquals(23,  fractionDigitsForDelta(9.1E-23));
-        assertEquals(24,  fractionDigitsForDelta(9.6E-23)); // Special case
-        assertEquals(300, fractionDigitsForDelta(1.1E-300));
-
-        assertEquals(-23,  fractionDigitsForDelta(1.0E+23));
-        assertEquals(-23,  fractionDigitsForDelta(1.9E+23));
-        assertEquals(-23,  fractionDigitsForDelta(9.1E+23));
-        assertEquals(-22,  fractionDigitsForDelta(9.6E+23)); // Special case
-        assertEquals(-300, fractionDigitsForDelta(1.1E+300));
+        assertEquals(23,  fractionDigitsForDelta(1.0E-23,  true));
+        assertEquals(23,  fractionDigitsForDelta(1.9E-23,  true));
+        assertEquals(23,  fractionDigitsForDelta(9.1E-23,  true));
+        assertEquals(24,  fractionDigitsForDelta(9.6E-23,  true)); // Special case
+        assertEquals(300, fractionDigitsForDelta(1.1E-300, true));
+
+        assertEquals(-23,  fractionDigitsForDelta(1.0E+23,  true));
+        assertEquals(-23,  fractionDigitsForDelta(1.9E+23,  true));
+        assertEquals(-23,  fractionDigitsForDelta(9.1E+23,  true));
+        assertEquals(-22,  fractionDigitsForDelta(9.6E+23,  true)); // Special case
+        assertEquals(-300, fractionDigitsForDelta(1.1E+300, true));
 
         // Special cases.
-        assertEquals(0,  fractionDigitsForDelta(0));
-        assertEquals(0,  fractionDigitsForDelta(Double.NaN));
-        assertEquals(0,  fractionDigitsForDelta(Double.POSITIVE_INFINITY));
-        assertEquals(0,  fractionDigitsForDelta(Double.NEGATIVE_INFINITY));
+        assertEquals(0,  fractionDigitsForDelta(0, true));
+        assertEquals(0,  fractionDigitsForDelta(Double.NaN, true));
+        assertEquals(0,  fractionDigitsForDelta(Double.POSITIVE_INFINITY, true));
+        assertEquals(0,  fractionDigitsForDelta(Double.NEGATIVE_INFINITY, true));
     }
 
     /**



Mime
View raw message