fabs()函數(shù)的用法:double fabs(double x)。其中參數(shù)x 是浮點(diǎn)值,這個(gè)函數(shù)返回x的絕對(duì)值。代碼示例如下:

創(chuàng)新互聯(lián)憑借專業(yè)的設(shè)計(jì)團(tuán)隊(duì)扎實(shí)的技術(shù)支持、優(yōu)質(zhì)高效的服務(wù)意識(shí)和豐厚的資源優(yōu)勢(shì),提供專業(yè)的網(wǎng)站策劃、成都做網(wǎng)站、成都網(wǎng)站建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站優(yōu)化、軟件開(kāi)發(fā)、網(wǎng)站改版等服務(wù),在成都10余年的網(wǎng)站建設(shè)設(shè)計(jì)經(jīng)驗(yàn),為成都千余家中小型企業(yè)策劃設(shè)計(jì)了網(wǎng)站。
int main (){
int a, b;
a = 1234;
b = -344;
printf("The absolute value of %d is %lf", a, fabs(a));
printf("The absolute value of %d is %lf", b, fabs(b));
return(0);}
編譯和運(yùn)行上面的程序,這將產(chǎn)生以下結(jié)果:
The absolute value of 1234 is 1234.000000
The absolute value of -344 is 344.000000
fabs()和abs()區(qū)別:
c語(yǔ)言中函數(shù)abs和fabs只有一個(gè)區(qū)別:abs函數(shù)是求整數(shù)的絕對(duì)值,函數(shù)原型是int abs(int x);fabs函數(shù)是求浮點(diǎn)數(shù)的絕對(duì)值,函數(shù)原型是float fabs(float x)。
abs函數(shù)是一種用于求絕對(duì)值的LV函數(shù)。因?yàn)閍bs(x)在0點(diǎn)的導(dǎo)數(shù)是不存在的,而對(duì)于x為復(fù)數(shù) abs(x)是不解析的,所以他的取值只能是正數(shù)或者負(fù)數(shù)。
有。C語(yǔ)言求絕對(duì)值的函數(shù)為abs( x )與fbs( x ),abs( x )包含于stdlib.h,且兩者均包含于math頭文件之下。
1、abs( x )函數(shù)
格式:int abs( int i );
作用:求整型數(shù)的絕對(duì)值
例子:
#includestdio.h
#include stdlib.h
#includemath.h
main(? ?)
{
int a = 1, b = -2 ;
printf("%d的絕對(duì)值是%d,%d的絕對(duì)值是%d\n", a, abs( a ), b, abs( b ));
}
運(yùn)行結(jié)果為:1的絕對(duì)值是1,-2的絕對(duì)值是2
2、fabs( x )函數(shù)
格式:float fabs( float i ); / double fabs( double x );
作用:求浮點(diǎn)數(shù)的絕對(duì)值
例子:
#includestdio.h
#includemath.h
main(? ?)
{
float a = 1.4, b = -2.7 ;
printf("%f的絕對(duì)值是%f,%f的絕對(duì)值是%f\n", a, fabs( a ), b, fabs( b ));
}
運(yùn)行結(jié)果為:1.400000的絕對(duì)值是1.400000,-2.700000的絕對(duì)值是2.700000
擴(kuò)展資料:
其他math.h頭文件包含函數(shù)介紹:
1、 三角函數(shù)
double sin(double);正弦
double cos(double);余弦
double tan(double);正切
2 、反三角函數(shù)
double asin (double); 結(jié)果介于[-PI/2,PI/2]
double acos (double); 結(jié)果介于[0,PI]
double atan (double); 反正切(主值),結(jié)果介于[-PI/2,PI/2]
double atan2 (double,double); 反正切(整圓值),結(jié)果介于[-PI,PI]
3 、雙曲三角函數(shù)
double sinh (double);
double cosh (double);
double tanh (double);
4 、指數(shù)與對(duì)數(shù)
double frexp(double value,int *exp);這是一個(gè)將value值拆分成小數(shù)部分f和(以2為底的)指數(shù)部分exp,并返回小數(shù)部分f,即f*2^exp。其中f取值在0.5~1.0范圍或者0。
double ldexp(double x,int exp);這個(gè)函數(shù)剛好跟上面那個(gè)frexp函數(shù)功能相反,它的返回值是x*2^exp
double modf(double value,double *iptr);拆分value值,返回它的小數(shù)部分,iptr指向整數(shù)部分。
double log (double); 以e為底的對(duì)數(shù)
double log10 (double);以10為底的對(duì)數(shù)
double pow(double x,double y);計(jì)算x的y次冪
float powf(float x,float y); 功能與pow一致,只是輸入與輸出皆為單精度浮點(diǎn)數(shù)
double exp (double);求取自然數(shù)e的冪
double sqrt (double);開(kāi)平方根
5 、取整
double ceil (double); 取上整,返回不比x小的最小整數(shù)
double floor (double); 取下整,返回不比x大的最大整數(shù),即高斯函數(shù)[x]
1. C語(yǔ)言的庫(kù)函數(shù)中提供了求絕對(duì)值的函數(shù),函數(shù)名為 abs
2. 函數(shù)的頭文件:#include
3. 函數(shù)原型:int abs (int j);
4. 函數(shù)說(shuō)明:abs()用來(lái)計(jì)算參數(shù)j 的絕對(duì)值,然后將結(jié)果返回。
5. 返回值:返回參數(shù)j 的絕對(duì)值結(jié)果。
c語(yǔ)言中取絕對(duì)值的函數(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
#include stdio.h
int main()
{
int a;
scanf("%d",a);
if(a0)
? a=-a;
printf("%d\n",a);
return 0;
}
//求絕對(duì)值
#include iostream
#include iomanip
#includecmath
#define PI 3.1415927
using namespace std;
int main()
{? ?//C++求絕對(duì)值:如果是整形的,就是abs(),如果是浮點(diǎn)型的,是fabs()
double r;
while(cinr){
coutsetprecision(2)std::fixedfabs(r)endl;
}
return 0;
}
擴(kuò)展資料:
c語(yǔ)言中取絕對(duì)值的函數(shù)
不同類型的數(shù)據(jù)使用不同類型的絕對(duì)值函數(shù):
1、整型:
int abs(int i) ?//返回整型參數(shù)i的絕對(duì)值 12
2、復(fù)數(shù):
double cabs(struct complex znum) ?//返回復(fù)數(shù)znum的絕對(duì)值 ?1
3、雙精度浮點(diǎn)型:
double fabs(double x) ?//返回雙精度參數(shù)x的絕對(duì)值 ? ?1
4、長(zhǎng)整型:
long labs(long n) ?//返回長(zhǎng)整型參數(shù)n的絕對(duì)值
當(dāng)前標(biāo)題:求絕對(duì)函數(shù)c語(yǔ)言,求絕對(duì)值的c語(yǔ)言函數(shù)
文章源于:http://chinadenli.net/article45/dsgseei.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、定制網(wǎng)站、App設(shè)計(jì)、軟件開(kāi)發(fā)、網(wǎng)站改版、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)