十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
C語言中的POW函數(shù)使用:
目前成都創(chuàng)新互聯(lián)已為超過千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)絡(luò)空間、網(wǎng)站托管運營、企業(yè)網(wǎng)站設(shè)計、潮南網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
#includestdio.h
#defineACCURACY100
doublefunc1(doublet,intn);
doublefunc2(doubleb,intn);
doublepow2(doublea,doubleb);
intmain(){
printf("%lf",pow2(5.21,4.11));
return0;
}
doublepow2(doublea,doubleb){
if(a==0b0){
return0;
}
elseif(a==0b=0){
return1/0;
}
elseif(a0!(b-(int)b0.0001||(b-(int)b0.999))){
return1/0;
}
if(a=2a=0){
doublet=a-1;
doubleanswer=1;
for(inti=1;iACCURACY;i++){
answer=answer+func1(t,i)*func2(b,i);
}
returnanswer;
}
elseif(a2){
inttime=0;
while(a2){
a=a/2;
time++;
}
returnpow2(a,b)*pow2(2,b*time);
}
else{
if((int)b%2==0){
returnpow2(-a,b);
}
else{
return-pow2(-a,b);
}
}
}
doublefunc1(doublet,intn){
doubleanswer=1;
for(inti=0;in;i++){
answer=answer*t;
}
returnanswer;
}
doublefunc2(doubleb,intn){
doubleanswer=1;
for(inti=1;i=n;i++){
answer=answer*(b-i+1)/i;
}
returnanswer;
}
擴展資料
C++提供以下幾種pow函數(shù)的重載形式:
doublepow(doubleX,intY);
floatpow(floatX,floatY);
floatpow(floatX,intY);
longdoublepow(longdoubleX,longdoubleY);
longdoublepow(longdoubleX,intY);
使用的時候應(yīng)合理設(shè)置參數(shù)類型,避免有多個“pow”實例與參數(shù)列表相匹配的情況。
其中較容易發(fā)生重載的是使用形如:
intX,Y;
intnum=pow(X,Y);
這是一個比較常用的函數(shù),但是編譯器會提醒有多個“pow”實例與參數(shù)列表相匹配。
可以使用強制類型轉(zhuǎn)換解決這個問題:num=pow((float)X,Y);
#include?stdio.h
#include?math.h
double?exp(double?x)?{
double?sum?=?0;
double?term?=?1;
double?index?=?1;
while?(fabs(term)?=?1e-6)?{
sum?=?sum?+?term;
term?=?term?*?x?/?index;
index?=?index?+?1;
}
return?sum;
}
int?main()?{
char?c;
double?x;
while?(scanf("?%c",?c)?==?1)?{
if?(c?==?'#')?break;
ungetc(c,?stdin);
scanf("%lf",?x);
printf("e(%.3lf)?=?%.5lf\n",?x,?exp(x));
}
}
在開始加上#include math.h;
程序中就可以調(diào)用pow(x,y)。
main()
{
double z;
z=pow(10,5);
printf("%lf/n",z);
}
輸出結(jié)果:
285.000000
擴展資料
c語言求自然對數(shù)的底e的指數(shù),可以使用函數(shù)exp().
exp()的頭文件:#include
exp()的函數(shù)原型:double exp(double x);
exp()函數(shù)的作用:返回e的x次方。
exp()的相關(guān)函數(shù):float expf(float x);
long double expl(long double x);
注:自然對數(shù)的底e叫做: 歐拉數(shù)(eula's number)
用pow()函數(shù)
如:
#include stdlib.h
#include math.h
#include conio.h
void main()
{
printf("10^20=%f",pow(10.,20.));
getchar();
}
基本性質(zhì)
(1) 指數(shù)函數(shù)的定義域為R,這里的前提是a大于0且不等于1。對于a不大于0的情況,則必然使得函數(shù)的定義域不連續(xù),因此我們不予考慮,同時a等于0函數(shù)無意義一般也不考慮。
(2) 指數(shù)函數(shù)的值域為(0, +∞)。
(3) 函數(shù)圖形都是上凹的。
(4) a1時,則指數(shù)函數(shù)單調(diào)遞增;若0單調(diào)遞減的。
以上內(nèi)容參考:百度百科-指數(shù)函數(shù)