Finrod Felagund
Finrod Felagund

Reputation: 1293

Python calculation of negative powers strange result

Does anyone know why the output of the bellow calculations returns different results, as it should be the same.

(-0.99)**(-0.99)

returns (-1.0095011228760993-0.03172485085856595j)

and

-0.99**-0.99

returns -1.0099994966583417

Upvotes: 0

Views: 86

Answers (2)

Mark Adelsberger
Mark Adelsberger

Reputation: 45659

Ok, so let's assemble the partial answers that are elsewhere, and also provide more complete context.

First of all, the premise that these should be equivalent expressions is incorrect. -x**y is not (-x)**y but rather is -(x**y).

Admittedly this is odd. If you write -2 on paper, you likely think of the - as part of the number, rather than as an operator whose precedence could be questioned. And yet if you write -22 it is standard to say this is -4, while (-2)2 is of course 4.

Here is an article on the subject: Link

Anyway, that's the short answer as to why they're not the same expression. Given that, it's just a matter of understanding each expression.

-.99**-.99

could be written

-1 * (.99**-.99)
-1 / (.99**.99)

The denominator can then be read "the hundredth root of .99 to the 99th power", and then it's just a matter of running through a pain-in-the-arse calculation.

On the other hand,

(-0.99)**(-0.99)

can also be simplified, but eventually we're going to take an even root of a negative number; so the answer must be a complex number.

Upvotes: 1

Dennis F
Dennis F

Reputation: 155


(-0.99)**(-0.99) yields a complex number, while -0.99**-0.99 yields a float.

Upvotes: 1

Related Questions