…or why Java sucks for arbitrary-precision arithmetic.
For many applications floating-point binary is bad. But BigDecimal
isn’t much fun either…
I came across this discussion yesterday on the evilness of BigDecimal
‘s double
constructor. It’s an important point to be aware of when using the BigDecimal
class. In choosing to use BigDecimal
instead of double, you probably wanted an exact value but, if you use that constructor, you’re unlikely to get it. I’d quite like an IDEA inspection to warn me if I inadvertently invoke that constructor with a floating-point literal.
Unfortunately, that issue is not the only thing that you have to keep in mind when using BigDecimal
. Unless you’ve read the Javadocs diligently, you maybe surprised that the equals
method does not consider 2.0 to be equivalent to 2.00. This is because it takes into account the scale as well as the value. If you want to compare values irrespective of scale, you need to use the compareTo
method:
if (firstValue.compareTo(secondValue) == 0)
{
// Do something...
} |
Not particulary readable. An idle glance may wrongly interpret this as a check for zero.
Of course, this means that the implementation of Comparable
is inconsistent with equals. This has unpleasant consequences:
It is strongly recommended (though not required) that natural orderings be consistent with equals. This is so because sorted sets (and sorted maps) without explicit comparators behave “strangely” when they are used with elements (or keys) whose natural ordering is inconsistent with equals. In particular, such a sorted set (or sorted map) violates the general contract for set (or map), which is defined in terms of the equals method.
On top of all this, the BigDecimal
class is not very user-friendly. Consider the equation a = b – c * d. Using doubles this would look something like this:
That’s pretty similar to the mathematical representation. The Java programming language doesn’t provide operators for BigDecimal
s so we have to use instance methods. Converting the above to use BigDecimal
s might look something like this:
BigDecimal a = b.subtract(c).multiply(d) |
Not only is this verbose, it’s also wrong. The rules of precedence have changed. With chained method calls like this, evaluation is strictly left-to-right. Instead of subtracting the product of c and d from b, we are multiplying the difference between b and c by d. We would have to rewrite it to be equivalent to the double example:
BigDecimal a = b.subtract(c.multiply(d)) |
This does not present an insurmountable mental challenge, but it does increase the cognitive load slightly by virtue of being different to what we are used to. We could split the computation into separate stages with intermediate variables. This might make the behaviour clearer but it’s not exactly concise.
The final point to make about the BigDecimal
class is that it is immutable and, as such, each of the “operator” methods returns a new instance. Compare the add
method of a Calendar
or a List
with the add method of a BigDecimal
. One modifies its target, the other returns an entirely new object. This ought not be a problem (immutability has many advantages) but a common error is to forget to use the result of the method. IDEA has an inspection for this. Alternatively, you can use FindBugs. In the absence of this type of tool, these are the kind of issues that will be picked up by your extensive unit test suite.
Conclusions
There is not much we can do about the first two problems. The double constructor won’t be deprecated since it does serve a purpose when performing type conversions from primitive variables. Also, it seems reasonable that BigDecimal
should provide a method to check equality with respect to both value and scale. Whether that method should have been the equals method is a judgment call that is not going to be changed now.
The other issues, those of readability, verbosity and bug-prone patterns of usage, can all be improved by overloading the common arithmetic operators (+, -, *, /, %, +=, -=, *=, /=, %=, — and ++).
Over-loaded operators for BigDecimal
s are apparently one of the language enhancements being considered for Java 7. Whether this would cover the comparison operators is not clear (I cannot find any definitive information on the proposal). The less-than, greater-than, less-than-or-equals and greater-than-or-equals operators would be straightforward. Overloading the equality operator (==) would be more problematic. Existing code may break. Also, would it be consistent with the equals
method or the compareTo
method?