BigInteger is capable of holding far bigger numbers than Long. BigInteger seems capable of holding (2 ^ 32) ^ Integer.MAX_VALUE, though that depends on the implementation (and, even if truly unbounded in the implementation, there will eventually be a physical resource limit) See explanation here.
The range of Long is [-9,223,372,036,854,775,808, +9,223,372,036,854,775,807].
Long will perform better than BigInteger so it really depends on what the size of your values will be. If they would all fall under Long's max value, it makes no sense not to use Long. If any would be bigger than the Long max value, you pretty much have to use BigInteger.
Answer from AHungerArtist on Stack OverflowEveryone 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.
java - Long vs BigInteger - Stack Overflow
java - Double vs. BigDecimal? - Stack Overflow
optimization - What to do with Java BigDecimal performance? - Stack Overflow
type conversion - Java BigDecimal data converting to opposite sign long - Stack Overflow
A BigDecimal is an exact way of representing numbers. A Double has a certain precision. Working with doubles of various magnitudes (say d1=1000.0 and d2=0.001) could result in the 0.001 being dropped altogether when summing as the difference in magnitude is so large. With BigDecimal this would not happen.
The disadvantage of BigDecimal is that it's slower, and it's a bit more difficult to program algorithms that way (due to + - * and / not being overloaded).
If you are dealing with money, or precision is a must, use BigDecimal. Otherwise Doubles tend to be good enough.
I do recommend reading the javadoc of BigDecimal as they do explain things better than I do here :)
My English is not good so I'll just write a simple example here.
double a = 0.02;
double b = 0.03;
double c = b - a;
System.out.println(c);
BigDecimal _a = new BigDecimal("0.02");
BigDecimal _b = new BigDecimal("0.03");
BigDecimal _c = _b.subtract(_a);
System.out.println(_c);
Program output:
0.009999999999999998
0.01
Does anyone still want to use double? ;)
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.
It is possible whenever the value of the BigDecimal is larger than what a long can hold.
Example:
BigDecimal num = new BigDecimal(Long.MAX_VALUE);
System.out.println(num); // prints: 9223372036854775807
System.out.println(num.longValue()); // prints: 9223372036854775807
num = num.add(BigDecimal.TEN); // num is now too large for long
System.out.println(num); // prints: 9223372036854775817
System.out.println(num.longValue()); // prints: -9223372036854775799
System.out.println(num.longValueExact()); // throws: ArithmeticException: Overflow
I will happen if the value is greater than the max value of long
BigDecimal dec = new BigDecimal(Long.MAX_VALUE +1);
System.out.println(dec.longValue());
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