Function abs() to get the modulus of a number

by Alex
Function abs() to get the modulus of a number

The built-in abs(x) function in Python returns the absolute value of the argument x, which can be an integer or a floating point number, or an object implementing __abs__(). For complex numbers, the function returns their value. The absolute value of any numeric value -x or +x is always the corresponding positive +x. Argumentxcelest number, floating point number, complex number, an object that implements __abs__()The returned value|x|returns the absolute value of the input argument

An abs() example with an integer

The following code demonstrates how to get the absolute value of the positive number 42.


x = 42
abs_x = abs(x)
print(f "The absolute value of {x} is {abs_x}")
# Conclusion: The absolute value of 42 is 42

Conclusion: “The absolute value of 42 is 42. The same, but with a negative -42.

x = -42
abs_x = abs(x)
print(f "The absolute value of {x} is {abs_x}")
# Conclusion: The absolute value of -42 is 42

Example with a float number

Here’s how to get an absolute value of 42.42 and for -42.42:


x = 42.42
abs_x = abs(x)
print(f "The absolute value of {x} is {abs_x}")
# The absolute value of 42.42 is 42.42
x = -42.42
abs_x = abs(x)
print(f "The absolute value of {x} is {abs_x}")
# The absolute value of -42.42 is 42.42

A complex number

The absolute value of a complex number is (3+10j).


complex_number = (3+10j)
abs_complex_number = abs(complex_number)
print(f "The absolute value of {complex_number} is {abs_complex_number}")
# The absolute value of (3+10j) is 10.44030650891055

abs() vs fabs()

abs(x) calculates the absolute value of the argument x. By analogy, the math module’s fabs(x) function calculates the same value. The only difference is that math.fabs(x) returns a floating-point number, while abs(x) returns an integer if the argument is an integer. Fabs stands for float absolute value. Example c fabs():


x = 42
print(abs(x))
# 42
import math
print(math.fabs(x))
# 42.0

abs() vs. np.abs()

Both abs() in Python and np.abs() in NumPy calculate the absolute value of a number, but there are two differences. np.abs(x) always returns a floating-point number. Also, np.abs(x) takes the NumPy array, calculating a value for each item in the collection. Example:


x = 42
print(abs(x))
# 42
import numpy as np
print(np.fabs(x))
# 42.0
a = np.array([-1, 2, -4])
print(np.abs(a))
# [1 2 4]

abs and np.abs are exactly the same. There is no difference which one to use. The only advantage of the former is that its call is shorter.

Output

The abs() function is a built-in function that returns the absolute value of a number. It accepts integers, floating point, and complex numbers as input. If you pass an integer or floating-point number to abs(), the function returns a non-negative value of n and keeps the type. For an integer, it is an integer. For a floating point number – floating point number.


>>> abs(20)
20
>>> abs(20.0)
20.0
>>> abs(-20.0)
20.0

Complex numbers have two parts and can be written in the form a + bj, where a and b are either integers or floating point numbers. The absolute value of a + bj is calculated mathematically as math.sqrt(a**2 + b**2).


>>> abs(3 + 4j)
5.0
>>> math.sqrt(3**2 + 4**2)
5.0

Thus, the result is always positive and is always a floating point number.

Related Posts

LEAVE A COMMENT