pricision of string.atof?
Fernando PĂ©rez
fperez528 at yahoo.com
Tue May 7 16:47:27 EDT 2002
Elvis Chen wrote:
>
> Greetings,
>
> I'm working on some numerical analysis that requires rather good precision
> on calculation. In my project, I need to read some numbers from a file
> and subsequently process it to give other results. However, I found that
> string.atof doesn't always convert the read string to equivalent number.
>
> For example, if I have
>
> s = 'A 0.424\n'
> splitline = string.split( s )
> A = string.atof( splitline[1] )
>
> then,
>
>>>> A
> 0.42399999999999999
>>>> print A
> 0.424
I know this topic gets beaten to death about once a week, but here it goes
once more. It has nothing to do with atof():
In [1]: a=0.424
In [2]: a
Out[2]: 0.42399999999999999
In [3]: print a
0.424
It's simply that repr() gives a more faithful representation of the internal
form of a value while str() does some rounding for visual convenience. And
since not _all_ real numbers can be represented via 32 bit floats (2^32 <<
infinity), some values by necessity have an approximate representation.
Any decent text on numerical analysis discusses this in detail.
cheers,
f.
More information about the Python-list
mailing list