pricision of string.atof?

Bjorn Pettersen BPettersen at NAREX.com
Tue May 7 16:37:31 EDT 2002


> From: Elvis Chen [mailto:chene at cs.queensu.ca] 
> 
> 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

This has to do with your floating point hardware and how numbers are
represented to you in the interpreter (just typing A will call A's
__repr__, typing "print A" will call A's __str__ method).

For all the gory details see:
http://www.python.org/doc/current/tut/node14.html

-- bjorn





More information about the Python-list mailing list