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: long(float('nan'))!=0L
Type: Stage:
Components: Interpreter Core Versions: Python 3.0, Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: mark.dickinson Nosy List: belopolsky, christian.heimes, edahl, georg.brandl, mark.dickinson, mwh, pitrou, ronaldoussoren, tim.peters
Priority: critical Keywords: patch

Created on 2006-05-03 18:39 by edahl, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
longobject.patch ronaldoussoren, 2006-05-27 18:34
issue1481296.patch mark.dickinson, 2008-07-21 21:56 make long(float('nan')) raise ValueError
Messages (14)
msg28408 - (view) Author: Erik Dahl (edahl) Date: 2006-05-03 18:39
on all platforms I can test long(float('nan'))=0L

But on maxos X intel long(float('nan'))!=0L

it returns:

000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000L

This is a problem because:

>>> 344 - long(float('nan'))    
Objects/longobject.c:1645: failed assertion `borrow == 0'
Abort trap


msg28409 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2006-05-03 19:09
Logged In: YES 
user_id=31435

Try it on Windows and you'll get:

>>> long(float('nan'))
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: invalid literal for float(): nan

Nothing about the behavior of NaNs, infinities, or signed
zeroes is defined or guaranteed by Python.  You use them at
your own risk, and their behavior does vary wildly in
practice (according to the HW, OS, C compiler, C library,
and even the C compiler flags specified when compiling Python).
msg28410 - (view) Author: Michael Hudson (mwh) (Python committer) Date: 2006-05-09 18:47
Logged In: YES 
user_id=6656

Nevertheless, assuming that a compiler implements the relavent standards (i.e. 
Appendix F of C99) it's pretty clear that what PyLong_FromDouble does with a 
nan is pretty random.  frexp(dval, &expo); for dval nan stores an unspecified 
value in expo which is then used to compute the length of the resulting long.  If 
the unspecified value is large-ish and positive, hilarity ensues.
msg28411 - (view) Author: Erik Dahl (edahl) Date: 2006-05-10 12:36
Logged In: YES 
user_id=1482543

Just to be clear this does cause a crash in the interpreter.
msg28412 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2006-05-27 18:34
Logged In: YES 
user_id=580910

This could quite easily be fixed by testing for NaN in PyLong_FromDouble, such 
as the attached patch. This works correctly on OSX, but I have no idea if this will 
work on other platforms, that depends on how portable the already existing 
Py_IS_NAN macro is.  C99 also introduces an fpclassify and isnan functions that 
could be used (Py_IS_NAN uses neither because Python is written in C89 instead 
of C99).
msg59193 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-01-04 00:38
Fixed in r59689 trunk

Backport it to 2.5?
msg59217 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2008-01-04 06:35
The change in the trunk seems like a good backport candidate, it fixes 
an issue with float conversion on OSX compared to other platforms.

That said, I don't like the patch. This makes the current behaviour of 
converting float NaN's to 0 the defined behaviour, which feels wrong (he 
says without having the slightest idea of what the relevant standards 
might say about this).  Shouldn't this raise an exception instead of 
silently converting?
msg62301 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2008-02-12 02:20
A late comment:  I agree with Ronald here;  mightn't it make more sense
to raise an exception (ValueError?) for long(float("nan"))?

For comparison, long(float("inf")) currently raises OverflowError, at 
least on OS X.
msg62679 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2008-02-22 07:26
FYI: IEEE Standard 754 requires an invalid operation exception when 
inf/nan conversion to integer is attempted.
msg70077 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-07-20 11:21
It seems backporting this is not useful.
msg70121 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2008-07-21 21:56
I still think this is the wrong solution, and should be fixed before 2.6; 
long(float('nan')) should raise ValueError.  As Alexander points out, 
this fits much better with the IEEE 754 standard, and also with the C99 
standard.  It also just "feels right", to me; an attempt to convert a nan 
to an integer should not pass silently.

Here's a patch, based on Ronald's original patch.

Christian, what was the motivation for returning 0 here?

(One could also argue on the basis of IEEE 754 and C99 that 
long(float('inf')) should raise ValueError instead of OverflowError;  
personally, I'm content that long(float('inf')) raises an exception---I'm 
not too bothered which one.)

I agree that there doesn't seem much value in backporting the fix.
msg70123 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2008-07-21 22:23
Assigning to me;  I'd like to check in the new fix for 2.6/3.0, but I'll 
give it a couple of weeks for any objections to surface first.
msg70143 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2008-07-22 10:18
> It also just "feels right", to me; an attempt to convert a nan 
> to an integer should not pass silently.

I have the same gut feeling.
msg70721 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2008-08-04 21:32
New patch committed, r65518.  Conversion of a nan to an integer now raises 
ValueError consistently across platforms.
History
Date User Action Args
2022-04-11 14:56:17adminsetgithub: 43315
2008-08-04 21:32:19mark.dickinsonsetstatus: open -> closed
messages: + msg70721
2008-07-22 10:18:57pitrousetnosy: + pitrou
messages: + msg70143
2008-07-21 22:23:49mark.dickinsonsetassignee: mark.dickinson
messages: + msg70123
versions: + Python 2.6, Python 3.0, - Python 2.5
2008-07-21 22:02:19georg.brandlsetstatus: closed -> open
priority: normal -> critical
2008-07-21 21:56:36mark.dickinsonsetfiles: + issue1481296.patch
keywords: + patch
messages: + msg70121
2008-07-20 11:21:33georg.brandlsetstatus: pending -> closed
nosy: + georg.brandl
messages: + msg70077
2008-02-22 07:26:49belopolskysetnosy: + belopolsky
messages: + msg62679
2008-02-12 02:20:31mark.dickinsonsetnosy: + mark.dickinson
messages: + msg62301
2008-01-04 06:35:17ronaldoussorensetmessages: + msg59217
2008-01-04 00:38:08christian.heimessetstatus: open -> pending
nosy: + christian.heimes
resolution: fixed
messages: + msg59193
versions: + Python 2.5, - Python 2.3
2006-05-03 18:39:07edahlcreate