tobrien     2004/05/22 17:52:32

  Modified:    math/src/test/org/apache/commons/math/complex
                        ComplexFormatTest.java
               math/src/java/org/apache/commons/math/complex
                        ComplexFormat.java
  Log:
  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.

  
  Revision  Changes    Path
  1.4       +15 -0     
jakarta-commons/math/src/test/org/apache/commons/math/complex/ComplexFormatTest.java
  
  Index: ComplexFormatTest.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/complex/ComplexFormatTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ComplexFormatTest.java    27 Apr 2004 04:37:59 -0000      1.3
  +++ ComplexFormatTest.java    23 May 2004 00:52:32 -0000      1.4
  @@ -82,4 +82,19 @@
                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" );
  +     }
  +
   }
  
  
  
  1.6       +17 -12    
jakarta-commons/math/src/java/org/apache/commons/math/complex/ComplexFormat.java
  
  Index: ComplexFormat.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/complex/ComplexFormat.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ComplexFormat.java        27 Apr 2004 04:37:59 -0000      1.5
  +++ ComplexFormat.java        23 May 2004 00:52:32 -0000      1.6
  @@ -31,9 +31,7 @@
       /** 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 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() );
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to