What is the difference between the modules Math and cmath?

Asked

Viewed 304 times

2

In Python we realize that there are the following modules: math and cmath, however I did not understand what difference between the two. See how they can be imported:

import math
import cmath

What differences between modules math and cmath?

3 answers

6


As described in the documentation, the module cmath "provides access to mathematical functions for complex numbers". In fact the functions of this module also accept integer and real numbers, and allow working with complex numbers. Already the "normal" functions of the module math cannot be used with complex numbers.

6

  • mathis the mathematical functions module.

  • cmath is the module of mathematical functions working with complex numbers.

1

inserir a descrição da imagem aqui

I created this diagram to display more clearly and objectively the hierarchical distribution of the numerical sets.

As we can observe each numerical set is represented by a certain letter, i.e.:

  1. N - Set of Natural Numbers;
  2. Z - Set of numbers Integers;
  3. Q - Set of Rational Numbers;
  4. GO - Set of Irrational Numbers;
  5. R - Set of Real Numbers;
  6. C - Set of Complex Numbers.

The class math is specialized to work with numbers belonging to the set of numbers Reais.

As we can see, the set of numbers Reais, is a subset of the set of numbers Complexos, that is, the irrigation used to operate with real numbers is not sufficient to cover operations with complex numbers. So when we want to operate with real numbers we use the class math.

Already the class cmath is specialized to work with numbers belonging to the set of complex numbers.

Looking again at the diagram, we can notice that all sets are contained in the set of complex numbers. For this reason, the methods implemented in the class cmath are much more comprehensive.

What does this mean?

This means that when we import the class math, we can work with all the numbers except the numbers complexos. And when we import the class cmath, we can work with all the numbers without any exception.

It is logical, that to operate with real numbers we do not need to import the class cmath, because we already have everything we need in class math.

Now, from the moment we need to work with complex numbers, it’s obvious that we should import the class cmath.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.