Python Number fabs() Method


Contents

Description

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, Python fabs function refers to one of the Python Math function that is known to return absolute positive value of a specific expression or number. Most of the times 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.

Syntax

Following is the syntax for fabs() method:

import math

math.fabs( x )

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.

Parameters

  • x − This is a numeric value.

Return Value

This method returns absolute value of x.

Example

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

#!/usr/bin/python
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 above program, it produces 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.



Navigation
Tutorial
Tutorial
Tutorial