开发者

What is the right data type for calculations in Java

开发者 https://www.devze.com 2023-01-02 12:42 出处:网络
Should we use double or BigDecimal for calculations in Ja开发者_JAVA百科va? How much is the overhead in terms of performance for BigDecimal as compared to double?For a serious financial application B

Should we use double or BigDecimal for calculations in Ja开发者_JAVA百科va?

How much is the overhead in terms of performance for BigDecimal as compared to double?


For a serious financial application BigDecimal is a must.

Depends on how many digits you need you can go with a long and a decimal factor for visualization.


For general floating point calculations, you should use double. If you are absolutely sure that you really do need arbitrary precision arithmetic (most applications don't), then you can consider BigDecimal.

You will find that double will significantly outperform BigDecimal (not to mention being easier to work with) for any application where double is sufficient precision.

Update: You commented on another answer that you want to use this for a finance related application. This is one of the areas where you actually should consider using BigDecimal, otherwise you may get unexpected rounding effects from double calculations. Also, double values have limited precision, and you won't be able to accurately keep track of pennies at the same time as millions of dollars.


How much is the overhead in terms of performance for BigDecimal as compared to double?

A lot. For example, a multiplication of two doubles is a single machine instruction. Multiplying two BigDecimals is probably a minimum of 50 machine instructions, and has complexity of O(N * M) where M and N are the number of bytes used to represent the two numbers.

However, if your application requires the calculation to be "decimally correct", then you need to accept the overhead.

However (#2) ... even BigDecimal can't do this calculation with real number accuracy:

       1/3 + 1/3 + 1/3  ->  ?

To do that computation precisely you would need to implement a Rational type; i.e. a pair of BigInteger values ... and some thing to reduce the common factors.

However (#3) ... even a hypothetical Rational type won't give you a precise numeric representation for (say) Pi.


As always: it depends.

If you need the precision (even for "small" numbers, when representing amounts for example) go with BigDecimal.

In some scientific applications, double may be a better choice.


Even in finance we can't answer without knowing what area. For instance if you were doing currency conversions of $billions, where the conversion rate could be to 5 d.p. you might have problems with double. Whereas for simply adding and subtracting balances you'd be fine.

If you don't need to work in fractions of a cent/penny, maybe an integral type might be more appropriate, again it depends on the size of numbers involved.

0

精彩评论

暂无评论...
验证码 换一张
取 消