fabs() Function in C
Last Updated :
09 Feb, 2023
fabs() function of math.h header file in C programming is used to get the absolute value of a floating point number. This function returns the absolute value in double.
Syntax:
double fabs (double a);
Parameter: It will take a single parameter which is to be converted to an absolute value.
Return Value: While passing values to fabs(), we can convert the number explicitly to double.
Time Complexity: O(1)
Auxiliary Space: O(1)
Example:
int marks = 90;
double percent;
percent = fabs(double(marks)); // This will convert marks to double type explicitly.
Example 1: Below is the C program to show the use of the fabs() function.
C
#include <math.h>
#include <stdio.h>
int main()
{
double a = 980;
double b = -1231;
double res;
res = fabs (a);
printf ( "The absolute value of %.3lf is %.3lf\n" ,
a, res);
res = fabs (b);
printf ( "The absolute value of %.3lf is %.3lf\n" ,
b, res);
return 0;
}
|
Output
The absolute value of 980.000 is 980.000
The absolute value of -1231.000 is 1231.000
Example 2: Below is the C program to show what happens when the fabs() function is used for int & long double numbers.
C
#include <math.h>
#include <stdio.h>
int main()
{
long double a = -7.546;
long double b = 4.980;
double res;
res = fabs (a);
printf ( "The absolute value of %.3lf is %.3lf\n" ,
a, res);
res = fabs (b);
printf ( "The absolute value of %.3lf is %.3lf\n" ,
b, res);
return 0;
}
|
Output
The absolute value of 7.546 is 0.000
The absolute value of 4.980 is 0.000
There are similar functions like abs() and labs() inside stdlib.h header file in C programming language.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...