Note that there are some explanatory texts on larger screens.

plurals
  1. POArithmeticException thrown during BigDecimal.divide
    primarykey
    data
    text
    <p>I thought <a href="http://java.sun.com/javase/6/docs/api/java/math/BigDecimal.html" rel="noreferrer"><code>java.math.BigDecimal</code></a> is supposed to be The Answer&trade; to the need of performing infinite precision arithmetic with decimal numbers.</p> <p>Consider the following snippet:</p> <pre><code>import java.math.BigDecimal; //... final BigDecimal one = BigDecimal.ONE; final BigDecimal three = BigDecimal.valueOf(3); final BigDecimal third = one.divide(three); assert third.multiply(three).equals(one); // this should pass, right? </code></pre> <p>I expect the <code>assert</code> to pass, but in fact the execution doesn't even get there: <code>one.divide(three)</code> causes <code>ArithmeticException</code> to be thrown!</p> <pre><code>Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result. at java.math.BigDecimal.divide </code></pre> <p>It turns out that this behavior is explicitly documented in the <a href="http://java.sun.com/javase/6/docs/api/java/math/BigDecimal.html" rel="noreferrer">API</a>:</p> <blockquote> <p>In the case of <code>divide</code>, the exact quotient could have an infinitely long decimal expansion; for example, 1 divided by 3. If the quotient has a non-terminating decimal expansion and the operation is specified to return an exact result, an <code>ArithmeticException</code> is thrown. Otherwise, the exact result of the division is returned, as done for other operations.</p> </blockquote> <p>Browsing around the API further, one finds that in fact there are various overloads of <code>divide</code> that performs <em>inexact</em> division, i.e.: </p> <pre><code>final BigDecimal third = one.divide(three, 33, RoundingMode.DOWN); System.out.println(three.multiply(third)); // prints "0.999999999999999999999999999999999" </code></pre> <p>Of course, the obvious question now is "What's the point???". I thought <code>BigDecimal</code> is the solution when we need <em>exact</em> arithmetic, e.g. for financial calculations. If we can't even <code>divide</code> exactly, then how useful can this be? Does it actually serve a general purpose, or is it only useful in a very niche application where you fortunately just don't need to <code>divide</code> at all?</p> <p>If this is not the right answer, what <em>CAN</em> we use for exact division in financial calculation? (I mean, I don't have a finance major, but they still use division, right???).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload