java – BigDecimal multiply by zero-ThrowExceptions

Exception or error:

I am performing a simple multiplication with BigDecimal and I have found some strange behaviour when multiplying by zero (multiplying by zero is correct in this use-case).

Basic maths tells me that anything multiplied by zero will equal zero (see:Zero Product Property and Multiplication Properties)

However, the following code will consistently fail with the same error:

assertEquals(new BigDecimal(0), new BigDecimal(22.3).multiply(new BigDecimal(0)));
java.lang.AssertionError: 
Expected :0
Actual   :0E-48

Is this an inaccuracy with BigDecimal or is there some niche branch of maths that I’m missing somewhere?

Notes: JDK 1.6.0_27 running in IntelliJ 11

How to solve:

You can’t use the equals() method to compare BigDecimals, like this assertion does. That is because this equals function will compare the scale. If the scale is different, equals() will return false, even if they are the same number mathematically.

You can however use compareTo() to do what you want:

As @assylias points out, you should also use the new BigDecimal("22.3") constructor to avoid double precision issues.

BigDecimal expected = BigDecimal.ZERO;
BigDecimal actual = new BigDecimal("22.3").multiply(BigDecimal.ZERO);
assertEquals(0, expected.compareTo(actual));

There is also a method called signum(), that returns -1, 0 or 1 for negative, zero, and positive. So you can also test for zero with

assertEquals(0, actual.signum());

Answer:

There are 2 issues with your code:

  • you should compare BigDecimal with compareTo instead of equals, as advised by the other answers
  • but you should also use the string constructor: new BigDecimal("22.3") instead of the double constructor new BigDecimal(22.3) to avoid double precision issues

In other words, the following code (which correctly uses compareTo) still returns false:

BigDecimal bd = new BigDecimal(0.1).multiply(new BigDecimal(10));
System.out.println(bd.compareTo(BigDecimal.ONE) == 0);

because 0.1d * 10d != 1

Answer:

equals() on BigDecimal checks the internal state of BigDecimal for comparison

Refer the code below

public boolean equals(Object x) {
    if (!(x instanceof BigDecimal))
        return false;
    BigDecimal xDec = (BigDecimal) x;
    if (x == this)
        return true;
    if (scale != xDec.scale)
        return false;
    long s = this.intCompact;
    long xs = xDec.intCompact;
    if (s != INFLATED) {
        if (xs == INFLATED)
            xs = compactValFor(xDec.intVal);
        return xs == s;
    } else if (xs != INFLATED)
        return xs == compactValFor(this.intVal);

    return this.inflate().equals(xDec.inflate());
}

if you want to compare the values use compareTo()

Change your code to

assertEquals(0 , new BigDecimal(0).compareTo(new BigDecimal(22.3).multiply(new BigDecimal(0)));

Update:

Use constructor taking String as a parameter for BigDecimal for accuracy in precision check the related links below


Also See

Leave a Reply

Your email address will not be published. Required fields are marked *