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

Phil Steitz commented on MATH-306:
----------------------------------

Thanks for reviewing the code.

The code is misleading, but should not lead to incorrect results.  The line you 
refer to above is actually in a block that will never get executed - so should 
be deleted.
 {code}
        if (Math.abs(c) < Math.abs(d)) {
            if (d == 0.0) {  <---- impossible to have abs(c) < 0
                return createComplex(real/c, imaginary/c);
            }
            double q = c / d;
            double denominator = c * q + d;
            return createComplex((real * q + imaginary) / denominator,
                (imaginary * q - real) / denominator);
        } else {
            if (c == 0.0) {  <-- to get here, we need c = d = 0,  but this is 
handled above.
                return createComplex(imaginary/d, -real/c);  <-- incorrect fmla 
is harmless because never executed.
            }
{code}


Interesting that static analyzers did not catch this as dead code.  Unless I am 
missing something, both of the if(* == 0) tests in the block above can be 
removed (with no effect on results).

Appreciate comments on this, but leaning toward code cleanup and closing as 
invalid.


> Method 'divide' in class 'Complex' uses a false formula for a special case 
> resulting in erroneous division by zero.
> -------------------------------------------------------------------------------------------------------------------
>
>                 Key: MATH-306
>                 URL: https://issues.apache.org/jira/browse/MATH-306
>             Project: Commons Math
>          Issue Type: Bug
>         Environment: all
>            Reporter: Joerg Huber
>
> The formula that 'divide' wants to implement is
> ( a + bi )  /  ( c + di )  =  ( ac + bd + ( bc - ad ) i )  /  ( c^2 + d^2 )
> as correctly written in the description.
> When c == 0.0 this leads to the special case
> ( a + bi )  /  di  = ( b / d ) - ( a / d ) i
> But the corresponding code is:
> if (c == 0.0) {
>     return createComplex(imaginary/d, -real/c);
> }
> The bug is the last division -real/c, which should obviously be -real/d.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to