Search code examples
javaroundingbigdecimal

Why is BigDecimal.divide() giving me a wrong answer?


I am currently using BigDecimal types for a project, and they've been working great.

Except for very large values - each divide appears to be giving me an unusually round and incorrect answer.

Here is an excerpt from my code:

System.out.print("a: "+a+", b: "+b);
i = a.divide(b, mc).setScale(0, RoundingMode.FLOOR);
System.out.println(", result: "+i);

And I am getting printouts such as:

a: 100000000000106, b: 1.414213562373095048801688724209699005327534773448623528143294164, result: 70000000000000
a: 100000000000022, b: 1.414213562373095048801688724209699005327534773448623528143294164, result: 70000000000000
a: 100000000000036, b: 1.414213562373095048801688724209699005327534773448623528143294164, result: 70000000000000

When I plug these three divides into a calculator, they each give me roughly 70710678118704. However the BigDecimal divide is giving me an even 70000000000000.

Does this have to do with me rounding incorrectly?

Note: All types (a, b, and i) are all BigDecimals

Edit: Added the instantiation of mc:

MathContext mc = new MathContext(1, RoundingMode.DOWN);

Solution

  • In your division, you are providing a MathContext argument, which controls how many significant figures your division will use. Your results indicate that you are EITHER

    • using a MathContext with a precision of 1; OR
    • using a MathContext with a precision of 2, but a rounding mode that rounds down.

    That is, you're only ever going to divide to one or two significant figures.

    You need to create a different MathContext object, with a higher value for precision, for example,

    mc = new MathContext(10);
    

    if you want division to 10 significant figures.