The fabs (x) method in Python returns the absolute value of x. Although similar to the abs () function, there are the following differences between the two functions:
- abs () is a built-in function, and fabs () is defined in the math module.
- The fabs () function is only applicable to float and integer types, and abs () is also applicable to complex numbers.
Examples:
#!/usr/bin/python # -*- coding: UTF-8 -*- import math
a = - . 1 B = - 1.3232 C = B D = . 1 + 1.0 J E = . 3 + 4.0 J Print "absolute value of a is:" , ABS ( a ) Print "is the absolute value of B:" , ABS ( B ) print “The absolute value of c is:” , math . fabs ( c ) print “The absolute value of d is:” , abs ( d ) “” “print“ The absolute value of e is:
”, math.fabs(e) “””
If the last line of code is uncommented, it will compile and report an error:
TypeError : can 't convert complex to float // fabs cannot convert complex numbers to float
Python3.x test code:
Import Math a = - . 1 b = - 1.3232 C = b D = . 1 + 1.0 J E = . 3 + 4.0 J Print "absolute value of a is:" , ABS ( a )) Print ( "the absolute value of b is:" , abs ( b )) print ( "The absolute value of c is:" , math . fabs ( c )) print ( "The absolute value of d is:" , abs ( d )) "" "print (" The absolute value of e is: ", math.fabs (e))" ""