diff options
| author | [email protected] <[email protected]> | 2011-05-21 07:34:02 +0000 |
|---|---|---|
| committer | [email protected] <[email protected]> | 2011-05-21 07:34:02 +0000 |
| commit | 18476d97371a255cd66e96216194318bd4562e1f (patch) | |
| tree | 871c153f9e02785adfbd8a0aa483cbe6ff0fa522 /components | |
| parent | 0050c8b708e2c59961879c9b6b0519fa25269bea (diff) | |
add more math function
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1420 bbd45198-f89e-11dd-88c7-29a3b14d5316
Diffstat (limited to 'components')
| -rw-r--r-- | components/libc/newlib/math.c | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/components/libc/newlib/math.c b/components/libc/newlib/math.c index cfd3763686..70db155236 100644 --- a/components/libc/newlib/math.c +++ b/components/libc/newlib/math.c @@ -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));
+}
+
|
