求绝对值函数 —— abs() 函数,fabs() 函数及其头文件

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值