C++ Reference: Standard C++ Library reference: C Library: cmath: abs

发布于:2022-11-28 ⋅ 阅读:(248) ⋅ 点赞:(0)

C++官网参考链接:https://cplusplus.com/reference/cmath/abs/

函数
<cmath> <ctgmath>
abs
C++98
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只在<stdlib.h>中声明(并作用于int值)。
从C++11开始,在这个头文件(<cmath>)中为整型(integral types)提供了额外的重载:这些重载在计算之前有效地将x转换为double类型(定义为T为任何整型(integral types))。

形参
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(cstdlib)    Absolute value (function) (绝对值(函数)) 
fabs            Compute absolute value (function) (计算绝对值(函数)) 
labs            Absolute value (function) (绝对值(函数)) 


网站公告

今日签到

点亮在社区的每一天
去签到