How to write string (float) to file?

Steve Holden sholden at holdenweb.com
Wed Aug 29 16:06:45 EDT 2001


"Rainer Deyke" <root at rainerdeyke.com> wrote in message
news:G89j7.54140$c8.24141594 at news1.denver1.co.home.com...
[ ... ]
>
> > >>>> foo(10000000000000000000000000000000000000000l, 2)
> > > '5000000000000000200000000000000000000000.00'
>                      ^
>
> Not just incorrect, but the incorrect digit is right in the middle of the
> number, not in the least significant place.
>
Well by my count it occurs in the seventeenth decimal place, which given the
precision on floats is about what you ought to expect. Clearly if you need
to deal with numbers of arbitrary precision you are going to *have* to
represent them as longs and keep a scaling factor (or use some other similar
technique).

However, I would say that for most normal accounting purposes (i.e. numbers
up to the tens of millions, significant to 2 DP, or ten significant figures)
floating point numbers will be perfectly satisfactory *provided roundoff
error is controlled at each step*. [This will also be good enough for the
OMB, since they seem to round everything to the nearest million anyway :-)]

In practice this means rounding to 2 D.P. after each calculation and before
storing the result. Working to ten significant figures means that
representational errors in the seventeenth decimal place simply aren't going
to propagate into the portion of the number you are using to represent your
money.

Being aware of representational problems is all very well, but at the same
time you don't want to make a rod for your own back by adopting
unnecessarily complicated solutions when the tools ready to hand will do
what you want.

As a sidenote, I learned about accountant's attitudes to accuracy when I
worked on my first freelance job, converting a single-currency ledger system
to multi-currency. Working on the Japanese Yen gave some very large figures
for invoice totals, and early checks revealed that invoices over a million
Yen could be out by anything up to 0.25 Yen, due to my inattention to
accumulation of the aforementioned rounding errors. In my youthful innocence
(alas, long gone) I asked the accountant whether this was a problem.

I still remember the glacial silence while he took the time to think of a
suitably sarcastic reply. In the end he simply said "Of course it bloody
well is, what kind of an outfit do you think we're running here?".

since-when-i've-been-careful-about-monetary-computations-ly y'rs  - steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list