Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python NaN squared error

Tags:

python

This is purely out of curiosity, but why does this occur?

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

I would have expected it to simply return NaN instead of generating an error.

like image 281
TimothyAWiseman Avatar asked May 19 '26 17:05

TimothyAWiseman


1 Answers

From http://www.mail-archive.com/[email protected]/msg00337.html, it seems that this is only the case on the windows builds, due to how the compiler implements floating point stuff.

  • Would some of the people who can't reproduce post their OS?
  • Would someone having a 2.x on windows installed try it out (I get the same error on 3.1.3 (on Windows 7 32 bit))?
  • @OP: You are using 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')