sqrtnum_ = 0
sqrtnum_ = cmath.sqrt(snum_)
print "Using cmath.sqrt:"
print " sqrt(", snum_, ") = ", '%.12f' %sqrtnum_
How can I limit the number of decimal places. Normally I would use the above but it does not work with complex numbers
sqrtnum_ = 0
sqrtnum_ = cmath.sqrt(snum_)
print "Using cmath.sqrt:"
print " sqrt(", snum_, ") = ", '%.12f' %sqrtnum_
How can I limit the number of decimal places. Normally I would use the above but it does not work with complex numbers
This looks to be a duplicate of Formatting Complex Numbers
hopefully that helps!
(would have followed duplicate marking/commenting/etc protocol, but lack the rep for it)
You can use imag
and real
attributes of a complex number to get their respective values.
>>> a = cmath.sqrt(-1000)
>>> "%.12f + %.12fj"%(a.real,a.imag)
'0.000000000000 + 31.622776601684j'
This works for completely real numbers also
>>> a = cmath.sqrt(1000)
>>> "%.12f + %.12fj"%(a.real,a.imag)
'31.622776601684 + 0.000000000000j'