Search code examples
javadoublebigdecimaldivideincompatibletypeerror

BigDecimal.divide(BD,int,RM) - BigDecimal cannot be converted to double error


The situation:

  public static double pi(int a) {
    return (BigDecimal.valueOf(53360*sqrt(640320))).divide(computeS(a),a,RoundingMode.HALF_DOWN);
  }

with: private static BigDecimal computeS(int a)

and here is the terminal

The problem is that while I compile the program it make a incompatible types error telling me that "BigDecimal cannot be converted to double" which seem to be a nonsense when you know that BigDecimal.divide(BigDecimal,int,RoundingMode) doesn't need any double and his polymorphic methods neither...

So I'm a bit stuck here. Can anyone help? Thanks!

PS: I also tried to put a double instead of a BigDecimal but as expected: terminal


Solution

  • The problem is not with the divide method, the problem is that your pi method must return a double, but you are returning a BigDecimal .

    Return the double you get by calling doubleValue() on it .

    return (BigDecimal.valueOf(53360*sqrt(640320))).divide(computeS(a),a,RoundingMode.HALF_DOWN).doubleValue();