Both the abs() and the fabs() function is used to find the absolute value of a number, i.e., remove the negative sign of a number.
Syntax of abs():
abs(number)
Syntax of fabs():
math.fabs(number)
Both will return the absolute value of a number.
The difference is that math.fabs(number) will always return a floating-point number even if the argument is an integer, whereas abs() will return a floating-point or an integer depending upon the argument.
In case the argument is a complex number, abs() will return the magnitude part whereas fabs() will return an error.
To use the fabs() function we need to import the library “math” while the abs() function comes with the standard library of Python.
Python3
import math
number = -10
print(abs(number))
print(math.fabs(number))
number = -12.08
print(abs(number))
print(math.fabs(number))
number = complex(3, 4)
print(abs(number))
|
Output:
10
10.0
12.08
12.08
5.0
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!