Python math.fabs() Method

Python math 模块Python math Module

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

The absolute value is a non-negative number; if there is a negative sign, it will be removed.

Unlike Python's built-inabs()different, this method always converts the value to a floating-point value.

Syntax

The syntax of the math.fabs() method is as follows:

math.fabs(x)

Parameter description:

  • x-- Required, number. If x is not a number, returns TypeError.

Return Value

Returns a floating-point number representing the absolute value of x.

Example

The following example returns the absolute value of a number:

Example

# Import math package
import math

# Output absolute value
print(math.fabs(2.77))
print(math.fabs(8.32))
print(math.fabs(-99.29))

Output result:

2.77
8.32
99.29

Python math 模块Python math Module

Other extensions