1

I'm writing a method that counts Fermat Point for my homework. But i can not pass the tests. I already tried to do all calculations in simple double. But it is working the same way. Two tests failed, and I think it's not about rounding. enter image description here enter image description here

Problems are with 'y' counting and the strangest thing that in one test it not enought numbers, and in other it seems that can be rounded. What am i doing wrong?

    BigDecimal ax = new BigDecimal(points.get(0).getX());
    BigDecimal ay = new BigDecimal(points.get(0).getY());
    BigDecimal bx = new BigDecimal(points.get(1).getX());
    BigDecimal by = new BigDecimal(points.get(1).getY());
    BigDecimal cx = new BigDecimal(points.get(2).getX());
    BigDecimal cy = new BigDecimal(points.get(2).getY());


    BigDecimal firstX = cx.add(bx).add(cy.subtract(by).multiply(new BigDecimal(StrictMath.sqrt(3))))
            .divide(new BigDecimal(2.0d), 20, RoundingMode.HALF_UP);

    BigDecimal firstY = cy.add(by).add(bx.subtract(cx).multiply(new BigDecimal(StrictMath.sqrt(3))))
            .divide(new BigDecimal(2.0d), 20, RoundingMode.HALF_UP);


    BigDecimal secondX = ax.add(bx).add(ay.subtract(by).multiply(new BigDecimal(StrictMath.sqrt(3))))
            .divide(new BigDecimal(2.0d), 20, RoundingMode.HALF_UP);

    BigDecimal secondY = ay.add(by).add(bx.subtract(ax).multiply(new BigDecimal(StrictMath.sqrt(3))))
            .divide(new BigDecimal(2.0d), 20, RoundingMode.HALF_UP);


    BigDecimal a1 = cy.subtract(secondY);

    BigDecimal b1 = secondX.subtract(cx);

    BigDecimal a2 = ay.subtract(firstY);

    BigDecimal b2 = firstX.subtract(ax);


    BigDecimal d = a1.multiply(b2).subtract(a2.multiply(b1));


    BigDecimal c1 = secondY.multiply(cx).subtract(secondX.multiply(cy));

    BigDecimal c2 = firstY.multiply(ax).subtract(firstX.multiply(ay));


    BigDecimal resultX = b1.multiply(c2).subtract(b2.multiply(c1));
    resultX = resultX.divide(d, 16, RoundingMode.HALF_UP);

    BigDecimal resultY = a2.multiply(c1).subtract(a1.multiply(c2));
    resultY = resultY.divide(d, 17, RoundingMode.HALF_UP);
  • 4
    This honestly reflects a problem with the tests, most likely; it shouldn't care about that level of accuracy in the doubles. Otherwise -- as you're finding -- the calculations must be done in one exact way, and even mathematically equivalent alternatives don't work. – Louis Wasserman Aug 31 '20 at 18:49
  • 5
    How did you get the reference values? You're using at least one problematic `double` calculation (`sqrt(3)`) which will introduce imprecise values. At the end of the day if your inputs are `double` then `BigDecimal` can't magically make all rounding and representation problems go away. They might just avoid adding further ones in the intermediate step, but won't remove existing problems. – Joachim Sauer Aug 31 '20 at 18:54
  • 1
    Does this answer your question? [BigDecimal/double Precision - number rounds up higher](https://stackoverflow.com/questions/51371109/bigdecimal-double-precision-number-rounds-up-higher) – Progman Aug 31 '20 at 19:31

0 Answers0