How to calculate absolute value in Python?

PythonServer Side ProgrammingProgramming

Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

Practical Data Science using Python

22 Lectures 6 hours

In this article, we will show you how to calculate the absolute value in python. Below are the methods to accomplish this task:

• Using User-Defined Function (Brute Method)

• Using abs() function

• Using math.fabs() function

The magnitude of a number, whether positive or negative, is referred to as its absolute value. For example, the absolute value of -2, is 2, and 2 is simply 2.

Important Points

• The absolute value is returned by the built-in abs() function.

• The math.fabs() function also returns the absolute value, but as a floating-point value.

• When we pass an integer or float value to abs(), we obtain the absolute integer or float value return.

• However, if we pass a complex number to abs(), the function returns the magnitude of that number.

Using User-Defined Function (Brute Method)

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

• Create a function that returns the absolute value of the number passed to it as an argument.

• Use the if conditional statement to check whether the number is less than 0.

• Return the negative of the value if the condition is true i.e -(-) value becomes plus(+) which is the absolute value.

• Return the number passed if it is a positive number directly.

The following program returns the absolute value of a number −

# creating a function that returns the absolute value
def getAbsoluteValue(value):
# checking whether the number is less than 0
if(value<0):
# returning negative of the value if the condition is true
# i.e -(-) value becomes plus(+)
return -value
# returning the number passed
return value
# calling the getAbsoluteValue() function by passing some random positive number
print("Absolute value of 5 = ", getAbsoluteValue(5))
# calling the getAbsoluteValue() function by passing some random negative number
print("Absolute value of -5 = ", getAbsoluteValue(-5))


Output

On executing, the above program will generate the following output −

Absolute value of 5 = 5
Absolute value of -5 = 5


Using abs() function

The abs() function is used to find the absolute value of a number. It can be applied for both real and complex numbers.

Syntax

abs(number)


When used to a real number, the abs() function returns the magnitude of that number. A real number is defined as being on the real number line, which is depicted in the graphic below. A real number's magnitude is its distance along a straight line from the origin.

The sign of the number denotes which direction the number is along the line; positive values are along the positive axis, and negative values are along the negative axis. In the introduction's quick example, -5 is a real number.

In Python, real numbers are numbers that are either integers or floats.

Calculating absolute values for integers using abs()

The following program returns the absolute values of integers using the abs() function −

# input numbers(integers)
num_1 = 4
num_2 = -6
num_3 = 0
num_4 = -875
# calculating absolute values of input integers
print("absolute value of 4 = ", abs(num_1))
print("absolute value of -6 = ", abs(num_2))
print("absolute value of 0 = ", abs(num_3))
print("absolute value of -875 = ", abs(num_4))


Output

On executing, the above program will generate the following output −

absolute value of 4 = 4
absolute value of -6 = 6
absolute value of 0 = 0
absolute value of -875 = 875


Calculating absolute values for floating-point numbers using abs()

The following program returns the absolute values of floating-point numbers using the abs() function −

# input numbers(float)
num_1 = -4.5
num_2 = 6.789
num_3 = -10.56
num_4 = 8.23
# calculating absolute values of input floating-point numbers
print("absolute value of -4.5 = ", abs(num_1))
print("absolute value of 6.789 = ", abs(num_2))
print("absolute value of -10.56 = ", abs(num_3))
print("absolute value of 8.23 = ", abs(num_4))


Output

On executing, the above program will generate the following output −

absolute value of -4.5 = 4.5
absolute value of 6.789 = 6.789
absolute value of -10.56 = 10.56
absolute value of 8.23 = 8.23


Calculating Absolute Value of Complex Numbers using abs()

The abs() method can also be used on complex numbers.

A complex number is made up of both real and imaginary numbers. An imaginary number is one that is expressed in terms of the square root of a negative number. They are typically stated in terms of value, which is the square root of -1.

Many mathematical gaps are filled by imaginary numbers. As a result, they are widely utilized in math-intensive fields, particularly in electrical engineering. An example of a complex number is seen in the figure below:

• If we pass a complex number to abs(), the function returns the magnitude of that number.

For example:

The following program returns the absolute values of complex numbers using the abs() function −

# input complex numbers
num_1 = 4+5j
num_2 = 2-3j
num_3 = -3-4j
# calculating absolute values of input complex numbers
print("absolute value of 4+5j = ", abs(num_1))
print("absolute value of 2-3j = ", abs(num_2))
print("absolute value of -3-4j = ", abs(num_3))


Output

On executing, the above program will generate the following output −

absolute value of 4+5j = 6.4031242374328485
absolute value of 2-3j = 3.605551275463989
absolute value of -3-4j = 5.0


Calculating the Absolute Value of Numbers using math.fabs() Function

Python has the math.fabs() function in addition to the standard abs() method. This function requires one argument as well. The absolute value of that argument is then returned as a floating-point value.

The following program returns the absolute values of complex numbers using abs() function −

# importing math module
import math
# input numbers
num_1 = 4
num_2 = -6.5
num_3 = -5
num_4 = -8
# calculating absolute values of input numbers as floating-point numbers
print("absolute value of 4 = ", math.fabs(num_1))
print("absolute value of -6.5 = ", math.fabs(num_2))
print("absolute value of -5 = ", math.fabs(num_3))
print("absolute value of -8.65 = ", math.fabs(num_4))


Output

On executing, the above program will generate the following output −

absolute value of 4 = 4.0
absolute value of -6.5 = 6.5
absolute value of -5 = 5.0
absolute value of -8.65 = 8.0


Conclusion

We covered how to calculate the absolut value in Python using three distinct ways in this tutorial. With examples, we also learned how to determine the absolute value of a complex number and a floating point number.

Updated on 25-Oct-2022 07:25:13