求绝对值函数

您所在的位置:网站首页 美国可兰保健品好不好 求绝对值函数

求绝对值函数

2024-01-04 16:42| 来源: 网络整理| 查看: 265

1.abs函数(推荐使用stdlib.h,C++可用cmath)的原型是: int abs(int x);

求x的绝对值,传入值x的类型是int型,返回值类型也是int型。

2.fabs(推荐使用math.h,C++可用cmath)函数的原型是: double fabs(double y);

求y的绝对值,传入值y的类型是float型,返回值类型也是float型。

那么他们各自在哪个头文件呢,下面详细讲解:

1.C语言中,求整数的绝对值abs()和labs()应该包含stdlib.h

求浮点数的绝对值fabs()应该包含math.h

2.在C++中,只需要包括cmath即可。

C标准:

只在stdlib.h中有定义abs():

int abs (int n);

fabs() 在math.h中

float fabs(float j); double fabs(double j); C++标准:

abs() 在stdlib.h中

int abs (int n); long int abs (long int n);

abs() 在math.h,cmath中

double abs (double x); float abs (float x); long double abs (long double x);

fabs() 在cmath中

float fabs(float j); double fabs(double j);

部分选自:https://blog.csdn.net/booksyhay/article/details/12164897



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3