fabs() Absolute value of number

Math

fabs function returns absolute value of float dtype.
Here are some examples with different type of numbers.
import math
print(math.fabs(-4))    # 4.0
print(math.fabs(-4.6))  # 4.6

print(math.fabs(4))     # 4.0
print(math.fabs(4.6))   # 4.6
Data type of the output is float.
import math
print(type(math.fabs(-4)))    # <class 'float'>
print(type(math.fabs(-4.6)))  # <class 'float'>

print(type(math.fabs(4)))     # <class 'float'>
print(type(math.fabs(4.6)))   # <class 'float'>

Complex Number

print(math.fabs(4+8j))
This will generate error message saying TypeError: can't convert complex to float.

Using abs()

We can use built in function abs() to get absolute value of complex number.
print(abs(4+8j))  # 8.94427190999916
print(type(abs(4+8j)))  # <class 'float'>

Difference between fabs() and abs()

fabs() is included in Python math module so we have to import it before using. Where as abs() is part of built in functions of core Python so no need to import any library.

fabs() returns float dtype. Whenever input number can’t be converted to float , fabs() generates error. However abs() returns the same dtype of input number.

abs()


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer