blob: d547568a1025916e15eb0b051d3ca94c42eeaf15 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
#ifndef ABS
#define ABS(a) (((a) < 0) ? -(a) : (a))
#endif
#define MINIMUM 0.000000000001 /* e-12 */
/* -----------------------------------------------------------------------
mySqrt uses Heron's algorithm, which is the Newtone-Raphson algorhitm
in it's private case for sqrt.
Thanks BlueChip for his intro text and Dave Straayer for the actual name.
----------------------------------------------------------------------- */
double sqrt(double square)
{
int k = 0;
double temp = 0;
double root= ABS(square+1)/2;
while( ABS(root - temp) > MINIMUM ){
temp = root;
root = (square/temp + temp)/2;
k++;
if (k>10000) return 0;
}
return root;
}
/*Uses the sequence sum(x^k/k!) that tends to exp(x)*/
double exp (double x) {
unsigned int k=0;
double res=0, xPow=1,fact=1,toAdd;
do {
toAdd = xPow/fact;
res += toAdd;
xPow *= x; //xPow = x^k
k++;
fact*=k; //fact = k!
} while (ABS(toAdd) > MINIMUM && xPow<1e302);
return res;
}
|