Python Number Fabs() Method



The fabs() method in Python number returns the absolute value of x. This method of Python Number has been specified in the math library of Python 2 and Python 3. Thus, the Python fabs function refers to one of the Python Math functions that are known to return the absolute positive value of a specific expression or number. Most of the time it may happen that while one computes the difference amongst two numbers in order to calculate the closeness of a number with other then one needs to determine the magnitude of a specific number then fabs() method comes out to be of great help especially in case we deal with ints and the result is desired in float number in order to make floating-point comparisons ahead as fabs() may convert all its magnitude to a floating-point value.


Following is the syntax for fabs() method:

import math

math.fabs( x )

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


  • x - This is a numeric value.

Return Value

This method returns the absolute value of x.


The following example shows the usage of fabs() method.

import math   # This will import math module

print "math.fabs(-45.17) : ", math.fabs(-45.17)
print "math.fabs(100.12) : ", math.fabs(100.12)
print "math.fabs(100.72) : ", math.fabs(100.72)
print "math.fabs(119L) : ", math.fabs(119L)
print "math.fabs(math.pi) : ", math.fabs(math.pi)

When we run the above program, it produces the following result:

math.fabs(-45.17) :  45.17
math.fabs(100.12) :  100.12
math.fabs(100.72) :  100.72
math.fabs(119L) :  119.0
math.fabs(math.pi) :  3.14159265359

Here at Intellinuts, we have created a complete Python tutorial for Beginners to get started in Python.