float literal too large

Stephen D Evans stevee at recombinant.demon.co.uk
Tue Jun 27 13:58:03 EDT 2000


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.

http://www.python.org/python-bugs/open?id=245;

print eval("float(1.0e-309)") # Ok == 0
print float("1.0e-309") # ValueError: float() literal too large: 1.0e-309

Apparently this is due to a difference between the C implementations of strtod()
and atof() on some platforms.

The quick fix I used was:

        # s is string
        try:
            value = float(s)
        except ValueError:
            import sys
            if sys.platform == 'win32' and sys.version[:5] == "1.5.2":
                # floating point values smaller than DBL_MIN
                # cause an exception
                # approx 2.2e-308 and smaller

                # according to Guido van Rossum this occurs on Solaris too.
                # the method that causes the exception used strtod()
                # whereas the following uses atof()
                value = eval("float(" + s + ")")
            else:
                # behaviour may be different on other platforms/versions
                raise

I did this so that I do not forget to change it when the Python version changes.


Stephen D Evans
stevee at recombinant.demon.co.uk


"Victor S. Miller" wrote:

> I just wrote a python program to read output produced by a Fortran
> program that somebody had written.  Much to my surprise I got the
> exception:
>
> float() literal too large.
>
> The literal in question was '0.23489798479873E-2325' (this was
> generated on a Cray computer).  I couldn't find anything in the
> documentation about this restriction.  Even more puzzling was the fact
> that eval() worked just fine (it gave the answer 0.0 which is
> perfectly acceptable to me, given that the platform on which I was
> running had less precision than the platform that generated it).  Now,
> on general grounds I would rather convert this using float, because I
> had a statement like
>
> a[i,j] = float(s)
>
> where a is a numerical python array.  So, is there some way around
> this?  How do I find out what the restrictions on literals are?
>
> I'm running:
>
> Python 1.5.2 (#4, Nov 23 1999, 14:57:03)  [GCC 2.7.2.2] on sunos5
> --
> Victor S. Miller     | " ... Meanwhile, those of us who can compute can hardly
> victor at idaccr.org    | be expected to keep writing papers saying 'I can do the
> CCR, Princeton, NJ   | following useless calculation in 2 seconds', and indeed
>     08540 USA        | what editor would publish them?"  -- Oliver Atkin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20000627/bb552712/attachment.html>


More information about the Python-list mailing list