Hetepeperfan

March 30th, 2011, 09:20 PM

Hello,

Why does the result of -2 to the power of 2 result in -4 if you use an operator, but nicely in 4 when you use a function?

In python eg:

>>> -2**2

-4

but :

>>> pow(-2,2)

4

and simillary for octave/matlab (only tried in octave)

octave:6> -3^2

ans = -9

octave:5> power(-3,2)

ans = 9

is there a reason why negative numbers remain negative while raising to the power of an even positive number? I just noticed it today and I don't understand why this behaviour is?

Kind regards,

Maarten

Why does the result of -2 to the power of 2 result in -4 if you use an operator, but nicely in 4 when you use a function?

In python eg:

>>> -2**2

-4

but :

>>> pow(-2,2)

4

and simillary for octave/matlab (only tried in octave)

octave:6> -3^2

ans = -9

octave:5> power(-3,2)

ans = 9

is there a reason why negative numbers remain negative while raising to the power of an even positive number? I just noticed it today and I don't understand why this behaviour is?

Kind regards,

Maarten