commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1185841 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/exception/util/ main/java/org/apache/commons/math/util/ main/resources/META-INF/localization/ test/java/org/apache/commons/math/util/
Date Tue, 18 Oct 2011 20:30:43 GMT
Author: erans
Date: Tue Oct 18 20:30:42 2011
New Revision: 1185841

URL: http://svn.apache.org/viewvc?rev=1185841&view=rev
Log:
MATH-690
New "copySign" method.

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java
    commons/proper/math/trunk/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java?rev=1185841&r1=1185840&r2=1185841&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java
Tue Oct 18 20:30:42 2011
@@ -272,6 +272,7 @@ public enum LocalizedFormats implements 
     OUT_OF_RANGE("out of range"), /* keep */
     OUT_OF_RANGE_SIMPLE("{0} out of [{1}, {2}] range"), /* keep */
     OUTLINE_BOUNDARY_LOOP_OPEN("an outline boundary loop is open"),
+    OVERFLOW("overflow"), /* keep */
     OVERFLOW_IN_FRACTION("overflow in fraction {0}/{1}, cannot negate"),
     OVERFLOW_IN_ADDITION("overflow in addition: {0} + {1}"),
     OVERFLOW_IN_SUBTRACTION("overflow in subtraction: {0} - {1}"),

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java?rev=1185841&r1=1185840&r2=1185841&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java Tue
Oct 18 20:30:42 2011
@@ -419,6 +419,28 @@ public final class MathUtils {
     }
 
     /**
+     * Returns the first argument with the sign of the second argument.
+     *
+     * @param magnitude Magnitude of the returned value.
+     * @param sign Sign of the returned value.
+     * @return a value with magnitude equal to {@code magnitude} and with the
+     * same sign as the {@code sign} argument.
+     * @throws MathArithmeticException if {@code magnitude == Byte.MIN_VALUE}
+     * and {@code sign >= 0}.
+     */
+    public static byte copySign(byte magnitude, byte sign) {
+        if ((magnitude >= 0 && sign >= 0) ||
+            (magnitude < 0 && sign < 0)) { // Sign is OK.
+            return magnitude;
+        } else if (sign >= 0 &&
+                   magnitude == Byte.MIN_VALUE) {
+            throw new MathArithmeticException(LocalizedFormats.OVERFLOW);
+        } else {
+            return (byte) -magnitude; // Flip sign.
+        }
+    }
+
+    /**
      * Returns the <a href="http://mathworld.wolfram.com/Sign.html"> sign</a>
      * for int value {@code x}.
      * <p>

Modified: commons/proper/math/trunk/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties?rev=1185841&r1=1185840&r2=1185841&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties
(original)
+++ commons/proper/math/trunk/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties
Tue Oct 18 20:30:42 2011
@@ -238,6 +238,7 @@ OUT_OF_ORDER_ABSCISSA_ARRAY = les abscis
 OUT_OF_RANGE_ROOT_OF_UNITY_INDEX = l''indice de racine de l''unit\u00e9 {0} est hors du domaine
autoris\u00e9 [{1};{2}]
 OUT_OF_RANGE_SIMPLE = {0} hors du domaine [{1}, {2}]
 OUT_OF_RANGE = hors domaine
+OVERFLOW = d\u00e9passement de capacit\u00e9
 OVERFLOW_IN_FRACTION = d\u00e9passement de capacit\u00e9 pour la fraction {0}/{1}, son signe
ne peut \u00eatre chang\u00e9
 OVERFLOW_IN_ADDITION = d\u00e9passement de capacit\u00e9 pour l''addition : {0} + {1}
 OVERFLOW_IN_SUBTRACTION = d\u00e9passement de capacit\u00e9 pour la soustraction : {0} -
{1}

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java?rev=1185841&r1=1185840&r2=1185841&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java
Tue Oct 18 20:30:42 2011
@@ -612,4 +612,40 @@ public final class MathUtilsTest {
             // Expected.
         }
     }
+
+    @Test
+    public void testCopySignByte() {
+        byte a = MathUtils.copySign(Byte.MIN_VALUE, (byte) -1);
+        Assert.assertEquals(Byte.MIN_VALUE, a);
+
+        final byte minValuePlusOne = Byte.MIN_VALUE + (byte) 1;
+        a = MathUtils.copySign(minValuePlusOne, (byte) 1);
+        Assert.assertEquals(Byte.MAX_VALUE, a);
+
+        a = MathUtils.copySign(Byte.MAX_VALUE, (byte) -1);
+        Assert.assertEquals(minValuePlusOne, a);
+
+        final byte one = 1;
+        byte val = -2;
+        a = MathUtils.copySign(val, one);
+        Assert.assertEquals(-val, a);
+
+        final byte minusOne = -one;
+        val = 2;
+        a = MathUtils.copySign(val, minusOne);
+        Assert.assertEquals(-val, a);
+
+        val = 0;
+        a = MathUtils.copySign(val, minusOne);
+        Assert.assertEquals(val, a);
+
+        val = 0;
+        a = MathUtils.copySign(val, one);
+        Assert.assertEquals(val, a);
+    }
+
+    @Test(expected=MathArithmeticException.class)
+    public void testCopySignByte2() {
+        MathUtils.copySign(Byte.MIN_VALUE, (byte) 1);
+    }
 }



Mime
View raw message