strings (dollar.cents) into floats

Steve Holden steve at holdenweb.com
Fri Aug 31 12:06:49 EDT 2007


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.

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.

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