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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
#include <ciso646>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
//----------------------------------------------------------------------------
// The recursive way
long double an( long double x, unsigned n )
{
long double a0 = 1;
long double g0 = sqrt( 1 + x*x );
while (n--)
{
a0 = 0.5 * (a0 + g0);
g0 = sqrt( a0*g0 );
}
return a0;
}
long double d( long double x, unsigned k, unsigned n )
{
if (k == 0) return an( x, n );
long double d0 = d( x, k-1, n );
long double d1 = d( x, k-1, n-1 );
long double p = 1/pow( 2.0, 2 * k );
return (d0 - (p * d1)) / (1 - p);
}
long double arctan_r( long double x, unsigned n = 10 )
{
return x / d( x, n, n );
}
//----------------------------------------------------------------------------
// Now to tweak it up fine
long double arctan( long double x, unsigned N = 10 )
{
if (N < 2) N = 1;
long double a0, g0; // ongoing calculation of a(n)
vector <long double> d( N+1 ); // results of d(k,n), where d[k] == d(k,n) for largest n
g0 = sqrt( 1 + (x * x) );
a0 = 0.5 * (1 + g0);
g0 = sqrt( a0 * g0 );
d[ 0 ] = a0; // d[0] == d(0,1) == a(1)
d[ 1 ] = (a0 - 0.25) / 0.75; // d[1] == d(1,1)
// for n = 2 to N
for (unsigned n = 1; n++ < N; )
{
// calc a(n)
a0 = 0.5 * (a0 + g0);
g0 = sqrt( a0 * g0 );
long double d0 = a0;
// for k = 1 to n
for (unsigned k = 0; k++ < n; )
{
// d0 = d(k-1,n)
// d[k-1] = d(k-1,n-1)
// p = 1/(2**(2*k))
// d(k,n) = (d0 - (p * d[k-1])) / (1 - p)
long double p = 1 / pow( 2.0, 2*k );
long double d1 = (d0 - (p * d[ k-1 ])) / (1 - p);
d[ k-1 ] = d0;
d0 = d1;
}
d[ n ] = d0;
}
return x / d[ N ];
}
//----------------------------------------------------------------------------
int main( int argc, char** argv )
{
double x;
int n = 8;
if (argc == 3)
{
string s( argv[ 2 ] );
istringstream ss( s );
ss >> n;
argc = 2;
}
if (argc == 2)
{
string s( argv[ 1 ] );
istringstream ss( s );
ss >> x;
if (!ss) argc = 1;
}
if (argc != 2)
{
cout << "calculate atan(x); input x> ";
cin >> x;
if (!cin) { cout << "fooey!\n"; return 1; }
cin >> n;
}
cout << setprecision( numeric_limits <long double> ::digits10 + 1 );
cout << "recursive = " << arctan_r( x, n ) << "\n";
cout << "iterative = " << arctan( x, n ) << "\n";
cout << "standard = " << atan( x ) << "\n";
cout << "test iterations? ";
unsigned long iterations;
{
string s;
getline( cin, s );
istringstream ss( s );
ss >> iterations;
if (ss and iterations)
{
clock_t time;
time = clock();
for (unsigned long n = 0; n < iterations; n++)
arctan( x, n );
time = clock() - time;
cout << "iterative * " << iterations << " = "
<< ((long double)time/CLOCKS_PER_SEC) << " seconds\n";
time = clock();
for (unsigned long n = 0; n < iterations; n++)
arctan_r( x, n );
time = clock() - time;
cout << "recursive * " << iterations << " = "
<< ((long double)time/CLOCKS_PER_SEC) << " seconds\n";
}
}
return 0;
}
|