strings (dollar.cents) into floats

Steve Holden steve at holdenweb.com
Fri Aug 31 14:37:40 EDT 2007


Chris Mellon wrote:
> 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.

There is much in what you say (which, I am happy to note, doesn't really 
contradict much of what I wrote). While I agree that Excel isn't a 
fundamental component of back-end financial systems you might be 
surprised at just how many people rely on its "currency" cells for 
financial applications, albeit of a more lightweight kind.

You let yourself down just a little right at the end by not knowing the 
intricacies of the old sterling currency - since there were twenty 
shillings to the pound and twelve pennies to a shilling that would have 
been £1.2. However, you are correct in assuming that nobody tried to 
represent £1 3s. 4d. as 23.333... shillings.

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list