简体   繁体   中英

Python 3.6: Floor division of a complex number

I need to floor divide a complex number, but it appears that floor division by complex numbers was removed in Python 3.x.

Is there any workarounds for this? Any ideas would be appreciated.

eg.

(2 + 3i) // (complex anything) will throw an error.

Complex numbers in python 3 are defined with their imaginary part as j , not i .

You are right that in Python 3 you can't compute floor division of complex numbers anymore, but you need to specify how you want it to behave, if you need a workaround.

In any case, you will need to implement a new class that inherits from complex , and override the __floordiv__ method

Unless (anything) is a multiple of (2+3j) , then your answer will also be complex, hence floor-division is not well-defined. You can implement division by multiplying by (anything).conjugate / (abs(anything)**2)

I just found the mpmath library. It has large amount of tools, including floor and mod for complex numbers. It appears that its actually included with SymPy.

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