它计算给定数字的绝对值。
假设数字为x:
fabs(x) = |x|;
fabs - 语法
float fabs(float x); double fabs(double x); int fabs(int x);
fabs - 参数
x :要确定其绝对值的值。
fabs - 返回值
它返回x的绝对值。
fabs - 例子1
让我们看一个简单的例子,当x的值为正时。
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float x=11.2;
std::cout << "Value of x is :" <<x<< std::endl;
cout<<"Absolute value of x is : "<<fabs(x);
return 0;
}
输出:
Value of x is :11.2 Absolute value of x is : 11.2
在此示例中,fabs()函数确定x = 11.2的绝对值。
fabs - 例子2
让我们看一个简单的例子,当x的值为负数时。
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float x=-9.4;
std::cout << "Value of x is :" <<x<< std::endl;
cout<<"Absolute value of x is : "<<fabs(x);
return 0;
}
输出:
Value of x is :-9.4 Absolute value of x is : 9.4
在此示例中,当x的值等于-9.4时,fabs()函数计算x的绝对值。