A surprise with 1**math.nan and 0j**math.nan


I'm surprised that

>>> import math
>>> 1**math.nan

And while we are at it, also that

>>> 0j**math.nan

I didn't find any other examples. Is there a reason or some logic I've missed that makes this the right choice? Or is this a slip?

I was expecting nan. As for every other number except 1 or 0j.

Edit 1: Thanks to jedwards's comment below I have a reference. But I still don't understand why. Why was this decided as the standard? Also, couldn't find reference to 0j**mat.nan...

Edit 2: So following the answers below and some other stuff, the logic may be this one: Any calculation involving nan should return nan unless the calculation is always returning the same answer regardless of arguments. In such cases, the fact that we have nan as an argument should not affect the result and we should still get the fixed answer.

This certainly explains 1**math.nan and math.nan**0. This also explains why 0**math.nan gives nan and not 0 (since 0**n is 0 for all but when n=0 where it results with 1), and might be stretched to cover why math.nan*0 is nan if we agree that the argument need not be finite.

But if this is the logic behind the scene, then 0j**math.nan should have been nan, since 0j**n is 0 for all n's except n=0 where 0j**0 is 1. So... does 0j**math.nan have different reasoning? or is it a problem in implementation?


Quoting this question which in turns quotes IEEE 754 (see Wikipedia),

The 2008 version of the IEEE 754 standard says that pow(1,qNaN) and pow(qNaN,0) should both return 1 since they return 1 whatever else is used instead of quiet NaN.

For details see page 56 of IEEE 754 2008:

pow(x, ±0) is 1 for any x (even a zero, quiet NaN, or infinity)

pow(±0, y) is ±∞ and signals the divideByZero exception for y an odd

Thus, the reasoning seems to be that no matter what number k is in the exponent, 1^k = 1, 1^Nan should also be 1. Why that reasoning is reasonable (I'm sure it is) I'll need to dig further.

Personally, I think this makes sense - Nan doesn't really exist in math, it's just that our floating point representation can't handle it (or, Nan is "the computation is too much, this is some number but not sure which"). Thus, 1^Nan could be 1 to an arbitrary power (not 1 to something that is not a number), but since the answer will always be 1, it can only help if we define 1^Nan to be 1.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at


Login to comment


TOP Ranking

  1. 1

    How can I create a column whose entries depend on the contents of a column in another table?

  2. 2

    pump.io port in URL

  3. 3

    How to display items using sessions

  4. 4

    BigQuery - concatenate ignoring NULL

  5. 5

    no applicable method for 'mutate_' applied to an object of class "c('integer', 'numeric')"

  6. 6

    How i extract text from a model dialog in selenium?

  7. 7

    x no applicable method for 'tbl_vars' applied to an object of class "c('double', 'numeric')"

  8. 8

    AirflowException: Celery command failed - The recorded hostname does not match this instance's hostname

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    Most efficient regex for checking if a string contains at least 3 alphanumeric characters

  11. 11

    using excel range find return Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))

  12. 12

    redirect your computer port to url

  13. 13

    Can't create database with Symfony 4.1 : could not find driver

  14. 14

    How to fix "Exception evaluating SpringEL expression" error after submitting a variable Spring/Thymeleaf

  15. 15

    A python function to get the maximum value of a specific column, given a string that represents a table in CSV format

  16. 16

    Double spacing in rmarkdown pdf

  17. 17

    Uploading Files on production server returns either CORS error or POST 400 Bad Request using Apollo-Graphql

  18. 18

    Android Kotlin Unit test failing with io.mockk.MockKException: no answer found for

  19. 19

    Change Model input_shape but got an : ValueError: Input 0 of layer dense_44 is incompatible with the layer

  20. 20

    Formik, Yup - How to check is Decimal number

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class