#includestdio.h
十載品牌的成都網(wǎng)站建設(shè)公司,成百上千家企業(yè)網(wǎng)站設(shè)計經(jīng)驗.價格合理,可準確把握網(wǎng)頁設(shè)計訴求.提供定制網(wǎng)站建設(shè)、商城網(wǎng)站制作、成都小程序開發(fā)、響應式網(wǎng)站設(shè)計等服務(wù),我們設(shè)計的作品屢獲殊榮,是您值得信賴的專業(yè)的建站公司。
#include math.h
void main()
{
float x=5,y;
y=log(x);
printf("%f\n",y);
}
擴展資料:
C語言中使用對數(shù)函數(shù)的方法
log()函數(shù):返回以e為底的對數(shù)值
頭文件:
1#include
log() 函數(shù)返回以 e 為底的對數(shù)值,其原型為:
1double?log?(double?x);
log()用來計算以e為底的 x 的對數(shù)值,然后將結(jié)果返回。設(shè)返回值為 ret,則
1x = eret
如果 x 為負數(shù)或 0,則會發(fā)生錯誤并設(shè)置 errno 值。錯誤代碼:
EDOM:參數(shù)x 為負數(shù);
ERANGE:參數(shù)x
為零值,零的對數(shù)值無定義。
注意:使用 GCC 編譯時請加入-lm。
x的自然對數(shù)用log(x)表示
常用對數(shù)用log10(x)表示
#includestdio.h
#includemath.h
int main()
{int i;
for(i=1;i=10;i++)
printf("log10(%d)=%lf\n",i,log10(i));
return 0;
}
#includestdio.h
#includemath.h
intmain(){
printf("%f\n",log(10));//以e為底的對數(shù)函數(shù)
printf("%f\n",log10(100));//以10為底的對數(shù)函數(shù)
printf("%f\n",log(8)/log(2));//計算log2^8,運用換底公式
printf("%f\n",exp(1));//計算自然常數(shù)e
return0;
}
擴展資料
模擬一個log日志的寫入
#includestdio.h
#includestdarg.h
#includetime.h
intwrite_log(FILE*pFile,constchar*format,…)
{
va_listarg;
intdone;
va_start(arg,format);
time_ttime_log=time(NULL);
structtm*tm_log=localtime(time_log);
fprintf(pFile,"%04d-%02d-%02d%02d:%02d:%02d",tm_log-tm_year+1900,tm_log-tm_mon+1,tm_log-tm_mday,tm_log-tm_hour,tm_log-tm_min,tm_log-tm_sec);
done=vfprintf(pFile,format,arg);
va_end(arg);
fflush(pFile);
returndone;
}
intmain()
{
FILE*pFile=fopen(“123.txt”,“a”);
write_log(pFile,"%s%d%f\n","isrunning",10,55.55);
fclose(pFile);
return0;
}
原型:double log (double x);
頭文件:math.h
功能:計算以e 為底的對數(shù)值
程序例:
#include math.h
#include stdio.h
int main(void)
{
double result;
double x = 321.123;
result = log(x);
printf("The common log of %lf is %lf\n", x, result);
return 0;
}
C語言里面有該函數(shù),所以輸入一個雙精度浮點數(shù),對其進行函數(shù)變換即可生成其對數(shù)。
還有如果你的意思是輸入對數(shù)進行冪運算的話有下面這個函數(shù)
原型:extern float pow(float x, float y);
用法:#include math.h
功能:計算x的y次冪。
說明:x應大于零,返回冪指數(shù)的結(jié)果。
舉例:
// pow.c
#include stdlib.h
#include math.h
#include conio.h
void main()
{
printf("4^5=%f",pow(4.,5.));
getchar();
}
首先在C語言中要用到指數(shù)、對數(shù)的相關(guān)公式,需要引入math.h。另外ln是以e為底數(shù),lg是以10為底數(shù)。
代碼如下:
#includestdio.h
#includemath.h
void main()
{
double exponent, base;
exponent = 3.14;
printf("ln(%f) = %.2f\n", exponent, log(exponent));//以e為底數(shù)的對數(shù)
exponent = 100;
printf("lg(%.f) = %.2f\n", exponent, log10(exponent));//以10為底數(shù)的對數(shù)
base = 5, exponent = 100;
printf("log_%.f(%.f) = %.2f\n", base, exponent, log(exponent)/log(base));//換底公式
return 0;
}
在求log_5(100)時需要用到“換底公式”:log_5(100) = ln(100)/ln(5)。
擴展資料:
math.h文件中包含的函數(shù)主要分為以下幾類:
1、三角函數(shù)、反三角函數(shù)、雙曲三角函數(shù)。
2、指數(shù)、對數(shù)。
3、取整、絕對值。
4、標準化浮點數(shù)。
涉及參數(shù)類型為double類型。
參考資料:
百度百科——換底公式
百度百科——math.h
Log(number)\x0d\x0a必要的 number 參數(shù)是 Double 或任何有效的大于 0 的數(shù)值表達式。\x0d\x0a說明\x0d\x0a自然對數(shù)是以 e 為底的對數(shù)。常數(shù) e 的值大約是 2.718282。\x0d\x0a如下所示,將 x 的自然對數(shù)值除以 n 的自然對數(shù)值,就可以對任意底 n 來計算數(shù)值 x 的對數(shù)值:\x0d\x0aLogn(x) = Log(x) / Log(n)\x0d\x0a下面的示例說明如何編寫一個函數(shù)來求以 10 為底的對數(shù)值:\x0d\x0aStatic Function Log10(X)\x0d\x0a Log10 = Log(X) / Log(10#)\x0d\x0aEnd Function\x0d\x0a本示例使用 Log 函數(shù)得到某數(shù)的自然對數(shù)值。\x0d\x0a\x0d\x0aDim MyAngle, MyLog\x0d\x0a' 定義角度(以“弧度”為單位)。\x0d\x0aMyAngle = 1.3' 計算反雙曲正弦函數(shù)值(inverse sinh())。\x0d\x0aMyLog = Log(MyAngle + Sqr(MyAngle * MyAngle + 1))
分享題目:log函數(shù)在c語言上輸入,c語言如何調(diào)用log函數(shù)
URL網(wǎng)址:http://aaarwkj.com/article18/dssjigp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、標簽優(yōu)化、建站公司、動態(tài)網(wǎng)站、網(wǎng)站導航、網(wǎng)站維護
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)