English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C ++The fabs() function returns the absolute value of the parameter.
It is<cmath>Defined in the header file.
|x| = fabs(x)
double fabs(double x); float fabs(float x); long double fabs(long double x); double fabs(T x); // For integral type
The fabs() function has only one parameter and returns a value of type double, float, or long double.
The fabs() function uses a single parameter x and returns its absolute value.
The fabs() function returns the absolute value of x, that is, |x|.
#include <iostream> #include <cmath> using namespace std; int main() { double x = -10.25, result; result = fabs(x); cout << "fabs(" << x << ") = |" << x << "| = " << result << endl; return 0; }
When running the program, the output is:
fabs(-10.25) = |-10.25| = 10.25
#include <iostream> #include <cmath> using namespace std; int main() { long int x = -23; double result; result = fabs(x); cout << "fabs(" << x << ") = |" << x << "| = " << result << endl; return 0; }
When running the program, the output is:
fabs(-23) = |-23| = 23