0 == ((-1)**.5).real
... is False in python 3.5.1, whereas:
0 == complex(0,1).real
... is True. how are these two cases handled differently? when do the zero-detecting features of the float class work and when do they not?
0 == ((-1)**.5).real
... is False in python 3.5.1, whereas:
0 == complex(0,1).real
... is True. how are these two cases handled differently? when do the zero-detecting features of the float class work and when do they not?
>>> (-1)**0.5
(6.123233995736766e-17+1j)
That's all there is to it - due to floating-point vagaries, the real part of the computed result isn't exactly zero. But in your other case it is:
>>> complex(0,1).real
0.0
By the way, **
invokes a general-purpose exponentiation routine, which adds several layers of floating-point roundoff errors under the covers. If you know you want a square root, it's better to use a square root function:
>>> import cmath
>>> cmath.sqrt(-1)
1j
The fractional power is computed -- simplifying somewhat -- as r cis theta. Since theta (pi) cannot be exactly represented as a binary fraction, the result is not exactly what you'd expect from hand calculation. There are various "equal within a tolerance" functions you can apply to work around this.