Vault

Vault

7M Downloads

Using double for currency amount should be replaced by decimal type

jkobus opened this issue ยท 3 comments

commented

Using double creates inflation, as the amount of in-game money is increasing. Its very hard to track where the leak actually occurs, but that is a good starting point.

http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency

commented

Not even going to go into this one. This is not going to change. It's also why there's a num significant digits method. In addition double accuracy seems fine:
Directly from stack-overflow
"If you want an accuracy of +/-0.5 (or 2^-1), the maximum size that the number can be is 2^52. Any larger than this and the distance between floating point numbers is greater than 0.5.

If you want an accuracy of +/-0.0005 (about 2^-11), the maximum size that the number can be is 2^42. Any larger than this and the distance between floating point numbers is greater than 0.0005."

If you have issues with doubles you should probably use smaller values across the board for your economy interactions, or find an economy implementation that handles rounding better. Vault doesn't do any rounding at all itself. the NumSignificantDigits method specifically allows plugins to check this from Vault to behave properly and round more accurately.

commented

I'm using BigDecimal right now and I have to convert it to double just because Vault interface forces me to do so. It's a bad practice and a bad example for beginners.

commented

@jkobus the interface is in double. It can't be changed without breaking every plugin also, you're the only plugin I've heardof that uses BigDecimal. As I mentioned earlier, NumSignificantDigits allows to get around this issue for the most part.