MATH-690
New "copySign" method. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1185841 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
74ab566575
commit
682d3afe65
|
@ -272,6 +272,7 @@ public enum LocalizedFormats implements Localizable {
|
|||
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}"),
|
||||
|
|
|
@ -418,6 +418,28 @@ public final class MathUtils {
|
|||
return (x == ZB) ? ZB : (x > ZB) ? PB : NB;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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}.
|
||||
|
|
|
@ -238,6 +238,7 @@ OUT_OF_ORDER_ABSCISSA_ARRAY = les abscisses doivent \u00eatre en ordre stricteme
|
|||
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}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue