This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Float patch for inf and nan on Windows (and other platforms)
Type: Stage:
Components: Versions: Python 3.0, Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder: Enhancements for mathmodule
View: 1640
Assigned To: christian.heimes Nosy List: Rhamphoryncus, christian.heimes, gvanrossum, tim.peters
Priority: normal Keywords: patch

Created on 2007-12-15 10:23 by christian.heimes, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
trunk_float_inf_nan.patch christian.heimes, 2007-12-15 10:23
trunk_float_inf_nan2.patch christian.heimes, 2007-12-15 20:54
Messages (12)
msg58661 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2007-12-15 10:23
The patch unifies the creation and representation of "inf", "-inf" and
"nan" on all platforms. 

>>> float("inf")
inf
>>> float("-inf")
-inf
>>> float("nan")
nan
>>> repr(1e300 * 1e300)
'inf'
>>> repr(1e300 * 1e300 * 0)
'nan'
>>> repr(1e300 * 1e300 * -1)
'-inf'
msg58664 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2007-12-15 20:54
Update:

I've added a platform independent implementation of stricmp and strnicmp
(case insensitive compare). It could be useful for other parts of the
code as well.
msg58697 - (view) Author: Adam Olsen (Rhamphoryncus) Date: 2007-12-17 17:37
You have:
#define Py_NAN Py_HUGE_VAL * 0
I think this would be safer as:
#define Py_NAN (Py_HUGE_VAL * 0)

For instance, in code that may do "a / Py_NAN".

Those manual string copies (*cp++ = 'n';) are ugly.  Can't you use
strcpy() instead?
msg58703 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2007-12-17 19:53
Adam Olsen wrote:
> You have:
> #define Py_NAN Py_HUGE_VAL * 0
> I think this would be safer as:
> #define Py_NAN (Py_HUGE_VAL * 0)
> 
> For instance, in code that may do "a / Py_NAN".

You are right! Fixed

> Those manual string copies (*cp++ = 'n';) are ugly.  Can't you use
> strcpy() instead?

Done

Christian
msg58704 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2007-12-17 19:54
I'm posting a combined patch for all features at #1640.
msg58759 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-12-18 19:48
Mostly looks good.  Here are some nits.

(1) You shouldn't have to add pystrcmp.c to the VC project files since
on Windows it isn't used, right?

(2) Will the Windows input routine still accept the *old*
representations for INF and NAN?  IMO that's important (a) so as to be
able to read old pickles or marshalled data, (b) so as to be able to
read data files written by C programs.

(3) Shouldn't you be using Py_HUGE_VAL instead of HUGE_VAL in the chunk
starting at line 187 in floatobject.c?
msg58768 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2007-12-18 20:26
[Guido]
> ...
> (2) Will the Windows input routine still accept the *old*
> representations for INF and NAN?  IMO that's important (a) so as to be
> able to read old pickles or marshalled data, (b) so as to be able to
> read data files written by C programs.

Ha!  You're such an optimist ;-)  The remarkable truth is that Windows
has never been able to read its own representations for INF and NAN:

'1.#INF'
>>> float(_)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): 1.#INF
>>> repr(nan)
'-1.#IND'
>>> float(_)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): -1.#IND

This has nothing to do with Python -- same thing from C, etc.
msg58771 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2007-12-18 21:12
Guido van Rossum wrote:
> (1) You shouldn't have to add pystrcmp.c to the VC project files since
> on Windows it isn't used, right?

It was the easiest way to test the functions in pystrcmp. Linux doesn't
have stricmp but it also doesn't require the additional code to mangle
1.#INF into inf.

> (2) Will the Windows input routine still accept the *old*
> representations for INF and NAN?  IMO that's important (a) so as to be
> able to read old pickles or marshalled data, (b) so as to be able to
> read data files written by C programs.

See Tim's answer.
Pickles and other C programs aren't an issue. Internally NaNs and INFs
are represented with a special bit pattern (all bits of the exponent are
set). As long as users don't use str() or repr() on floats it still
works. The pickle module uses struct.

> (3) Shouldn't you be using Py_HUGE_VAL instead of HUGE_VAL in the chunk
> starting at line 187 in floatobject.c?

I missed the spot, thanks.

Christian
msg58776 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2007-12-18 21:56
Historical note:  Guido is probably thinking of "the old" pickle and
marshal here, which did have problems with inf and NaN on Windows (as in
they didn't work at all).  Michael Hudson changed them to use special
bit patterns instead, IIRC for Python 2.5.
msg58780 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-12-18 22:19
> Historical note:  Guido is probably thinking of "the old" pickle and
> marshal here, which did have problems with inf and NaN on Windows (as in
> they didn't work at all).  Michael Hudson changed them to use special
> bit patterns instead, IIRC for Python 2.5.

In pickle.py, protocol 0 (still the default in 2.6) uses repr(x) to
write a float and float(s) to convert that back to input. Maybe you're
thinking of marshal, which is more sophisticated.
msg58781 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-12-18 22:21
I suggest you check this in (with the Py_HUGE_VAL fix) and then see how
much of #1640 we still need.
msg58785 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2007-12-18 23:26
Applied in r59558 to the trunk
History
Date User Action Args
2022-04-11 14:56:29adminsetgithub: 45976
2008-01-06 22:29:44adminsetkeywords: - py3k
versions: Python 2.6, Python 3.0
2007-12-18 23:26:07christian.heimessetstatus: open -> closed
resolution: accepted -> fixed
messages: + msg58785
2007-12-18 22:21:27gvanrossumsetresolution: accepted
messages: + msg58781
2007-12-18 22:19:55gvanrossumsetmessages: + msg58780
2007-12-18 21:56:36tim.peterssetmessages: + msg58776
2007-12-18 21:12:23christian.heimessetmessages: + msg58771
2007-12-18 20:26:10tim.peterssetnosy: + tim.peters
messages: + msg58768
2007-12-18 19:48:33gvanrossumsetnosy: + gvanrossum
messages: + msg58759
2007-12-17 19:54:27christian.heimessetsuperseder: Enhancements for mathmodule
messages: + msg58704
2007-12-17 19:53:44christian.heimessetmessages: + msg58703
2007-12-17 17:37:24Rhamphoryncussetnosy: + Rhamphoryncus
messages: + msg58697
2007-12-17 16:04:28christian.heimeslinkissue1640 dependencies
2007-12-15 20:54:44christian.heimessetfiles: + trunk_float_inf_nan2.patch
messages: + msg58664
2007-12-15 10:23:24christian.heimescreate