add more math function

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1420 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
qiuyiuestc@gmail.com 2011-05-21 07:34:02 +00:00
parent 0050c8b708
commit 18476d9737
1 changed files with 92 additions and 0 deletions

View File

@ -166,3 +166,95 @@ double cos(double x)
return result; 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));
}