INET-Complexity / Core-ESL

Open-source, distributed, economic simulation libraries for Java and Scala
7 stars 6 forks source link

Finite precision problems #15

Open rafabap opened 7 years ago

rafabap commented 7 years ago

In the stress testing model we have started to encounter problems due to finite precision, since we use double for the value of all contracts and as the unit of cash.

We run into problems whenever we perform comparisons, due to the accumulated errors from adding and subtracting quantities.

At the moment the most sensible solution would seem to be using int for the value of everything. Any better suggestions welcome!

rafabap commented 7 years ago

@davidrpugh I was wondering whether you could help? The only solutions I can come up with are switching to int, using some Java class that deals with rounding errors such as BigDecimal (which in practice, is equivalent to using integer arithmetic) or guarding against the final precision inside all comparisons in the code (especially when comparing against zero). Do you know if there is a good standard solution to these problems?

davidrpugh commented 7 years ago

@rafabap I have encountered exactly the same issue in the auctions library and in agora. @bherd-rb and I discussed this at length and settled on using Long in all of our calculations. Using BigDecimal would entail taking a significant performance hit. @DavoudTaghawiNejad might have thoughts on this...

davidrpugh commented 7 years ago

@rafabap Came across this link which has suggestions for performing efficient financial calculations...

http://java-performance.com/