c語言求自然對數
① C++中,自然對數怎麼表達,就是比如b=ln(a),怎麼表示
在C++/c語言中,對數函數y = lnx的表示方法為y = log(x),函數的完整原型為:double log(double x)。
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
printf("%f ",log(10));
return 0;
}
(1)c語言求自然對數擴展閱讀
C語言 log10() 函數用來求以 10 為底的對數值。
頭文件:math.h
語法/原型:double log10(double x);
參數 x 是一個雙精度數。
返回值:以 10 為底的 x 的對數值。
【實例】使用C語言 log10() 函數求以 10 為底的 40 的對數。
#include <stdio.h>
#include <math.h>
int main() {
double m = 40; //為變數賦初值
double n = log10(m); //求以10為底的參數40的對數
printf("%lf ", n);
return 0;
}
運行結果:
1.602060
② C語言編程:利用下面公式,求自然對數e的近似值 e=1+1/1!+1/2!+1/3!+............
//C語言中,求e=1/1!+1/2!+…+1/n!精確到10ˉ8
#include<stdio.h>
intmain(void)
{
longn=0,ns=1;doublex=0.0f,y=0.0f,e=1.0f;
for(;;){
n++;/*計算n*/
ns*=n;/*計算n!*/
x=ns;
y=1.0f/x;/*計算1/n!*/
if(y<1e-8)break;/*如果足夠小則停止*/
e+=y;/*計算e*/
}
printf("%9.8f ",e);/*輸出結果*/
return0;
}
參考自:http://..com/link?url=U13lLtXlkIyBeFe4_G4I15Kc20oj9A0Xp__q
③ C語言中log函數怎麼使用
先引用
#include <math.h>
假設要計算log3(9)的值,因為C語言的logx是以e為底的相當於lnx,如果以其他數作為對數的底,必須寫成
float i
i=log(9)/log(3);
④ C語言中,自然對數是怎樣表示的舉個例子
C語言中直接提供的是e為底的自然對數log,和以10為底的常用對數log10,其他對數寫個函內數就可以。
#include <stdio.h>
#include <math.h>
double loga(double n, double base);
int main (void)
{
double a, b, c;
a = log(exp(1));
b = log10(10);
c = loga(100, 5);
printf("%lf %lf %lf", a, b, c);
}
double loga(double n, double base)
{ return log(n) / log(base);}
(4)c語言求自然對數擴展閱讀:
如果一個變數名後面跟著一個有數字的中括弧,這個聲明就是數組聲明。字元串也是一種數組。它們以ASCII的NULL作為數組的結束。要特別注意的是,中括弧內的索引值是從0算起的。
C語言的字元串其實就是以'