This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: (-1) ** 0.5 returns (6.123233995736766e-17+1j) instead of 1j
Type: behavior Stage: resolved
Components: Versions: Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: NathanFallet, mark.dickinson, serhiy.storchaka, steven.daprano
Priority: normal Keywords:

Created on 2021-04-13 08:54 by NathanFallet, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (6)
msg390939 - (view) Author: Nathan Fallet (NathanFallet) Date: 2021-04-13 08:54
Complex exponentiation doesn't work as expected:

```
>>> (-1) ** 0.5
(6.123233995736766e-17+1j)
```

I think the issue is linked with this part of the code:
https://github.com/python/cpython/blob/32bd68c839adb7b42af12366ab0892303115d1d1/Objects/complexobject.c#L142-L151
msg390946 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2021-04-13 09:51
What do you mean? It works as I expected.  Can you explain what you expected, and why?
msg390947 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2021-04-13 09:55
Before replying please read:

https://docs.python.org/3/faq/design.html#why-are-floating-point-calculations-so-inaccurate

https://duckduckgo.com/?q=python+why+are+floating+point+so+inaccurate
msg390948 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2021-04-13 09:58
Sorry Nathan, I worded my first response awkwardly, of course mathematically we should expect a result of 1j, by why do you expect it in a floating point calculation? Do you have an alternative?

(I promise this is my last comment until you have had a chance to reply.)
msg390961 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-04-13 13:03
It is the same "gotcha" as

>>> math.cos(math.pi/2)
6.123233995736766e-17

You can expect that cos(π/2) is exactly 0, but floating point value math.pi is only an approximation of the π number. The difference between math.pi and exact value of π leads to non-zero result of cos(math.pi/2).
msg390965 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-04-13 13:48
FWIW, if you're specifically interested in complex square roots rather than powers in general, I'd recommend using `cmath.sqrt(value)` rather than `value**0.5` - there are fewer intermediate steps involved in computing `cmath.sqrt`, and the returned value will in general be a bit more accurate, and better defined in corner cases. (And probably slightly faster too, in the rare situations where that matters.)
History
Date User Action Args
2022-04-11 14:59:44adminsetgithub: 87996
2021-04-13 13:48:06mark.dickinsonsetmessages: + msg390965
2021-04-13 13:03:40serhiy.storchakasetstatus: open -> closed
resolution: not a bug
stage: resolved
2021-04-13 13:03:06serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg390961
2021-04-13 09:58:30steven.dapranosetmessages: + msg390948
2021-04-13 09:55:28steven.dapranosetmessages: + msg390947
2021-04-13 09:51:26steven.dapranosetnosy: + steven.daprano
messages: + msg390946
2021-04-13 09:10:26christian.heimessetnosy: + mark.dickinson
2021-04-13 08:54:53NathanFalletcreate