python 2.3.4 for windows: float("NaN") throws exception

Fredrik Lundh fredrik at pythonware.com
Thu Jan 13 10:26:08 EST 2005


<asmirnov1234567890 at yahoo.com> wrote:

> my python 2.3.4 for windows refuse to execute line float("NaN"). It
> says:
>
>>>> float("NaN")
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> ValueError: invalid literal for float(): NaN
>
> The same line works as expected on Linux and Solaris with python 2.3.4.
> Could anybody explain what is possibly wrong here? is it bug or
> feature?

feature, sort of.  see http://www.python.org/peps/pep-0754.html :

    "Currently, the handling of IEEE 754 special values in Python
    depends on the underlying C library. Unfortunately, there is little
    consistency between C libraries in how or whether these values
    are handled. For instance, on some systems "float('Inf')" will
    properly return the IEEE 754 constant for positive infinity. On
    many systems, however, this expression will instead generate
    an error message."

the PEP includes a pointer to a module that lets you replace that "float"
with the fully portable:

    import fpconst

    def myfloat(x):
        if x == "NaN":
            return fpconst.NaN
        return float(x)

</F> 






More information about the Python-list mailing list