1. C語言的庫函數(shù)中提供了求絕對值的函數(shù),函數(shù)名為 abs
成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),靈壽企業(yè)網(wǎng)站建設(shè),靈壽品牌網(wǎng)站建設(shè),網(wǎng)站定制,靈壽網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,靈壽網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
2. 函數(shù)的頭文件:#include
3. 函數(shù)原型:int abs (int j);
4. 函數(shù)說明:abs()用來計(jì)算參數(shù)j 的絕對值,然后將結(jié)果返回。
5. 返回值:返回參數(shù)j 的絕對值結(jié)果。
c語言中取絕對值的函數(shù)
*?? ABS.C:?? This?? program?? computes?? and?? displays
*?? the?? absolute?? values?? of?? several?? numbers.
#include???? stdio.h
#include???? math.h
#include???? stdlib.h
void?? main(?? void?? )
{int???????? ix?? =?? -4,?? iy;
long?????? lx?? =?? -41567L,?? ly;
double?? dx?? =?? -3.141593,?? dy;
iy?? =?? abs(?? ix?? );
printf(?? "The?? absolute?? value?? of?? %d?? is?? %d/n",?? ix,?? iy);
ly?? =?? labs(?? lx?? );
printf(?? "The?? absolute?? value?? of?? %ld?? is?? %ld/n",?? lx,?? ly);
dy?? =?? fabs(?? dx?? );
printf(?? "The?? absolute?? value?? of?? %f?? is?? %f/n",?? dx,?? dy?? );
Output
The?? absolute?? value?? of?? -4?? is?? 4
The?? absolute?? value?? of?? -41567?? is?? 41567
The?? absolute?? value?? of?? -3.141593?? is?? 3.141593
用abs()函數(shù)表示,其步驟如下:
需要準(zhǔn)備的材料分別有:電腦、C語言編譯器。
1、首先,打開C語言編譯器,新建一個(gè)初始.cpp文件,例如:test.cpp。
2、其次,在test.cpp文件中,輸入C語言代碼:printf("%d", abs(-9));。
3、最后,編譯器運(yùn)行test.cpp文件,此時(shí)成功表示了-9的絕對值并打印了出來。
//求絕對值
#include iostream
#include iomanip
#includecmath
#define PI 3.1415927
using namespace std;
int main()
{? ?//C++求絕對值:如果是整形的,就是abs(),如果是浮點(diǎn)型的,是fabs()
double r;
while(cinr){
coutsetprecision(2)std::fixedfabs(r)endl;
}
return 0;
}
擴(kuò)展資料:
c語言中取絕對值的函數(shù)
不同類型的數(shù)據(jù)使用不同類型的絕對值函數(shù):
1、整型:
int abs(int i) ?//返回整型參數(shù)i的絕對值 12
2、復(fù)數(shù):
double cabs(struct complex znum) ?//返回復(fù)數(shù)znum的絕對值 ?1
3、雙精度浮點(diǎn)型:
double fabs(double x) ?//返回雙精度參數(shù)x的絕對值 ? ?1
4、長整型:
long labs(long n) ?//返回長整型參數(shù)n的絕對值