Is there any difference between the infinities returned by the math module and cmath module?
Does the complex infinity have an imaginary component of 0?
Is there any difference between the infinities returned by the math module and cmath module?
Does the complex infinity have an imaginary component of 0?
Copyright © 2021 Jogjafile Inc.
Any difference?
No, there is no difference. According to the docs, both
math.infandcmath.infare equivalent tofloat('inf'), or floating-point infinity.If you want a truly complex infinity that has a real component of infinity and an imaginary component of 0, you have to build it yourself:
complex(math.inf, 0)There is, however,
cmath.infj, if you want 0 as a real value and infinity as the imaginary component.Constructing imaginary infinity
As others have pointed out
math.inf + 0jis a bit faster thancomplex(math.inf, 0). We're talking on the order of nanoseconds though.