简体   繁体   中英

How to avoid getting imaginary/complex number python

I am using a python code, where one of the equations got sqrt root of negative value. I use cmath.sqrt to solve it. All answers I got from that equation are shown in imaginary/complex number (egxxxxxx j ). I don't want to get that imaginary/complex number as the code that I use subsequently cannot read those imaginary/ complex number. As such, how can I avoid not to get imaginary numbers? OR in other way, how can I convert those imaginary number into real ones? or how can I remove those "j". Thanks.

If I understand your question you either want

abs(z)

or

z.real

If you want to drop the imaginary part from the value which you got after square root of any random number in python, so you can simply do:

value = sqrt(any number)

# for only real part
value = value.real

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM