Everyone says that BigDecimal should be used when dealing with money but it’s much slower and takes more memory than double. I would think this would be especially important in high frequency low-latency applications like trading. Do people actually use BigDecimal in such systems or do they use doubles with some kind of workaround to handle the precision issue?
Edit: I do have experience working on trading and risk systems and I see doubles used much more often than BigDecimal so I was curious to see if this is more common in actual practice. Most of the systems I worked on only need precision to the penny so I wonder if that’s the reason?
Also, BigDecimal is a pain to use and code written with it look much uglier than plain doubles.
Use long or big decimal. Performance problems will most probably arise elsewhere before you factor a big decimal as the culprit
Open jshell and type:
0.1 + 0.2
Now tell me if it's OK to use double for storing money values.
I know it is better to use BigDecimal for much better precision when calculating money, but what if I am only calculating a few hundred thousands with four decimal points? Is it still better to use BigDecimal?
What is the range of money when I should really use BigDecimal?
Title is pretty much the question. If big-decimals are more accurate than doubles, why not just exclusively use them instead?
I’ve tried looking for definitive answers but the only difference I see seems to be based on the precision of the numbers?
Which data type is larger and which one is smaller?
May be you should start with replacing a = (1/b) * c with a = c/b ? It's not 10x, but still something.
If I were you, I'd create my own class Money, which would keep long dollars and long cents, and do math in it.
Most double operations give you more than enough precision. You can represent $10 trillion with cent accuracy with double which may be more than enough for you.
In all the trading systems I have worked on (four different banks), they have used double with appropriate rounding. I don't see any reason to be using BigDecimal.
I come from the Java world. Worked a lot in fintech. I used BigDecimal all over the place for calculations when it came to money. Is there a library you use for precise numerical calculations?
From: http://www.javamex.com/tutorials/math/BigDecimal_BigInteger_performance.shtml
Note that a BigDecimal is essentially a wrapper around a BigInteger that "remembers where the decimal point is".
Because of the way we are used to dealing with numbers, as humans, we may have to "think a bit more" when calculating with non-integers by hand compared to integers (e.g. we may have learnt the 7 times table by heart to speed up certain calculations, but probably not the .7 times table). But to a computer implementation, it is essentially no more effort to manipulate non-integers than it is integers, and so on the whole, methods on BigDecimal tend to perform similarly to analogous methods on BigInteger.
I know the title of your question states "Performace of BigDecimal vs. BigInteger and BigDecimal" but one of your questions states "How about using only primitive data types and BigInteger or only BigInteger to make ". Well here is what I recommend:
If you need accuracy, precision and you have large numbers to work with such as huge financial data then it may be best to use BigDecimal and BigInteger instead of primitive types. Few other characteristics of BigDecimal and BigInteger are also:
- Both are from immutable objects.
- Both extend from Number class and implement comparable interface
Now back to your main question in terms of performance, there is not much of difference in terms of performance between the two. If you can present your information in BigDecimal just use that so you don't have to convert it over to BigInteger but again look at the program and see if it does make sense to only use BigDecimal all the time. The main difference between the BigInteger and BigDecimal is that BigInteger supports arbitrary-precision integers and BigDecimal is for arbitrary-precision fixed-point numbers.
Here are my sources and good luck :)
NY University
Colorado University