Python Number fabs() Method

Advertisements


Description

The method fabs() returns the absolute value of x.

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



Advertisements
Advertisements