Python gives me a different result if I assign one of the intermediate steps to a variable, like this:
>>> -0.207 ** 0.66 - 1
-1.3536229379434348
>>> a = -0.207
>>> a ** 0.66 - 1
(-1.1703591496008927+0.30988214273656856j)
For this simple calculation, if I assign -0.207 to a temporary variable a, then the result of a ** 0.66 - 1 evaluates to a complex number.
Why does this happen, and how do I stop Python from doing that?
The correct answer to your statement (according to python) is a negative number. -0.207**0.66-1 evaluates to about -1.35.
The reason this is happening is that you're miscalculating in the one-liner:
-0.207 ** 0.66 - 1 actually evaluates to -(0.207 ** 0.66) - 1 and not to (-0.207) ** 0.66 - 1 like you'd expect.
When you separate the lines, you're changing the calculation to the second statement.
To stop this from happening, use explicit parentheses where there might be any ambiguity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With