Re: Working with Cash and uses of Typedef



On Apr 16, 3:15 pm, Flash Gordon <s...@xxxxxxxxxxxxxxxxxx> wrote:
Morris Dovey wrote, On 16/04/08 23:38:





Flash Gordon wrote:
Morris Dovey wrote, On 16/04/08 16:03:
Philluminati wrote:
I am writing a poker game which needs to work with cash. I am aware
that there are problems with floats that make them unsuitable for
storing money values. On top of this I may be expected to do
operations such as £10.52 / 3. Which data type should I use instead?
Is there an Industry standard one?
Depending on the range of values you need to work with, use
either float or double.
That is asking for trouble, especially with someone inexperienced enough
to be asking the question.

Hopefully not. What /might/ be asking for trouble is for an
inexperienced programmer to implement any kind of financial
(sub)system without help from experienced folks...

Well, we are talking about someone working on a system dealing with
monetary values...

The OP should store the values as pennies in an int or long depending on
the required range (I'm assuming that negative money is valid as it
often is).

When the Philadelphia Stock Exchange added decimal trading in
2001, there was considerable debate over currenty representation.
After a _lot_ of really nit-picky analysis, we settled on double.

I know it can be done, I also know how easy it is to hit problems...

Before bringing the decimal trading on line, we stress tested all
the standard library functions that worked on or returned
doubles, and we _did_ find bugs - Philluminati beware!

and you did hit problems :-)

I'm sure you also worked out how to handle them appropriately.

As evidence we already have a disagreement :-)

Not really - I'm perfectly happy for you to represent cash
however it works for you. May you have enough of it to find cause
to worry about the number of significant digits in a double! :-D

That's not a problem, if I hit that level I will be able to afford to
buy a computer with more significant digits and get the SW written for
it :-)

People are sometimes surprised when adding a column of numbers
forwards and backwards gives different answers (as floating point
definitely will for non-integral totals).

For a GAAP accounting package, I would definitely use decimal
arithmetic, if possible.

For some kinds of financial operations exact reproducability is very
important (e.g. 'how much money is in my account'). For others,
perhaps not as important (e.g. according to this derivative, we should
buy pork belly futures).

There is no magic bullet for financial operations, and I would
certainly expect anyone who is handling *my* money to be absurdly
careful to do everything as precisely and correctly as possible.
.



Relevant Pages


Loading