How to calculate absolute value in Python

How to Calculate Absolute Value in Python

In this article, we’ll show you how to calculate absolute value in Python. Here’s how to accomplish this task.

  • Using a User-Defined Function (Brute Force Method)
  • Using the abs() Function

  • Using the math.fabs() Function

The magnitude of a number, whether positive or negative, is called its absolute value. For example, the absolute value of -2 is 2, while 2 is just 2.

Important Note

  • The absolute value is returned by the built-in abs() function.
  • math.fabs() also returns an absolute value, but it is a floating-point value.

  • When we pass an integer or floating-point number to abs(), we get the absolute integer or floating-point value back.

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

Using User-Defined Functions (Brute Force Method)

Algorithm (Steps)

Below are the algorithm/steps you need to follow to perform the desired task.

  • Create a function that returns the absolute value of the number passed to it as an argument.
  • Use an if condition statement to check if the number is less than 0.

  • If the condition is true, the negative of the value is returned. That is, the – value becomes the plus (+), which is the absolute value.

  • If the number is positive, the passed number is returned 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 the 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

Upon execution, the above program will produce the following output

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

Using the abs() Function

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

Syntax

abs(number)

When applied to a real number, the abs() function returns the magnitude of that number. A real number is defined as lying on the number line, as shown in the figure below. The magnitude of a real number is its distance along the line from the origin.

The sign of a number indicates the direction of the number along the line; positive values lie along the positive axis, and negative values lie along the negative axis. In the quick introduction example, -5 is a real number.

Using abs() to calculate the absolute value of an integer

The following program uses the abs() function to return the absolute value of an integer –

# 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

When executed, the above program will produce 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

Using abs() to Calculate the Absolute Value of a Complex Number

abs() can also be used on complex numbers.

A complex number consists of a real number and an imaginary number. Imaginary numbers are represented by the square root of a negative number. They are usually represented by their value, i.e., the square root of -1.

Imaginary numbers fill many mathematical gaps. Therefore, they are widely used in math-intensive fields, especially in electrical engineering. An example of a complex number is shown in the figure below.

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

For example

Use abs() to calculate the absolute value of a complex number

The following program uses the abs() function to return the absolute value of a complex number.

# 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

When executed, the above program will produce the following output

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

Using the math.fabs() Function to Calculate the Absolute Value of a Number

In addition to the standard abs() method, Python also has the math.fabs() function. This function also takes a single argument and returns the absolute value of that argument as a floating-point value.

The following program uses the abs() function to return the absolute value of a complex number.

# 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

When executed, the above program will produce 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

Summary

In this tutorial, we introduced how to calculate absolute value in Python using three different methods. We also learned how to determine the absolute value of a complex number and a floating-point number through examples.

Leave a Reply

Your email address will not be published. Required fields are marked *