Issue 1635: Float patch for inf and nan on Windows (and other platforms) (original) (raw)

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 "", line 1, in ValueError: invalid literal for float(): 1.#INF >>> repr(nan) '-1.#IND' >>> float(_) Traceback (most recent call last): File "", line 1, in 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:29 admin set github: 45976
2008-01-06 22:29:44 admin set keywords: - py3kversions: Python 2.6, Python 3.0
2007-12-18 23:26:07 christian.heimes set status: open -> closedresolution: accepted -> fixedmessages: +
2007-12-18 22:21:27 gvanrossum set resolution: acceptedmessages: +
2007-12-18 22:19:55 gvanrossum set messages: +
2007-12-18 21:56:36 tim.peters set messages: +
2007-12-18 21:12:23 christian.heimes set messages: +
2007-12-18 20:26:10 tim.peters set nosy: + tim.petersmessages: +
2007-12-18 19:48:33 gvanrossum set nosy: + gvanrossummessages: +
2007-12-17 19:54:27 christian.heimes set superseder: Enhancements for mathmodulemessages: +
2007-12-17 19:53:44 christian.heimes set messages: +
2007-12-17 17:37:24 Rhamphoryncus set nosy: + Rhamphoryncusmessages: +
2007-12-17 16:04:28 christian.heimes link issue1640 dependencies
2007-12-15 20:54:44 christian.heimes set files: + trunk_float_inf_nan2.patchmessages: +
2007-12-15 10:23:24 christian.heimes create