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 ?
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 ?
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/.
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)
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