0

When you try to get the square root of -1 using the exponent operator:

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

Why do we get this insignificant real part (still not 0)? What are the operations done by the exponent operator that leads to the float precision error?

What I am trying to get at is how the ** approximate the answer, not to actually get the right answer.

Gopoi
  • 113
  • 7
  • 4
    It's zero to seventeen decimal places, that's pretty good! – kindall May 24 '17 at 18:21
  • 4
    Expect rounding error unless you have specific knowledge that an operation won't produce it. – user2357112 May 24 '17 at 18:21
  • 3
    try using sqrt from cmath https://docs.python.org/2/library/cmath.html#module-cmath – DSLima90 May 24 '17 at 18:22
  • 2
    Let's make sure that we solve your actual problem -- why is the difference important to you? – Prune May 24 '17 at 18:28
  • 1
    Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Stephen Rauch May 24 '17 at 18:34
  • 4
    `**` doesn't know that `sqrt(-1) == j`; it just jumps right into the trigonometry that lets you compute the value of any negative number raised to a fractional power. – chepner May 24 '17 at 18:36
  • @chepner that might just be the answer I'm after, if you could provide an example :) – Gopoi May 28 '17 at 03:42
  • See https://hg.python.org/cpython/file/3.6/Objects/complexobject.c#l109 for the code that ultimately gets called. – chepner May 28 '17 at 19:50

0 Answers0