提交 18476d97 编写于 作者: qiuyiuestc's avatar qiuyiuestc

add more math function

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1420 bbd45198-f89e-11dd-88c7-29a3b14d5316
上级 0050c8b7
......@@ -166,3 +166,95 @@ double cos(double x)
return result;
}
static const int N = 100;
double coef(int n)
{
double t;
if (n == 0)
{
return 0;
}
t = 1.0/n;
if (n%2 == 0)
{
t = -t;
}
return t;
}
double horner(double x)
{
double u = coef(N);
int i;
for(i=N-1; i>=0; i--)
{
u = u*x + coef(i);
}
return u;
}
double sqrt(double b)
{
double x = 1;
int step = 0;
while ((x*x-b<-0.000000000000001 || x*x-b>0.000000000000001) && step<50)
{
x = (b/x+x)/2.0;
step++;
}
return x;
}
double ln(double x)
{
int i;
if (x > 1.5)
{
for(i=0; x>1.25; i++)
{
x = sqrt(x);
}
return (1<<i)*horner(x-1);
}
else if (x<0.7 && x>0)
{
for(i=0; x<0.7; i++)
{
x = sqrt(x);
}
return (1<<i)*horner(x-1);
}
else if(x > 0)
{
return horner(x-1);
}
}
double exp(double x)
{
double sum = 1;
int i;
for(i=N; i>0; i--)
{
sum /= i;
sum *= x;
sum += 1;
}
return sum;
}
double pow(double m, double n)
{
return exp(n*ln(m));
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册