From 18476d97371a255cd66e96216194318bd4562e1f Mon Sep 17 00:00:00 2001 From: "qiuyiuestc@gmail.com" Date: Sat, 21 May 2011 07:34:02 +0000 Subject: [PATCH] add more math function git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1420 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- components/libc/newlib/math.c | 92 +++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) 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<0) + { + for(i=0; x<0.7; i++) + { + x = sqrt(x); + } + return (1< 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)); +} + -- GitLab