Home C&C++函数库 c++ 语法 程序源码 Linux C库

abs

函数  <cmath> <ctgmath>

C98 double abs (double x);
float abs (float x );
long double abs (long double x);
C++11 double abs (double x);
float abs (float x);
long double abs (long double x);
double abs (T x);        // additional overloads for integral types

计算绝对值
返回x的绝对值:|x|。
这些abs重载在c++中是不存在的。 在C语言中,abs (cstdlib)只在<stdlib.h>中声明(并且对int值进行操作)。
从c++ 11开始,这个头文件(<cmath>)为整型提供了额外的重载: 这些重载有效地在计算前将x转换为double(在T是任何整型时定义)。

☲  参数


x
返回其绝对值的值。

☉  返回值



x的绝对值。

☣  示例



// cmath's abs example
#include <iostream>     // std::cout
#include <cmath>        // std::abs

int main ()
{
  std::cout << "abs (3.1416) = " << std::abs (3.1416) << '\n';
  std::cout << "abs (-10.6)  = " << std::abs (-10.6) << '\n';
  return 0;
}

输出:
abs (3.1416) = 3.1416
abs (-10.6) = 10.6

🍄  另请参阅



abs (cstdlib) 绝对值(function )
labs 绝对值(function )
fabs 绝对值(function )


联系我们 免责声明 关于CandCplus 网站地图