Python math.fabs() Method



The Python math.fabs() method is used to calculate the float absolute value of a number. The result of this method is never negative; even if the number is a negative value, the method will return the negation of it.

Unlike the abs() method, the fabs() method will always produce the result in float type; and it does not accept complex numbers as its arguments.

Note − This function is not accessible directly, so we need to import math module and then we need to call this function using math static object.

Syntax

Following is the syntax for the Python math.fabs() method −

math.fabs( x )

Parameters

  • x − This is a numeric value.

Return Value

This method returns float absolute value of x.

Example

The following example shows the usage of the Python math.fabs() method. In here, let us try to calculate the absolute values of positive real numbers.

import math

# Create positive Integer and Float objects
inr = 45
flt = 100.12

# Calculate the absolute values of the objects
abs_int = math.fabs(inr)
abs_flt = math.fabs(flt)

# Print the values
print("Absolute Value of an Integer:", abs_int)
print("Absolute Value of an Float:", abs_flt)

When we run above program, it produces following result −

Absolute Value of an Integer: 45.0
Absolute Value of an Float: 100.12

Example

As we have already discussed, the absolute value only considers the magnitude of a number. Therefore, in this example, we are creating number objects with negative values and try to use the fabs() method to calculate their absolute values of float type.

import math

# Create negative Integer and Float objects
inr = -34
flt = -154.32

# Calculate the absolute values of the objects
abs_int = math.fabs(inr)
abs_flt = math.fabs(flt)

# Print the values
print("Absolute Value of an Integer:", abs_int)
print("Absolute Value of an Float:", abs_flt)

Let us compile annd run the program above, the output is displayed as follows −

Absolute Value of an Integer: 34.0
Absolute Value of an Float: 154.32

Example

If we pass a complex number as an argument to this method, a TypeError is raised.

In the following example, we are creating two objects holding complex numbers, one positive and the other negative; which are then passed as arguments to this method.

import math

# Create positive and negative complex number objects
pos_cmplx = 12-11j
neg_cmplx = -34-56j

# Calculate the absolute values of the objects created
abs1 = math.fabs(pos_cmplx)
abs2 = math.fabs(neg_cmplx)

# Print the return values
print("Absolute Value of a positive complex number:", abs1)
print("Absolute Value of a negative complex number:", abs2)

Compile and run the program above, and the output is obtained as given below −

Traceback (most recent call last):
  File "main.py", line 8, in 
abs1 = math.fabs(pos_cmplx)
TypeError: can't convert complex to float

Example

When a None value is passed as an argument to the method, a TypeError is raised. But when we pass a zero as the argument, the method returns zero.

import math

# Create negative Integer and Float objects
zero = 0
null = None

# Calulate and Print the absolute values
print("Absolute Value of Zero:", math.fabs(zero))
print("Absolute Value of a Null:", math.fabs(null))

On executing the program above, the result is displayed as follows −

Absolute Value of Zero: 0.0
Traceback (most recent call last):
  File "main.py", line 9, in 
    print("Absolute Value of a Null:", math.fabs(null))
TypeError: must be real number, not NoneType
python_maths.htm
Advertisements