Reputation: 616
Why is 1**Inf == 1
?
I believe it should be NaN
, just like Inf-Inf
or Inf/Inf
.
How is exponentiation implemented on floats in python?
exp(y*log(x))
would get correct result :/
Upvotes: 1
Views: 253
Reputation: 106167
Floating-point arithmetic is not real-number arithmetic. Notions of "correct" informed by real analysis do not necessarily apply to floating-point.
In this case, however, the trouble is just that pow
fundamentally represents two similar but distinct functions:
pow(x,y) = exp(y * log(x))
restricted to the real line.These functions agree for normal values, but differ in their edge cases at zero, infinity, and along the negative real axis (which is traditionally the branch cut for the second function).
These two functions are sometimes divided up to make the edge cases more reasonable; when that's done the first function is called pown
and the second is called powr
; as you have noticed pow
is a conflation of the two functions, and uses the edge cases for these values that come from pown
.
Upvotes: 2
Reputation: 122383
You are right, mathematically, the value of 1∞ is indeterminate.
However, Python doesn't follow the maths exactly in this case. The document of math.pow
says:
math.pow(x, y)
Return
x
raised to the powery
. Exceptional cases follow Annex ‘F’ of the C99 standard as far as possible. In particular,pow(1.0, x)
andpow(x, 0.0)
always return1.0
, even whenx
is a zero or a NaN.
Upvotes: 4
Reputation: 7132
Technically 1^inf is defined as limit(1^x, x->inf). 1^x = 1 for any x >1, so it should be limit(1,x->inf) = 1, not NaN
Upvotes: 1