Friday, June 12, 2026
HomeLanguagesPython | fabs() vs abs()

Python | fabs() vs abs()

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




# Python code to demonstrate working
# of fabs() and abs()
import math
 
#################################
# When the argument is an integer#
#################################
number = -10
 
# abs() will return an integer as
# the argument is an integer
print(abs(number))
 
# fabs() will return a floating point number
print(math.fabs(number))
 
###########################################
# When the input is a floating point number#
###########################################
number = -12.08
 
# abs() will return an floating point number
# as the argument is a floating point number
print(abs(number))
 
# fabs() will return a floating point number
print(math.fabs(number))
 
####################################
# When the input is a complex number#
####################################
number = complex(3, 4)
 
# abs() will return the magnitude
print(abs(number))
 
# fabs() will return an error
# print(math.fabs(number))


Output: 

10
10.0
12.08
12.08
5.0
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS