strings (dollar.cents) into floats

Chris Mellon arkanes at gmail.com
Fri Aug 31 12:28:11 EDT 2007


On 8/31/07, Steve Holden <steve at holdenweb.com> wrote:
> sturlamolden wrote:
> > On 31 Aug, 02:12, Wildemar Wildenburger
> > <lasses_w... at klapptsowieso.net> wrote:
> >
> >> I've heard (ok, read) that several times now and I understand the
> >> argument. But what use is there for floats, then? When is it OK to use them?
> >
> > There are fractions that can be exactly represented by floats that
> > cannot be exactly represented by decimals.
>
> Would you care to give an example?
>
> > There are fractions that
> > can be exactly represented by decimals that cannot be exactly
> > represented by floats.
> >
> > Which one is better? Which do we prefer?
> >
> > What a float cannot do is to represent a decimal fractional number
> > (e.g. 1.1) exactly. If we need that, we cannot use floats. A notable
> > example is monetary computations, it covers 99% of the use for decimal
> > numbers in computers. For this reason, we should never use floats to
> > add 10 cents to a dollar. The use of decimals for monetary
> > calculations is mandatory.
> >
> That last sentence is patent nonsense, and completely untrue. Many
> satisfactory financial applications have been written using only
> floating-point arithmetic. Indeed I believe the accountant's Swiss army
> knife, the Excel spreadsheet, uses floating-point numbers exclusively.
>
This is true, although Excel munges it's FP to provide "expected" results.

It depends on what you consider a "financial" application though.
Excel, while in extremely broad use, is not used to implement any of
the systems which actually "define" money, like the back end financial
systems at banks and credit unions.

In my experience, by far the most common method of calculating
financial numbers is actually using integer amounts, and then applying
well-defined rounding rules which I can't be bothered to look up the
name for.

For what it's worth, the work that I do with money (which is
middleware doing data transport, not calculations or billing) uses
either string representations or fixed point.

> What you say about floating-point have speed advantages is true, but you
> go too far in claiming that decimal arithmetic is mandatory for monetary
> calculations. That's about as sensible as saying that base 12 and base
> 20 arithmetic units were required to calculate in pounds, shillings and
> pence.
>

I believe that to the degree that "real" accounting was done in those
currencies it did in fact use non-decimal bases. Just as people don't
use decimal time values (except us crazy computer folk), you're write
1 pound 4 shillings, not 1.333... pounds.



More information about the Python-list mailing list