commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1146755 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/complex/ComplexFormat.java test/java/org/apache/commons/math/complex/ComplexFormatAbstractTest.java
Date Thu, 14 Jul 2011 15:25:12 GMT
Author: erans
Date: Thu Jul 14 15:25:11 2011
New Revision: 1146755

URL: http://svn.apache.org/viewvc?rev=1146755&view=rev
Log:
MATH-617
Make "1 + 1i" appear as "1 + i" on formatted output.

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/ComplexFormat.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexFormatAbstractTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/ComplexFormat.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/ComplexFormat.java?rev=1146755&r1=1146754&r2=1146755&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/ComplexFormat.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/ComplexFormat.java
Thu Jul 14 15:25:11 2011
@@ -26,6 +26,7 @@ import org.apache.commons.math.util.Comp
 import org.apache.commons.math.exception.util.LocalizedFormats;
 import org.apache.commons.math.exception.MathParseException;
 import org.apache.commons.math.exception.MathIllegalArgumentException;
+import org.apache.commons.math.exception.MathInternalError;
 import org.apache.commons.math.exception.NullArgumentException;
 import org.apache.commons.math.exception.NoDataException;
 
@@ -178,13 +179,16 @@ public class ComplexFormat {
 
         // format sign and imaginary
         double im = complex.getImaginary();
+        StringBuffer imAppendTo = new StringBuffer();
         if (im < 0.0) {
             toAppendTo.append(" - ");
-            CompositeFormat.formatDouble(-im, getImaginaryFormat(), toAppendTo, pos);
+            imAppendTo = formatImaginary(-im, new StringBuffer(), pos);
+            toAppendTo.append(imAppendTo);
             toAppendTo.append(getImaginaryCharacter());
         } else if (im > 0.0 || Double.isNaN(im)) {
             toAppendTo.append(" + ");
-            CompositeFormat.formatDouble(im, getImaginaryFormat(), toAppendTo, pos);
+            imAppendTo = formatImaginary(im, new StringBuffer(), pos);
+            toAppendTo.append(imAppendTo);
             toAppendTo.append(getImaginaryCharacter());
         }
 
@@ -192,6 +196,35 @@ public class ComplexFormat {
     }
 
     /**
+     * Format the absolute value of the imaginary part.
+     *
+     * @param absIm Absolute value of the imaginary part of a complex number.
+     * @param toAppendTo where the text is to be appended.
+     * @param pos On input: an alignment field, if desired. On output: the
+     * offsets of the alignment field.
+     * @return the value passed in as toAppendTo.
+     * @throws MathInternalError if {@code absIm} is not positive.
+     */
+    private StringBuffer formatImaginary(double absIm,
+                                         StringBuffer toAppendTo,
+                                         FieldPosition pos) {
+        if (absIm < 0) {
+            throw new MathInternalError();
+        }
+
+        pos.setBeginIndex(0);
+        pos.setEndIndex(0);
+
+        CompositeFormat.formatDouble(absIm, getImaginaryFormat(), toAppendTo, pos);
+        if (toAppendTo.toString().equals("1")) {
+            // Remove the character "1" if it is the only one.
+            toAppendTo.setLength(0);
+        }
+
+        return toAppendTo;
+    }
+
+    /**
      * Formats a object to produce a string.  {@code obj} must be either a
      * {@link Complex} object or a {@link Number} object.  Any other type of
      * object will result in an {@link IllegalArgumentException} being thrown.

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexFormatAbstractTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexFormatAbstractTest.java?rev=1146755&r1=1146754&r2=1146755&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexFormatAbstractTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexFormatAbstractTest.java
Thu Jul 14 15:25:11 2011
@@ -42,13 +42,39 @@ public abstract class ComplexFormatAbstr
 
     @Test
     public void testSimpleNoDecimals() {
-        Complex c = new Complex(1, 1);
-        String expected = "1 + 1i";
+        Complex c = new Complex(1, 2);
+        String expected = "1 + 2i";
         String actual = complexFormat.format(c);
         Assert.assertEquals(expected, actual);
     }
 
     @Test
+    public void testTrimOneImaginary() {
+        final ComplexFormat fmt = ComplexFormat.getInstance(getLocale());
+        fmt.getImaginaryFormat().setMaximumFractionDigits(1);
+
+        Complex c = new Complex(1, 1.04);
+        String expected = "1 + i";
+        String actual = fmt.format(c);
+        Assert.assertEquals(expected, actual);
+
+        c = new Complex(1, 1.09);
+        expected = "1 + 1" + getDecimalCharacter() + "1i";
+        actual = fmt.format(c);
+        Assert.assertEquals(expected, actual);
+
+        c = new Complex(1, -1.09);
+        expected = "1 - 1" + getDecimalCharacter() + "1i";
+        actual = fmt.format(c);
+        Assert.assertEquals(expected, actual);
+
+        c = new Complex(1, -1.04);
+        expected = "1 - i";
+        actual = fmt.format(c);
+        Assert.assertEquals(expected, actual);
+    }
+
+    @Test
     public void testSimpleWithDecimals() {
         Complex c = new Complex(1.23, 1.43);
         String expected = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter()
+ "43i";
@@ -107,7 +133,7 @@ public abstract class ComplexFormatAbstr
     @Test
     public void testDifferentImaginaryChar() {
         Complex c = new Complex(1, 1);
-        String expected = "1 + 1j";
+        String expected = "1 + j";
         String actual = complexFormatJ.format(c);
         Assert.assertEquals(expected, actual);
     }



Mime
View raw message