Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decimal module and complex numbers in Python

Tags:

python

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.

like image 378
NSZ Avatar asked Jun 12 '17 13:06

NSZ


People also ask

What is decimal module in Python?

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.

How do you find the modulus of a complex number in Python?

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.

What is a complex number in Python?

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.

What is the difference between float and decimal in Python?

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.


Video Answer


1 Answers

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.

like image 79
casevh Avatar answered Oct 03 '22 19:10

casevh