ComplexFormat now handles situations where either the real or

imaginary part is NaN, POSITIVE_INFINITY, or NEGATIVE_INFINITY.
Three new tests were added to address these situations.
PR:
Obtained from:
Submitted by:	
Reviewed by:	
CVS: ----------------------------------------------------------------------
CVS: PR:
CVS:   If this change addresses a PR in the problem report tracking
CVS:   database, then enter the PR number(s) here.
CVS: Obtained from:
CVS:   If this change has been taken from another system, such as NCSA,
CVS:   then name the system in this line, otherwise delete it.
CVS: Submitted by:
CVS:   If this code has been contributed to Apache by someone else; i.e.,
CVS:   they sent us a patch or a new module, then include their name/email
CVS:   address here. If this is your work then delete this line.
CVS: Reviewed by:
CVS:   If we are doing pre-commit code reviews and someone else has
CVS:   reviewed your changes, include their name(s) here.
CVS:   If you have not had it reviewed then delete this line.


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141237 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Tim O'Brien 2004-05-23 00:52:32 +00:00
parent 56d545478a
commit 9459e748c8
2 changed files with 32 additions and 12 deletions

View File

@ -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() );

View File

@ -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" );
}
}