C++ Math fabs()
C++ Math fabs()
它计算给定数字的绝对值。
假设数字为'x':
语法
float fabs(float x);
double fabs(double x);
int fabs(int x);
参数
x : 要确定其绝对值的值。
返回值
它返回x的绝对值。
示例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的绝对值。
示例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的绝对值。