Home>

I am practicing to write a slow calculation program using the BigDecimal type, and when I run the junit test, I get an assertionfailed error, and when I divide it by 0, I get an error saying that it is useless.
I don't know if I made a mistake in the code, so please let me know.
Thank you.

Error message
assertionfailederror
### Corresponding source code
public static BigDecimal division (final BigDecimal a, final BigDecimal b) throws IllegalArgumentException {
if (a == null || b == null || b == BigDecimal.valueOf (0)) {
   throw new IllegalArgumentException ("invalid");
}
BigDecimal number1 = a.divide (b, 8, RoundingMode.HALF_UP);
   return number1;
}

Enter the language name here
Source code
`` ```

What I tried

I tried to rewrite the if (b == BigDecimal.valOf (0)) part in various ways, but I couldn't understand it without any change.
Thank you.

Supplementary information (FW/tool version, etc.)

Please provide more detailed information here.

  • Answer # 1

    Java==IsSame objectIt is (generally) unsuitable when you want to find out if the values ​​are the same, as it will determine if.

    Also,equalsAlsoscaleYou can even see if they are equal, so if you want to check "whether the value is 0"compareToPlease use.

    b.compareTo (BigDecimal.ZERO) == 0

  • Answer # 2

    Error that it is useless if divided by 0

    I think this is the case ...
    Reference: Division by zero
    Division by zero