Is using floating point type for money/currency a good idea?

Era Scarecrow rtcvb32 at yahoo.com
Mon May 20 15:19:19 UTC 2019


On Monday, 20 May 2019 at 12:50:29 UTC, Simen Kjærås wrote:
> If you're worried that $92_233_720_368_547_758.07 (long.max) is 
> not enough money for your game, I'd note that the entire 
> current world economy is about a thousandth of that. Even so, 
> there's std.bigint.BigInt, which has no set limit, and can in 
> theory represent every whole number up to about 256^(2^64), or 
> about 4 quintillion digits. You will encounter other problems 
> before this limit becomes an issue.

  Yes at that point BigInt would be a better solution.

  I made a NoGC fixed int type that would allow you to have any 
sized int (once defined, Cent anyone?) and only use stack 
data/space for calculations; It did fairly decently 
performance-wise (still need to do the special assembly 
instructions for supported 128/256 cryto stuff for faster speed 
on supported hardware), but otherwise it worked. Truthfully 
working around with the registers and carry and other details can 
be a chore, though only on divide, everything else is 
straightforward.

  Hmmmm going with numbers very very very very large would 
probably be more idle games more than anything else. Though I'm 
not sure if many need actual precision (as after you're over say 
E+10 over what you used to have, what you buy isn't really 
relevant til it starts getting only e+4 off; So even rounding 
errors wouldn't matter much. Not sure what format those games 
use. Tempted to believe it's a two pair int (one for exponent and 
one for currency, letting you get E+4billion, would be easy to 
code and do calculations overall, i don't see BigInts being used 
everywhere)


More information about the Digitalmars-d-learn mailing list