/** * Test for equality up to a given precision. * <p> * The precision argument sets how many total digits of precision to * consider. That's digits in the number, not just digits after the decimal * point. The decimal point (for this purpose) counts as a digit! 123.456 is * seven digits, not three or six. * * @param c * the Complex to comapre for equality. * @param precision * the number of digits of precsion to use. * @return true if this Complex is equal (within precision) to the argument, * false otherwise. */ public boolean equals(Complex c, int precision) { String meReal = Double.toString(re); String cReal = Double.toString(c.getRe()); String meImag = Double.toString(im); String cImag = Double.toString(c.getIm()); if (re < 0) { if (meReal.length() > precision) meReal = meReal.substring(0, precision + 1); if (cReal.length() > precision) cReal = cReal.substring(0, precision + 1); } else { if (meReal.length() >= precision) meReal = meReal.substring(0, precision); if (cReal.length() > precision) cReal = cReal.substring(0, precision); } if (Double.parseDouble(meReal) != Double.parseDouble(cReal)) return false; if (im < 0) { if (meImag.length() > precision) meImag = meImag.substring(0, precision); if (cImag.length() > precision) cImag = cImag.substring(0, precision); } else { if (meImag.length() >= precision) meImag = meImag.substring(0, precision); if (cImag.length() > precision) cImag = cImag.substring(0, precision); } if (Double.parseDouble(meImag) != Double.parseDouble(cImag)) return false; return true; }