-1

In Python 3.8, when I run: -math.inf + math.inf ==> I am getting "nan".

Should the result be: 0? Or what is the rational for Python to produce "nan" for this addition?

charlesqwu
  • 354
  • 2
  • 11
  • 5
    I'd look at the IEEE math rules for that operation. – Mark Ransom Mar 24 '22 at 00:54
  • 2
    Here link to the [IEEE standard on wikipedia](https://en.wikipedia.org/wiki/IEEE_754#2019). You might also be intrested in the following [thread](https://stackoverflow.com/questions/36151158/how-are-nan-and-infinity-of-a-float-or-double-stored-in-memory) explaining the bit representation of `+inf`, `-inf` and `NaN` and providing further links. – Mushroomator Mar 24 '22 at 01:14

1 Answers1

1

In mathematical terms Infinity is not a definable number. You're thinking of it as "a really big number that has a value". It doesn't. (Infinity * Infinity) is still Infinity. Don't try to mentally assign a real value to this. The "nan" value makes sense as any math with Infinity (or math.inf in this case) really makes no sense.

stdunbar
  • 16,263
  • 11
  • 31
  • 53