How to write string (float) to file?

Bengt Richter bokr at accessone.com
Tue Aug 28 20:11:08 EDT 2001


On Tue, 28 Aug 2001 18:43:32 -0400, "Steve Holden" <sholden at holdenweb.com> wrote:

>"Rainer Deyke" <root at rainerdeyke.com> wrote in message
>news:x5Ui7.53338$c8.23437612 at news1.denver1.co.home.com...
[...]
>>
>> If you're that picky about accuracy, floats are definitely the wrong
>choice.
>>
>Wrong. Tim isn't being picky about accuracy, he's being picky about
>correctly rounding numbers to the required number of decimal places. If a
>number is 6.66666 (plus or minus noise beyond ten decimal places) there is
>no system which makes 6.66 the correct answer to 2 D.P.
>
>> >>> '%.2f' % (0.005)
>> '0.01'
>> >>> '%.2f' % (0.015)
>> '0.01'
>> >>> '%.2f' % (0.025)
>> '0.03'
>>
>Perfect, and just what a numerical analyst would require: alternate cases
>that are exactly at the midpoint round in opposite directions. [Thouhg in
>fact it's a bit lucky, and I seem to remember the rule I was taught back in
>the days when you did this sort of thing on a motor-driven calculator said
>round up if the digit before 5 is evan, down if it's odd. Since this is
>almost 30 years on, I'm sure you'll forgive me if I'm wrong).
>
Well, the problem is fp literals are not necessarily as exact as they look:

 >>> `0.005`
 '0.0050000000000000001'
 >>> `0.015`
 '0.014999999999999999'
 >>> `0.025`
 '0.025000000000000001'

The essential problem is transforming representations into elements of
another set that doesn't match 1:1 all over both sets of representations.





More information about the Python-list mailing list