Is there a way for manipulating complex numbers in more than floating point precision using python?
For example to get a better precision on real numbers I can easily use the Decimal
module. However it doesn't appear to work with complex numbers.
In Python, there is a module called Decimal, which is used to do some decimal floating point related tasks. This module provides correctly-rounded floating point arithmetic. To use it at first we need to import it the Decimal standard library module. import decimal.
Modulus of a complex number in Python using abs() function It should of the form a+bj, where a and b are real numbers. For instance, 8+6j. The complex number is stored in variable 'x'. Next, we used built-in abs() function to get the modulus of complex number.
A complex number is created from real numbers. Python complex number can be created either using direct assignment statement or by using complex () function. Complex numbers which are mostly used where we are using two real numbers.
Float is a single precision (32 bit) floating point data type and decimal is a 128-bit floating point data type. Floating point data type represent number values with fractional parts.
Disclaimer: I maintain gmpy2.
gmpy2 supports extended precision integer, rational, real, and complex numbers. It also supports a variety of scientific functions.
Another alternative is mpmath. mpmath
is written in pure Python so it may be easier to install. If gmpy2
is available, it will be used automatically to improve performance. mpmath
supports a wider variety of functions.
Note that both gmpy2
and mpmath
support binary (radix-2) floating point arithmetic while the Decimal
module supports decimal (radix-10) arithmetic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With