Latest web development tutorials

Python3 fabs () function

Python3 digital Python3 digital


description

fabs () method returns the number of absolute value, such as math.fabs (-10) returns 10.0.

fabs () function is similar to abs () function, but he has two differences:

  • abs () is a built-in function. fabs () function is defined in the math module.

  • fabs () function is only valid floating-point with integer value. abs () can also be used in the plural.


grammar

The following is the syntax fabs () method:

import math

math.fabs( x )

Note: fabs () is not directly accessible, you need to import math module, invoke the method through static object.


parameter

  • x - numeric expression.

return value

Returns the absolute value of the number.

Examples

The following shows an example of the use of fabs () method:

#!/usr/bin/python3
import math   # 导入 math 模块

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(math.pi) : ", math.fabs(math.pi))

After running the above example output is:

math.fabs(-45.17) :  45.17
math.fabs(100.12) :  100.12
math.fabs(100.72) :  100.72
math.fabs(math.pi) :  3.141592653589793

Python3 digital Python3 digital