它计算给定数字的绝对值。
假设数字为'x':
fabs(x) = |x|;
float fabs(float x); double fabs(double x); int fabs(int x);
x :要确定其绝对值的值。
它返回x的绝对值。
让我们看一个简单的例子,当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的绝对值。
链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-math-fabs-function.html
来源:LearnFk无涯教程网
让我们看一个简单的例子,当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的绝对值。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)