0

So basically I am writing an android app and I notice that the add function is not working properly. When I tried testing it with

Complex dummy = Complex.ZERO;
        dummy.add(Complex.I);
        Log.i("DEBUG", "DUMMY = " + dummy);

I get

I/DEBUG: DUMMY = (0.0, 0.0)

from Logcat. I can't seem to find what went wrong with such a simple function call.

This is the description of the function from Apache's website

public Complex add(Complex addend) throws NullArgumentException

Returns a Complex whose value is (this + addend). Uses the definitional formula (a + bi) + (c + di) = (a+c) + (b+d)i

1
  • 1
    Hint: "returns a Complex", not "mutates a complex" Commented Jan 23, 2019 at 16:39

1 Answer 1

2

Complex.Zero = (0.0 + 0.0i)

Complex.I = (0.0 + 1.0i)

Why not?

The answer is in your question itself(on the docs),

Returns a Complex whose value is (this + addend)

Here dummy.add(Complex.I); returns (0.0 + 1.0i) but is not used. So, assign the return value back to dummy

Correct code would be

dummy = dummy.add(Complex.I);

PS: Also, in my 5+ years of coding. I've blindly trusted Apache Commons everyday everytime. Never it failed ;)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.