What is the usage of the fabs function in the C language?

In C language, the fabs function is used to return the absolute value of a floating-point number.

The prototype of the fabs function is as follows:

double fabs(double x);

The function will return the absolute value of the floating point number x.

Here is an example usage of the fabs function:

#include <stdio.h>
#include <math.h>

int main() {
   double num1 = -3.14;
   double num2 = 2.718;

   printf("绝对值:%f\n", fabs(num1));
   printf("绝对值:%f\n", fabs(num2));

   return 0;
}

Output:

绝对值:3.140000
绝对值:2.718000

In the above example, we used the fabs function to calculate the absolute values of num1 and num2 separately, and then displayed the results on the screen.

bannerAds