0

I have created a class for complex numbers and a constructor for the class which takes 2 doubles, like

Complex c = new Complex(3.0,1.0)

Then how do I compare a Complex number to 0? I know that I need to create a tolerance number, say

TOL = 1.0e-10

Do I just do this:

Complex NullComplex = new Complex(TOL,TOL);
c >= NullComplex;

I would like to get a boolean output from this. But whenever I want to compare, I get that the operators "=>" are undefined for the type double, Complex (or Complex, Complex)

Slim Shady
  • 220
  • 3
  • 18
  • Assuming your `Complex` class defines methods for `subtract` and `absoluteValue`, you can do `absoluteValue(subtract(new Complex(0, 0), c)) < TOL` – tobias_k Mar 09 '20 at 16:17
  • It is not possible to override operators in Java. `c >= NullComplex` will not work. You need to add a method to your class. It will probably look like this: `c.compareWith(zeroComplex, TOL)` – Michael Mar 09 '20 at 16:19
  • Your `Complex` is just another class for java. It might as well be an `Dog` or a `Car`. Java does not know how to compare a `Dog` or a `Car`, you need to specify what criteria has to be used. Create a method for that or implement the `Comparable` interface. – f1sh Mar 09 '20 at 16:20

0 Answers0