0

I already know how does abs function works.I mean it only shows you how far the number is from zero

The only thing i can't just get is with this example:

print(abs(3 + 4j)) # prints 5 !! Why ? 
moh80s
  • 763
  • 1
  • 7
  • 21

3 Answers3

2

Because for complex numbers abs(number) will return magnitude of Complex Numbers.

Magnitude value will counted as : √x2+y2 = √(3² + 4²) = √(9 + 16) = √(25) = 5.0

So abs with complex number will return magnitude of complex numbers.

for further reference you can use https://www.geeksforgeeks.org/abs-in-python/.

Aditya
  • 557
  • 3
  • 13
1

Because in python, 3+4j is complex number and python calculates the absolute value or magnitude of the complex number when you do abs() on it. Magnitude of 3+4j is 5. Try this :

type(3+4j)

It should give <class 'complex'>.

Note : Magnitude of a complex number a+bj is ((a**2+b**2)**0.5)

Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
1

As the rest of the answers stated above, 3+4j is a complex number and the formula of calculating the absolute value of a complex number x+yi is sqrt( (x^2) + (y^2) ). In your case it's:

 sqrt(3^2 + 4^2) = sqrt(9 + 16) = sqrt(25) = 5
Vasilis G.
  • 7,556
  • 4
  • 19
  • 29
  • 1
    I got it @Vasilis G. , as my first language is not english i did not know how do they calculate the absolute value of a complex number, thank you. – moh80s Sep 28 '19 at 09:13