<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
I found this one some time ago and reported it. Guido van Rossum and
Tim Peters followed up on this one. It is still open as of 27 June 2000.
<p><a href="http://www.python.org/python-bugs/open?id=245;">http://www.python.org/python-bugs/open?id=245;</a>
<p>print eval("float(1.0e-309)") # Ok == 0
<br>print float("1.0e-309") # ValueError: float() literal too large: 1.0e-309
<p>Apparently this is due to a difference between the C implementations
of strtod() and atof() on some platforms.
<p>The quick fix I used was:
<p><tt> # s is string</tt>
<br><tt> try:</tt>
<br><tt>
value = float(s)</tt>
<br><tt> except ValueError:</tt>
<br><tt>
import sys</tt>
<br><tt>
if sys.platform == 'win32' and sys.version[:5] == "1.5.2":</tt>
<br><tt>
# floating point values smaller than DBL_MIN</tt>
<br><tt>
# cause an exception</tt>
<br><tt>
# approx 2.2e-308 and smaller</tt><tt></tt>
<p><tt>
# according to Guido van Rossum this occurs on Solaris too.</tt>
<br><tt>
# the method that causes the exception used strtod()</tt>
<br><tt>
# whereas the following uses atof()</tt>
<br><tt>
value = eval("float(" + s + ")")</tt>
<br><tt>
else:</tt>
<br><tt>
# behaviour may be different on other platforms/versions</tt>
<br><tt>
raise</tt>
<p>I did this so that I do not forget to change it when the Python version
changes.
<br>
<p>Stephen D Evans
<br>stevee@recombinant.demon.co.uk
<br>
<p>"Victor S. Miller" wrote:
<blockquote TYPE=CITE>I just wrote a python program to read output produced
by a Fortran
<br>program that somebody had written. Much to my surprise I got
the
<br>exception:
<p>float() literal too large.
<p>The literal in question was '0.23489798479873E-2325' (this was
<br>generated on a Cray computer). I couldn't find anything in the
<br>documentation about this restriction. Even more puzzling was
the fact
<br>that eval() worked just fine (it gave the answer 0.0 which is
<br>perfectly acceptable to me, given that the platform on which I was
<br>running had less precision than the platform that generated it).
Now,
<br>on general grounds I would rather convert this using float, because
I
<br>had a statement like
<p>a[i,j] = float(s)
<p>where a is a numerical python array. So, is there some way around
<br>this? How do I find out what the restrictions on literals are?
<p>I'm running:
<p>Python 1.5.2 (#4, Nov 23 1999, 14:57:03) [GCC 2.7.2.2] on sunos5
<br>--
<br>Victor S. Miller | " ... Meanwhile, those of
us who can compute can hardly
<br>victor@idaccr.org | be expected to keep writing papers
saying 'I can do the
<br>CCR, Princeton, NJ | following useless calculation in 2
seconds', and indeed
<br> 08540 USA
| what editor would publish them?" -- Oliver Atkin</blockquote>
</html>