fabs() is a method specified in math library in Python 2 and Python 3.
Sometimes while computing the difference between 2 numbers to compute the closeness of a number with the other number, we require to find the magnitude of certain number, fabs() can come handy in the cases where we are dealing with ints and want the result in float number to perform floating point comparisons further as fabs() converts its every magnitude to floating point value.
Syntax : fabs(x)
x : Number whose magnitude has to be computed.
Returns : Returns the magnitude of the element passed in the function. Always returns a floating point number, irrespective of the data type of the argument number.
Code #1 : Code to demonstrate fabs() working
The magnitude of integer is : 2.0 The type of output is : The magnitude of float is : 2.0 The type of output is :
There are many exceptions associated with this method, since it always returns a floating point number, this function throws an exception when python cannot convert the argument to floating point number. For eg. in case of string and complex numbers.
Code #2 : Code to demonstrate exceptions of fabs()
The absolute value of string is : Error !! The error on passing string is : a float is required The absolute value of complex is : Error !! The error on passing complex is : can't convert complex to float
- Python | fabs() vs abs()
- numpy.fabs() in Python
- Python | math.fabs() function
- Python Number | uniform() method
- Python Number | cmp(list) method
- Python | Find the Number Occurring Odd Number of Times using Lambda expression and reduce function
- class method vs static method in Python
- 21 Number game in Python
- Python | Superfactorial of a number.
- Python | set() method
- Python | os.dup() method
- Python | next() method
- Python | Number Theoretic Transformation
- Python | Insert a number in string
- Number guessing game in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.