[ 
https://issues.apache.org/jira/browse/MATH-614?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13068257#comment-13068257
 ] 

Arne Plöse commented on MATH-614:
---------------------------------

What about this:
{code}
    @Override
    public String toString() {
        if (isNaN) {
            return String.format("%s + %si", Double.toString(Double.NaN), 
Double.toString(Double.NaN));
        } 
        
        if (imaginary == 0.0) {
            return Double.toString(real);
        } 
        
        if (imaginary > 0) {
            return String.format("%s + %si", Double.toString(real), 
Double.toString(imaginary));
        } 
        
        return String.format("%s - %si", Double.toString(real), 
Double.toString(Math.abs(imaginary)));
    }
{code}

testcase:
{code}
    @Test
    public void testToString() {
        Assert.assertEquals("NaN + NaNi", new Complex(nan, inf).toString());
        Assert.assertEquals("1.0", Complex.ONE.toString());
        Assert.assertEquals("0.0 + 1.0i", Complex.I.toString());
        Assert.assertEquals("0.0 - 1.0i", new Complex(0.0, - 1.0).toString());
        Assert.assertEquals("Infinity + Infinityi", new Complex(inf, 
inf).toString());
        Assert.assertEquals("Infinity - Infinityi", new Complex(inf, - 
inf).toString());
    }
{code}


> "toString" method for "Complex"
> -------------------------------
>
>                 Key: MATH-614
>                 URL: https://issues.apache.org/jira/browse/MATH-614
>             Project: Commons Math
>          Issue Type: Wish
>            Reporter: Gilles
>            Assignee: Gilles
>            Priority: Trivial
>             Fix For: 3.0
>
>         Attachments: math.patch
>
>
> Class "Complex" (in package "complex") lacks a "toString()" method.
> [Request from Arne Ploese on the "dev" ML.]
> I also propose to slightly modify the "format" method in "ComplexFormat" so 
> that the stringified version of "Complex(1, 1)" is "1 + i" instead of "1 + 
> 1i".

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira


Reply via email to