JeremyWildsmith / bazaarbot-java

Reimplementation of bazaarbot, translated from the dotnet translation https://github.com/Vibr8gKiwi/bazaarBot2 ; originally implementation was in haxe by @larsiusprime (https://github.com/Vibr8gKiwi/bazaarBot2)
MIT License
5 stars 2 forks source link

Double to BigDecimal proposal #10

Closed DigitalSmile closed 4 years ago

DigitalSmile commented 5 years ago

Usually if you are dealing with money, it is recommended to use BigDecimal instead of double. It is a bit slower, but provides a far more accuracy when you are doing calculations.

https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html

JeremyWildsmith commented 5 years ago

I agree; I can see for larger simulations the inaccuracies of doubles could build up.

guillaume-alvarez commented 4 years ago

BigDecimal may be accurate but will destroy performances... I work on trading software (including order books) and it is strictly forbidden to use it for this specific reason. If you want to avoid inaccuracies in prices you should use integer prices and a number of decimals for display (like counting in cents and converting to dollars for UI only). Please note it is also faster than double.

DigitalSmile commented 4 years ago

Well, trading software has very different requirements. I do not believe we need the same level of performance (1-2ms latency) in the simulations of that kind, but accuracy is a key. And what you described is somewhat BigDecimal is :) But with sanity checks and a lot of useful overhead. We can, of course, create a class with similar functions, but I do not think we will be able to support it with bugfree levels of java core.

Let's hope in nearest versions of java someone will propose BigDecimal with steroids and intrinsics for faster calculations :)

guillaume-alvarez commented 4 years ago

If you want to use it in game then I think you have similar requirements to Java trading application, not talking about native high frequency trading ;-)

The main cost of BigDecimal is not latency, it is memory and garbage collection. This class instantiates arrays and thus has a huge overhead. :-( It is really fine for scientific calculations where you have a few variables, or accounting where precision is paramount and execution happens in daily batches, not to compute interactions between many near-real-time agents.

FYI most trading software keep using double anyway, because the errors are not that big as long as the number remain in the same range, for instance you do not try to add 10e10 and 10e-9 and it is more intuitive. So it should not be an issue in this case, it allows for a about 15 significant decimal numbers.

DigitalSmile commented 4 years ago

So, that's why our economy is so f***ed nowadays, doubles... :))) Just joking Still I do not think BigDecimal can be an issue within small indie game... But if you feel that BigDecimal can be substitute with improved classes, without too much pain and the same accuracy, you are welcome to contribute!

DigitalSmile commented 4 years ago

I am closing this issue, if you feel we need to move forward with something more than BigDecimal, feel free to open specified issue.