diff --git a/src/java/org/apache/commons/math/complex/ComplexFormat.java b/src/java/org/apache/commons/math/complex/ComplexFormat.java index c8443658c..c36c5421a 100644 --- a/src/java/org/apache/commons/math/complex/ComplexFormat.java +++ b/src/java/org/apache/commons/math/complex/ComplexFormat.java @@ -24,16 +24,14 @@ import java.text.NumberFormat; * can be configured. * * @author Apache Software Foundation - * @version $Revision: 1.5 $ $Date: 2004/04/27 04:37:59 $ + * @version $Revision: 1.6 $ $Date: 2004/05/23 00:52:32 $ */ public class ComplexFormat { /** The default complex format. */ private static final ComplexFormat DEFAULT = new ComplexFormat(); - // @TODO This class only allows for max fraction digits, we might want to allow other parameters - - /** The notation used to signify the imaginary part of the complex number. */ + /** The notation used to signify the imaginary part of the complex number. */ private String imaginaryCharacter = "i"; /** The maximum number of decimal digits in the formatted output. */ @@ -74,24 +72,31 @@ public class ComplexFormat { */ public String format(Complex c) { - // @TODO What happens when either a real or imaginary is NaN, INIFINITY, etc? - NumberFormat format = NumberFormat.getInstance(); format.setMaximumFractionDigits( fractionDigits ); StringBuffer buffer = new StringBuffer(); - buffer.append( format.format( c.getReal() ) ); + if( Double.isNaN( c.getReal() ) || Double.isInfinite( c.getReal() ) ) { + buffer.append( "(" + c.getReal() + ")" ); + } else { + buffer.append( format.format( c.getReal() ) ); + } if( c.getImaginary() < 0 ) { buffer.append( " - " ); - buffer.append( format.format( Math.abs(c.getImaginary()) ) ); - buffer.append( imaginaryCharacter ); - } else if( c.getImaginary() > 0 ) { + } else if( c.getImaginary() > 0 || Double.isNaN( c.getImaginary() )) { buffer.append( " + " ); - buffer.append( format.format( c.getImaginary() ) ); - buffer.append( imaginaryCharacter ); } + + if( c.getImaginary() != 0 ) { + if( Double.isNaN( c.getImaginary() ) || Double.isInfinite( c.getImaginary() ) ) { + buffer.append( "(" + Math.abs( c.getImaginary() ) + ")" ); + } else { + buffer.append( format.format( Math.abs(c.getImaginary()) ) ); + } + buffer.append( imaginaryCharacter ); + } return( buffer.toString() ); diff --git a/src/test/org/apache/commons/math/complex/ComplexFormatTest.java b/src/test/org/apache/commons/math/complex/ComplexFormatTest.java index d88cb6862..0832b1559 100644 --- a/src/test/org/apache/commons/math/complex/ComplexFormatTest.java +++ b/src/test/org/apache/commons/math/complex/ComplexFormatTest.java @@ -82,4 +82,19 @@ public class ComplexFormatTest extends TestCase { assertEquals( ComplexFormat.formatComplex( c ), "232.22 - 342.33i" ); } + public void testNan() { + Complex c = new Complex(Double.NaN, Double.NaN); + assertEquals( complexFormat.format( c ), "(NaN) + (NaN)i" ); + } + + public void testPositiveInfinity() { + Complex c = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); + assertEquals( complexFormat.format( c ), "(Infinity) + (Infinity)i" ); + } + + public void testNegativeInfinity() { + Complex c = new Complex(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY); + assertEquals( complexFormat.format( c ), "(-Infinity) - (Infinity)i" ); + } + }