Python NaN squared error

This is purely out of curiosity, but why is this happening?

>>> a = float('Nan')
>>> a**2.0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: (33, 'Domain error')

I would expect it to just return NaN instead of generating an error.

+3
source share
4 answers

From http://www.mail-archive.com/ relax-devel@gna.org /msg00337.html it seems that this is only in the case of Windows builds, due to the way the compiler implements floating point things.

  • Will there be some people who cannot reproduce their OS?
  • Will someone installed on 2.x on windows try it (I get the same error in 3.1.3 (in Windows 7 32 bit))?
  • @OP: You use windows, yes?

Example

Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> float('NaN')
nan
>>> _**2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: (33, 'Domain error')
+4
source

Python, . Python, 2,5 3,1.

>>> nan = float('NaN')
>>> nan ** 2.0
nan
+4

Vista SP2 Intel DualCore 2.1

CPython:

In []: sys.version
Out[]: '2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)]'
In []: float('NaN')** 2.
Out[]: nan

>>> sys.version
'3.1.3 (r313:86834, Nov 27 2010, 18:30:53) [MSC v.1500 32 bit (Intel)]'
>>> float('NaN')** 2.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: (33, 'Domain error')

The same compiler, but different versions, different results

From another world of IronPython:

>>> sys.version
'2.6.1 ()'
>>> float('NaN')** 2.
nan

>>> sys.version
'2.7.0 (IronPython 2.7 Beta 1 (2.7.0.10) on .NET 4.0.30319.1)'
>>> float('NaN')** 2.
nan
+1
source

This is what I get

Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2

>>> nan=float("NaN")
>>> nan
nan
>>> nan*2
nan
>>> nan**2
nan
>>>
0
source

Source: https://habr.com/ru/post/1788329/


All Articles