All Math methods

fabs function returns absolute value of float data type.
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

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.

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net


    Post your comments , suggestion , error , requirements etc here

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