We know that using double
for currency is error-prone and not recommended. However, I'm yet to see a realistic example, where BigDecimal
works while double
fails and can't be simply fixed by some rounding.
Note that trivial problems
double total = 0.0;
for (int i = 0; i < 10; i++) total += 0.1;
for (int i = 0; i < 10; i++) total -= 0.1;
assertTrue(total == 0.0);
don't count as they're trivially solved by rounding (in this example anything from zero to sixteen decimal places would do).
Computations involving summing big values may need some intermediate rouding, but given the total currency in circulation being USD 1e12
, Java double
(i.e., the standard IEEE double precision) with its 15 decimal digits is still sufficient event for cents.
Computations involving division are in general imprecise even with BigDecimal
. I can construct a computation which can't be performed with double
s, but can be performed with BigDecimal
using a scale of 100, but it's not something you can encounter in reality.
I don't claim that such a realistic example does not exist, it's just that I haven't seen it yet.
I also surely agree, that using double
is more error-prone.
Example
What I'm looking for is a method like the following (based on the answer by Roland Illig)
/**
* Given an input which has three decimal places,
* round it to two decimal places using HALF_EVEN.
*/
BigDecimal roundToTwoPlaces(BigDecimal n) {
// To make sure, that the input has three decimal places.
checkArgument(n.scale() == 3);
return n.round(new MathContext(2, RoundingMode.HALF_EVEN));
}
together with a test like
public void testRoundToTwoPlaces() {
final BigDecimal n = new BigDecimal("0.615");
final BigDecimal expected = new BigDecimal("0.62");
final BigDecimal actual = roundToTwoPlaces(n);
Assert.assertEquals(expected, actual);
}
When this gets naively rewritten using double
, then the test could fail (it doesn't for the given input, but it does for others). However, it can be done correctly:
static double roundToTwoPlaces(double n) {
final long m = Math.round(1000.0 * n);
final double x = 0.1 * m;
final long r = (long) Math.rint(x);
return r / 100.0;
}
It's ugly and error-prone (and can probably be simplified), but it can be easily encapsulated somewhere. That's why I'm looking for more answers.
I can see four basic ways that double
can screw you when dealing with currency calculations.
Mantissa Too Small
With ~15 decimal digits of precision in the mantissa, you are you going to get the wrong result any time you deal with amounts larger than that. If you are tracking cents, problems would start to occur before 1013 (ten trillion) dollars.
While that's a big number, it's not that big. The US GDP of ~18 trillion exceeds it, so anything dealing with country or even corporation sized amounts could easily get the wrong answer.
Furthermore, there are plenty of ways that much smaller amounts could exceed this threshold during calculation. You might be doing a growth projection or a over a number of years, which results in a large final value. You might be doing a "what if" scenario analysis where various possible parameters are examined and some combination of parameters might result in very large values. You might be working under financial rules which allow fractions of a cent which could chop another two orders of magnitude or more off of your range, putting you roughly in line with the wealth of mere individuals in USD.
Finally, let's not take a US centric view of things. What about other currencies? The Indonesian Rupiah is worth roughly 13,000 USD, so that's another 2 orders of magnitude you need to track currency amounts in that currency (assuming there are no "cents"!). You're almost getting down to amounts that are of interest to mere mortals.
Here is an example where a growth projection calculation starting from 1e9 at 5% goes wrong:
method year amount delta
double 0 $ 1,000,000,000.00
Decimal 0 $ 1,000,000,000.00 (0.0000000000)
double 10 $ 1,628,894,626.78
Decimal 10 $ 1,628,894,626.78 (0.0000004768)
double 20 $ 2,653,297,705.14
Decimal 20 $ 2,653,297,705.14 (0.0000023842)
double 30 $ 4,321,942,375.15
Decimal 30 $ 4,321,942,375.15 (0.0000057220)
double 40 $ 7,039,988,712.12
Decimal 40 $ 7,039,988,712.12 (0.0000123978)
double 50 $ 11,467,399,785.75
Decimal 50 $ 11,467,399,785.75 (0.0000247955)
double 60 $ 18,679,185,894.12
Decimal 60 $ 18,679,185,894.12 (0.0000534058)
double 70 $ 30,426,425,535.51
Decimal 70 $ 30,426,425,535.51 (0.0000915527)
double 80 $ 49,561,441,066.84
Decimal 80 $ 49,561,441,066.84 (0.0001678467)
double 90 $ 80,730,365,049.13
Decimal 90 $ 80,730,365,049.13 (0.0003051758)
double 100 $ 131,501,257,846.30
Decimal 100 $ 131,501,257,846.30 (0.0005645752)
double 110 $ 214,201,692,320.32
Decimal 110 $ 214,201,692,320.32 (0.0010375977)
double 120 $ 348,911,985,667.20
Decimal 120 $ 348,911,985,667.20 (0.0017700195)
double 130 $ 568,340,858,671.56
Decimal 130 $ 568,340,858,671.55 (0.0030517578)
double 140 $ 925,767,370,868.17
Decimal 140 $ 925,767,370,868.17 (0.0053710938)
double 150 $ 1,507,977,496,053.05
Decimal 150 $ 1,507,977,496,053.04 (0.0097656250)
double 160 $ 2,456,336,440,622.11
Decimal 160 $ 2,456,336,440,622.10 (0.0166015625)
double 170 $ 4,001,113,229,686.99
Decimal 170 $ 4,001,113,229,686.96 (0.0288085938)
double 180 $ 6,517,391,840,965.27
Decimal 180 $ 6,517,391,840,965.22 (0.0498046875)
double 190 $ 10,616,144,550,351.47
Decimal 190 $ 10,616,144,550,351.38 (0.0859375000)
The delta (difference between double
and BigDecimal
first hits > 1 cent at year 160, around 2 trillion (which might not be all that much 160 years from now), and of course just keeps getting worse.
Of course, the 53 bits of Mantissa mean that the relative error for this kind of calculation is likely to be very small (hopefully you don't lose your job over 1 cent out of 2 trillion). Indeed, the relative error basically holds fairly steady through most of the example. You could certainly organize it though so that you (for example) subtract two various with loss of precision in the mantissa resulting in an arbitrarily large error (exercise up to reader).
Changing Semantics
So you think you are pretty clever, and managed to come up with a rounding scheme that lets you use double
and have exhaustively tested your methods on your local JVM. Go ahead and deploy it. Tomorrow or next week or whenever is worst for you, the results change and your tricks break.
Unlike almost every other basic language expression and certainly unlike integer or BigDecimal
arithmetic, by default the results of many floating point expressions don't have a single standards defined value due to the strictfp feature. Platforms are free to use, at their discretion, higher precision intermediates, which may result in different results on different hardware, JVM versions, etc. The result, for the same inputs, may even vary at runtime when the method switches from interpreted to JIT-compiled!
If you had written your code in the pre-Java 1.2 days, you'd be pretty pissed when Java 1.2 suddenly introduces the now-default variable FP behavior. You might be tempted to just use strictfp
everywhere and hope you don't run into any of the multitude of related bugs - but on some platforms you'd be throwing away much of the performance that double bought you in the first place.
There's nothing to say that the JVM spec won't again change in the future to accommodate further changes in FP hardware, or that the JVM implementors won't use the rope that the default non-strictfp behavior gives them to do something tricky.
Inexact Representations
As Roland pointed out in his answer, a key problem with double
is that it doesn't have exact representations for some most non-integer values. Although a single non-exact value like 0.1
will often "roundtrip" OK in some scenarios (e.g., Double.toString(0.1).equals("0.1")
), as soon as you do math on these imprecise values the error can compound, and this can be irrecoverable.
In particular, if you are "close" to a rounding point, e.g., ~1.005, you might get a value of 1.00499999... when the true value is 1.0050000001..., or vice-versa. Because the errors go in both directions, there is no rounding magic that can fix this. There is no way to tell if a value of 1.004999999... should be bumped up or not. Your roundToTwoPlaces()
method (a type of double rounding) only works because it handled a case where 1.0049999 should be bumped up, but it will never be able to cross the boundary, e.g., if cumulative errors cause 1.0050000000001 to be turned into 1.00499999999999 it can't fix it.
You don't need big or small numbers to hit this. You only need some math and for the result to fall close to the boundary. The more math you do, the larger the possible deviations from the true result, and the more chance of straddling a boundary.
As requested here a searching test that does a simple calculation: amount * tax
and rounds it to 2 decimal places (i.e., dollars and cents). There are a few rounding methods in there, the one currently used, roundToTwoPlacesB
is a souped-up version of yours1 (by increasing the multiplier for n
in the first rounding you make it a lot more sensitive - the original version fails right away on trivial inputs).
The test spits out the failures it finds, and they come in bunches. For example, the first few failures:
Failed for 1234.57 * 0.5000 = 617.28 vs 617.29
Raw result : 617.2850000000000000000000, Double.toString(): 617.29
Failed for 1234.61 * 0.5000 = 617.30 vs 617.31
Raw result : 617.3050000000000000000000, Double.toString(): 617.31
Failed for 1234.65 * 0.5000 = 617.32 vs 617.33
Raw result : 617.3250000000000000000000, Double.toString(): 617.33
Failed for 1234.69 * 0.5000 = 617.34 vs 617.35
Raw result : 617.3450000000000000000000, Double.toString(): 617.35
Note that the "raw result" (i.e., the exact unrounded result) is always close to a x.xx5000
boundary. Your rounding method errs both on the high and low sides. You can't fix it generically.
Imprecise Calculations
Several of the java.lang.Math
methods don't require correctly rounded results, but rather allow errors of up to 2.5 ulp. Granted, you probably aren't going to be using the hyperbolic functions much with currency, but functions such as exp()
and pow()
often find their way into currency calculations and these only have an accuracy of 1 ulp. So the number is already "wrong" when it is returned.
This interacts with the "Inexact Representation" issue, since this type of error is much more serious than that from the normal mathematic operations which are at least choosing the best possible value from with the representable domain of double
. It means that you can have many more round-boundary crossing events when you use these methods.
When you round double price = 0.615
to two decimal places, you get 0.61 (rounded down) but probably expected 0.62 (rounded up, because of the 5).
This is because double 0.615 is actually 0.6149999999999999911182158029987476766109466552734375.
The main problems you are facing in practice are related to the fact that round(a) + round(b)
is not necessarily equal to round(a+b)
. By using BigDecimal
you have fine control over the rounding process and can therefore make your sums come out correctly.
When you calculate taxes, say 18 % VAT, it is easy to get values that have more than two decimal places when represented exactly. So rounding becomes an issue.
Lets assume you buy 2 articles for $ 1.3 each
Article Price Price+VAT (exact) Price+VAT (rounded)
A 1.3 1.534 1.53
B 1.3 1.534 1.53
sum 2.6 3.068 3.06
exact rounded 3.07
So if you do the calculations with double and only round to print the result, you would get a total of 3.07 while the amount on the bill should actually be 3.06.
Let's give a "less technical, more philosophical" answer here: why do you think that "Cobol" isn't using floating point arithmetic when dealing with currency?!
("Cobol" in quotes, as in: existing legacy approaches to solve real world business problems).
Meaning: almost 50 years ago, when people started using computers for business aka financial work, they quickly realized that "floating point" representation isn't going to work for the financial industry (maybe expect some rare niche corners as pointed out in the question).
And keep in mind: back then, abstractions were truly expensive! It was expensive enough to have a bit here and and a register there; and still it quickly become obvious to the giants on whose shoulders we stand ... that using "floating points" would not solve their problems; and that they had to rely on something else; more abstract - more expensive!
Our industry had 50+ years to come up with "floating point that works for currency" - and the common answer is still: don't do it. Instead, you turn to solutions such as BigDecimal.
You don't need an example. You just need fourth-form mathematics. Fractions in floating-point are represented in binary radix, and binary radix is incommensurable with decimal radix. Tenth grade stuff.
Therefore there will always be rounding and approximation, and neither is acceptable in accounting in any way, shape, or form. The books have to balance to the last cent, and so FYI does a bank branch at the end of each day, and the entire bank at regular intervals.
an expression suffering from round-off errors doesn't count'
Ridiculous. This is the problem. Excluding rounding errors excludes the entire problem.
Suppose that you have 1000000000001.5 (it is in the 1e12 range) money. And you have to calculate 117% of it.
In double, it becomes 1170000000001.7549 (this number is already imprecise). Then apply your round algorithm, and it becomes 1170000000001.75.
In precise arithmetic, it becomes 1170000000001.7550, which is rounded to 1170000000001.76. Ouch, you lost 1 cent.
I think that it is a realistic example, where double is inferior to precise arithmetic.
Sure, you can fix this somehow (even, you can implement BigDecimal using double arihmetic, so in a way, double can be used for everything, and it will be precise), but what's the point?
You can use double for integer arithmetic, if numbers are less than 2^53. If you can express your math within these constraints, then calculation will be precise (division needs special care, of course). As soon as you leave this territory, your calculations can be imprecise.
As you can see, 53 bits is not enough, double is not enough. But, if you store money in decimal-fixed point number (I mean, store the number money*100
, if you need cents precision), then 64 bits might be enough, so a 64-bit integer can be used instead of BigDecimal
.
Using BigDecimal would be most necessary when dealing with high value digital forms of currency such as cyprtocurrency (BTC, LTC, etc.), stocks, etc. In situations like these a lot of times you will be dealing with very specific values at 7 or 8 significant figures. If your code accidentally causes rounding error at 3 or 4 sig figs then the losses could be extremely significant. Losing money because of a rounding error is not going to be fun, especially if it's for clients.
Sure, you could probably get away with using a Double for everything if you made sure to do everything right, but it would probably be better to not take the risk, especially if you're starting from scratch.
Bottom Line up front:
Simple realistic example where double
fails:
All larger numerical types can be perfectly simulated by smaller numerical types using lists of the smaller number types and maintaining a record of things like sign and decimal place. Thus, a numeric type only fails when using it equates to higher code complexity, and/or slower speed.
BigDecimal
does not decrease code complexity by much when you know how to handle double
multiplications and divisions to avoid underflow. However, there may be situations where BigDecimal
is potentially faster than double
.
However, there should be no case where it is strictly
better (in the mathematical sense) than double. Why? because double
calculations are implemented as unit operations in modern processors (during one cycle), and therefore any efficient large-precision floating point calculation, at its foundation, either uses some kind of double-esque
numeric type, or is slower than optimal.
In other words, if a double is a brick, a BigDecimal is a stack of bricks.
So, first, let's define what "bad" means in the context of "double
is bad for financial analysis."
A double
floating point number is a list of binary states. Thus, if all you had access to were classes and 32 bit integers, you could "re-create" a double
simply by recording the position of the decimal, sign, etc. and maintaining a list of integers.
The downside of this process is that you would have a much more complex and buggy code base to manage this. Further, a double
is equal to the word size of a 64 bit processor, so calculations will be slower with your class containing a list of integers.
Now, computers are very fast. And unless you are writing sloppy code, you won't notice the difference between double
and your class with its list of integers for O(n) operations (one for loop).
Thus, the main issue here is written code complexity (complexity of use, reading, etc.).
Since code complexity is the main issue, consider a financial situation where you are multiplying fractions through many times.
This can cause underflow, which is the rounding error you are talking about.
The fix for underflow is to take the log:
// small numbers a and b
double a = ...
double b = ...
double underflowed_number = a*pow(b,15); // this is potentially an inaccurate calculation.
double accurate_number = pow(e,log(a) + 15*log(b)); // this is accurate
Now, the question is: is this too much code complexity for you to handle?
Or, better yet: Is it too much complexity for your fellow employees to handle? Will someone come along and say: "wow, this looks really inefficient, I'll just change it back to a*pow(b,15)
"?
If yes, then just use BigDecimal
; otherwise: double
will, with the exception of the underflow calculation, be lighter weight in terms of use and syntax...and the written code complexity really isn't that big of a deal anyways.
With one caveat: if you are doing significant calculations involving the underflow workaround in a computationally complex setting, such as a nested for loop on some inner subroutine running on a bank's back end, then you ought to test using BigDecimal
, as this may actually be faster.
So the answer to your question:
// at some point, for some large_number this *might* be slower,
// depending on hardware, and should be tested:
for (i=1; i<large_number; i++){
for(j=1;j<large_number;j++){
for(k=1;k<large_number;k++){
// switched log to base 2 for speed
double n = pow(2,log2(a) + 15*log2(b));
}
}
}
// this *might* be faster:
for (i=1; i<large_number; i++){
for(j=1;j<large_number;j++){
for(k=1;k<large_number;k++){
BigDecimal n = a * pow(b,15);
}
}
}
I'll add an asymptotic plot if I have time.
The following would appear to be a decent implementation of a method that needed to "round down to the nearest penny".
private static double roundDowntoPenny(double d ) {
double e = d * 100;
return ((int)e) / 100.0;
}
However, the output of the following shows that the behavior isn't quite what we expect.
public static void main(String[] args) {
System.out.println(roundDowntoPenny(10.30001));
System.out.println(roundDowntoPenny(10.3000));
System.out.println(roundDowntoPenny(10.20001));
System.out.println(roundDowntoPenny(10.2000));
}
Output:
10.3
10.3
10.2
10.19 // Not expected!
Of course, a method can be written which produces the output that we want. The problem is that it actually very difficult to do so (and to do so in every place where you need to manipulate prices).
For every numeral-system (base-10, base-2, base-16, etc.) with a finite number of digits, there are rationals that cannot be stored exactly. For example, 1/3 cannot be stored (with finite digits) in base-10. Similarly, 3/10 cannot be stored (with finite digits) in base-2.
If we needed to chose a numeral-system to store arbitrary rationals, it wouldn't matter what system we chose - any system chosen would have some rationals that couldn't be stored exactly.
However, humans began assigning prices to things way before the development of computer systems. Therefore, we see prices like 5.30 rather that 5 + 1/3. For example, our stock exchanges use decimal prices, which mean that they accept orders, and issue quotes, only in prices that can be represented in base-10. Likewise, it means that they can issue quotes and accept orders in prices that cannot be accurately represented in base-2.
By storing (transmitting, manipulating) those prices in base-2, we are essentially relying on rounding logic to always correctly round our (in-exact) base-2 (representation of) numbers back to their (exact) base-10 representation.
来源:https://stackoverflow.com/questions/42871564/a-realistic-example-where-using-bigdecimal-for-currency-is-strictly-better-than