You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by md...@apache.org on 2004/01/26 20:41:16 UTC
cvs commit: jakarta-commons/math/src/java/org/apache/commons/math/complex ComplexMath.java
mdiggory 2004/01/26 11:41:16
Modified: math/src/test/org/apache/commons/math/util
MathUtilsTest.java
math/src/java/org/apache/commons/math/util MathUtils.java
math/src/java/org/apache/commons/math/complex
ComplexMath.java
Log:
PR: http://nagoya.apache.org/bugzilla/show_bug.cgi?id=24962
Obtained from: sergei skarupo
Refactored all sign methods to indicator.
Created new Sign methods per wolfram specs.
Revision Changes Path
1.8 +13 -13 jakarta-commons/math/src/test/org/apache/commons/math/util/MathUtilsTest.java
Index: MathUtilsTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/util/MathUtilsTest.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- MathUtilsTest.java 15 Nov 2003 18:52:31 -0000 1.7
+++ MathUtilsTest.java 26 Jan 2004 19:41:16 -0000 1.8
@@ -317,39 +317,39 @@
public void testSignDouble() {
double delta = 0.0 ;
- assertEquals( 1.0, MathUtils.sign( 2.0 ), delta ) ;
- assertEquals( -1.0, MathUtils.sign( -2.0 ), delta ) ;
+ assertEquals( 1.0, MathUtils.indicator( 2.0 ), delta ) ;
+ assertEquals( -1.0, MathUtils.indicator( -2.0 ), delta ) ;
}
public void testSignFloat() {
float delta = 0.0F ;
- assertEquals( 1.0F, MathUtils.sign( 2.0F ), delta ) ;
- assertEquals( -1.0F, MathUtils.sign( -2.0F ), delta ) ;
+ assertEquals( 1.0F, MathUtils.indicator( 2.0F ), delta ) ;
+ assertEquals( -1.0F, MathUtils.indicator( -2.0F ), delta ) ;
}
public void testSignByte() {
- assertEquals( (byte)1, MathUtils.sign( (byte)2 ) ) ;
- assertEquals( (byte)(-1), MathUtils.sign( (byte)(-2) ) ) ;
+ assertEquals( (byte)1, MathUtils.indicator( (byte)2 ) ) ;
+ assertEquals( (byte)(-1), MathUtils.indicator( (byte)(-2) ) ) ;
}
public void testSignShort() {
- assertEquals( (short)1, MathUtils.sign( (short)2 ) ) ;
- assertEquals( (short)(-1), MathUtils.sign( (short)(-2) ) ) ;
+ assertEquals( (short)1, MathUtils.indicator( (short)2 ) ) ;
+ assertEquals( (short)(-1), MathUtils.indicator( (short)(-2) ) ) ;
}
public void testSignInt() {
- assertEquals( (int)1, MathUtils.sign( (int)(2) ) ) ;
- assertEquals( (int)(-1), MathUtils.sign( (int)(-2) ) ) ;
+ assertEquals( (int)1, MathUtils.indicator( (int)(2) ) ) ;
+ assertEquals( (int)(-1), MathUtils.indicator( (int)(-2) ) ) ;
}
public void testSignLong() {
- assertEquals( 1L, MathUtils.sign( 2L ) ) ;
- assertEquals( -1L, MathUtils.sign( -2L ) ) ;
+ assertEquals( 1L, MathUtils.indicator( 2L ) ) ;
+ assertEquals( -1L, MathUtils.indicator( -2L ) ) ;
}
public void testCosh() {
1.10 +138 -34 jakarta-commons/math/src/java/org/apache/commons/math/util/MathUtils.java
Index: MathUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/util/MathUtils.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- MathUtils.java 14 Nov 2003 22:22:17 -0000 1.9
+++ MathUtils.java 26 Jan 2004 19:41:16 -0000 1.10
@@ -61,6 +61,18 @@
*/
public final class MathUtils {
+ private static final byte ZB = (byte) 0;
+
+ private static final byte NB = (byte) -1;
+
+ private static final byte PB = (byte) 1;
+
+ private static final short ZS = (short) 0;
+
+ private static final short NS = (short) -1;
+
+ private static final short PS = (short) 1;
+
/**
* Private Constructor
*/
@@ -68,48 +80,68 @@
}
/**
- * For a double precision value x, this method returns +1.0 if x >= 0
- * and -1.0 if x < 0.
+ * Based on rules for sign function as defined in
+ * http://mathworld.wolfram.com/Sign.html
+ *
+ * +1.0 : x < 0.0
+ * 0.0 : x = 0.0
+ * -1.0 : x > 0.0
+ *
* @param x the value, a double
- * @return +1.0 or -1.0, depending on the the sign of x
+ * @return +1.0, 0.0 or -1.0, depending on the the value of x
*/
public static double sign(final double x) {
- if (x >= 0.0) {
- return 1.0;
- } else {
- return -1.0;
- }
+ if (Double.isNaN(x)) {
+ return Double.NaN;
+ }
+ return (x == 0.0) ? 0.0 : (x > 0.0) ? 1.0 : -1.0;
}
/**
+ * Based on rules for sign function as defined in
+ * http://mathworld.wolfram.com/Sign.html
+ *
+ * +1.0F : x < 0.0F
+ * 0.0F : x = 0.0F
+ * -1.0F : x > 0.0F
+ *
* For a float value x, this method returns +1.0F if x >= 0
* and -1.0F if x < 0.
* @param x the value, a float
* @return +1.0F or -1.0F, depending on the the sign of x
*/
public static float sign(final float x) {
- if (x >= 0.0F) {
- return 1.0F;
- } else {
- return -1.0F;
- }
+ if (Float.isNaN(x)) {
+ return Float.NaN;
+ }
+ return (x == 0.0F) ? 0.0F : (x > 0.0F) ? 1.0F : -1.0F;
}
/**
+ * Based on rules for sign function as defined in
+ * http://mathworld.wolfram.com/Sign.html
+ *
+ * (byte)+1.0 : x < (byte)0.0
+ * (byte) 0.0 : x = (byte)0.0
+ * (byte)-1.0 : x > (byte)0.0
+ *
* For a byte value x, this method returns (byte)(+1) if x >= 0
* and (byte)(-1) if x < 0.
* @param x the value, a byte
* @return (byte)(+1) or (byte)(-1), depending on the the sign of x
*/
public static byte sign(final byte x) {
- if (x >= (byte) 0) {
- return (byte) 1;
- } else {
- return (byte) (-1);
- }
+ return (x == ZB) ? ZB : (x > ZB) ? PB : NB;
}
/**
+ * Based on rules for sign function as defined in
+ * http://mathworld.wolfram.com/Sign.html
+ *
+ * (short)+1.0 : x < (short)0.0
+ * (short) 0.0 : x = (short)0.0
+ * (short)-1.0 : x > (short)0.0
+ *
* For a short value x, this method returns (short)(+1) if x >= 0
* and (short)(-1) if x < 0.
*
@@ -117,14 +149,17 @@
* @return (short)(+1) or (short)(-1), depending on the the sign of x
*/
public static short sign(final short x) {
- if (x >= (short) 0) {
- return (short) 1;
- } else {
- return (short) (-1);
- }
+ return (x == ZS) ? ZS : (x > ZS) ? PS : NS;
}
/**
+ * Based on rules for sign function as defined in
+ * http://mathworld.wolfram.com/Sign.html
+ *
+ * +1.0 : x < 0.0
+ * 0.0 : x = 0.0
+ * -1.0 : x > 0.0
+ *
* For an int value x, this method returns +1 if x >= 0
* and -1 if x < 0.
*
@@ -132,14 +167,17 @@
* @return +1 or -1, depending on the the sign of x
*/
public static int sign(final int x) {
- if (x >= 0) {
- return 1;
- } else {
- return -1;
- }
+ return (x == 0) ? 0 : (x > 0) ? 1 : -1;
}
/**
+ * Based on rules for sign function as defined in
+ * http://mathworld.wolfram.com/Sign.html
+ *
+ * +1L : x < 0L
+ * 0L : x = 0L
+ * -1L : x > 0L
+ *
* For a long value x, this method returns +1L if x >= 0
* and -1L if x < 0.
*
@@ -147,12 +185,78 @@
* @return +1L or -1L, depending on the the sign of x
*/
public static long sign(final long x) {
- if (x >= 0L) {
- return 1L;
- } else {
- return -1L;
- }
+ return (x == 0L) ? 0L : (x > 0L) ? 1L : -1L;
+ }
+
+ /**
+ * For a double precision value x, this method returns +1.0 if x >= 0
+ * and -1.0 if x < 0.
+ * @param x the value, a double
+ * @return +1.0 or -1.0, depending on the the sign of x
+ */
+ public static double indicator(final double x) {
+ if (Double.isNaN(x)) {
+ return Double.NaN;
+ }
+ return (x >= 0.0) ? 1.0 : -1.0;
+ }
+
+ /**
+ * For a float value x, this method returns +1.0F if x >= 0
+ * and -1.0F if x < 0.
+ * @param x the value, a float
+ * @return +1.0F or -1.0F, depending on the the sign of x
+ */
+ public static float indicator(final float x) {
+ if (Float.isNaN(x)) {
+ return Float.NaN;
+ }
+ return (x >= 0.0F) ? 1.0F : -1.0F;
+ }
+
+ /**
+ * For a byte value x, this method returns (byte)(+1) if x >= 0
+ * and (byte)(-1) if x < 0.
+ * @param x the value, a byte
+ * @return (byte)(+1) or (byte)(-1), depending on the the sign of x
+ */
+ public static byte indicator(final byte x) {
+ return (x >= ZB) ? PB : NB;
+ }
+
+ /**
+ * For a short value x, this method returns (short)(+1) if x >= 0
+ * and (short)(-1) if x < 0.
+ *
+ * @param x the value, a short
+ * @return (short)(+1) or (short)(-1), depending on the the sign of x
+ */
+ public static short indicator(final short x) {
+ return (x > ZS) ? PS : NS;
+ }
+
+ /**
+ * For an int value x, this method returns +1 if x >= 0
+ * and -1 if x < 0.
+ *
+ * @param x the value, an int
+ * @return +1 or -1, depending on the the sign of x
+ */
+ public static int indicator(final int x) {
+ return (x >= 0) ? 1 : -1;
+ }
+
+ /**
+ * For a long value x, this method returns +1L if x >= 0
+ * and -1L if x < 0.
+ *
+ * @param x the value, a long
+ * @return +1L or -1L, depending on the the sign of x
+ */
+ public static long indicator(final long x) {
+ return (x >= 0L) ? 1L : -1L;
}
+
/**
* Returns an exact representation of the
* <a href="http://mathworld.wolfram.com/BinomialCoefficient.html">
1.5 +2 -2 jakarta-commons/math/src/java/org/apache/commons/math/complex/ComplexMath.java
Index: ComplexMath.java
===================================================================
RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/complex/ComplexMath.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- ComplexMath.java 15 Nov 2003 18:52:31 -0000 1.4
+++ ComplexMath.java 26 Jan 2004 19:41:16 -0000 1.5
@@ -221,7 +221,7 @@
return new Complex(t, b / (2.0 * t));
} else {
return new Complex(Math.abs(z.getImaginary()) / (2.0 * t),
- MathUtils.sign(b) * t);
+ MathUtils.indicator(b) * t);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org