Fabs() function in C
×


Fabs() function in C

181

fabs() function returns the absolute value of the given floating-point number x.

It's declared in the math.h header file.

The argument x can be of type float, double, or long double.

The return type of fabs() matches the type of the argument provided.

It's commonly used to get the magnitude of a floating-point number regardless of its sign.

Syntax fabs() in C:

double fabs(double x);
Example:

//Program for fabs() function in C
#include<stdio.h> 
#include<math.h> 

int main() {
    double num = -10.5;
    double absolute_value = fabs(num);
    printf("Absolute value of %lf = %lf\n", num, absolute_value);
    return 0;
}
	

Output:

Absolute value of -10.500000 = 10.500000

In the provided program, fabs() is used to find the absolute value of -10.5.

The result is printed to the console.

Uses of fabs function in C:

Useful for mathematical calculations where only the magnitude of a number is relevant.

Frequently used in scientific and engineering applications for absolute value computations.

Helps in simplifying conditional logic by avoiding explicit checks for negative numbers.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments