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: math.copysign buggy with nan under Windows
Type: behavior Stage:
Components: Library (Lib), Windows Versions: Python 2.7
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: mark.dickinson Nosy List: mark.dickinson, pitrou, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2014-10-09 19:19 by pitrou, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (9)
msg228894 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-10-09 19:19
Z:\>c:\Python27\python.exe
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import math
>>> math.copysign(0.0, float("nan"))
-0.0
>>> ^Z

Z:\>c:\Python34\python.exe
Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:24:06) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> math.copysign(0.0, float("nan"))
0.0
>>> ^Z
msg228895 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2014-10-09 19:40
Why do you consider this a bug?
msg228896 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2014-10-09 19:43
More info: the reason for the difference is that in Python 3.4, float("nan") and float("inf") create the float directly from the appropriate bit-pattern, rather than deferring to the platform's definition of nan.  This change was introduced to avoid obscure problems on platforms where the Py_NAN macros were causing compile-time errors.  There should be a (resolved, fixed) issue about this somewhere on the tracker, but I don't have the number handy right now.

I don't consider this a bug.
msg228899 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-10-09 19:56
Ah, apparently it was already reported as issue14521...

Well, it's bug in as much as it shows different behaviour across platforms... AFAICT, we try to provide consistent math behaviour despite possible platform variations.
msg228900 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-10-09 20:00
Also, the fact that we support float('nan') and float('-nan') as producing different bit patterns means we should perhaps produce the right sign bit for each (it seems that's the problem here).

>>> import struct
>>> struct.pack("d", float("nan"))
'\x00\x00\x00\x00\x00\x00\xf8\xff'
>>> struct.pack("d", float("-nan"))
'\x00\x00\x00\x00\x00\x00\xf8\x7f'
msg228901 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2014-10-09 20:13
> we should perhaps produce the right sign bit for each

Perhaps.  But given that signs of NaNs are pretty much meaningless anyway (the IEEE 754 standard explicitly refuses to attach any meaning to NaN sign bits, and the sign bit of a NaN result is not specified for most operations), and that a change *could* conceivably break existing code, I don't see much of a case for changing the behaviour in 2.7.  I'm not particularly *opposed* to such a change; I just don't really see the point.

BTW, I'd expect this report to apply to other platforms in addition to Windows; Intel's "default" NaN has its sign bit set, so a platform that just does the lazy thing and lets the FPU NaN propagate will tend to see an inverted sign bit here.

It's only the 2.7 behaviour you're complaining about, right?  Are things working as expected on 3.4 and Windows?
msg228903 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-10-09 20:14
Yes, things are ok under 3.x. We'll probably disable our NaN tests under Windows anyway, since they aren't very useful as you point out.
msg229232 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2014-10-13 08:08
Antoine: is it okay to close this as "wont fix"?
msg229233 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-10-13 08:10
Yep, it's ok.
History
Date User Action Args
2022-04-11 14:58:08adminsetgithub: 66780
2014-10-13 08:19:50mark.dickinsonsetstatus: open -> closed
resolution: wont fix
2014-10-13 08:10:33pitrousetmessages: + msg229233
2014-10-13 08:08:04mark.dickinsonsetassignee: mark.dickinson
messages: + msg229232
2014-10-09 20:14:49pitrousetmessages: + msg228903
2014-10-09 20:13:33mark.dickinsonsetmessages: + msg228901
2014-10-09 20:00:22pitrousetmessages: + msg228900
2014-10-09 19:56:22pitrousetmessages: + msg228899
2014-10-09 19:43:56mark.dickinsonsetmessages: + msg228896
2014-10-09 19:40:50mark.dickinsonsetmessages: + msg228895
2014-10-09 19:19:23pitroucreate