You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
755 B
33 lines
755 B
5 years ago
|
#include <cmath>
|
||
5 years ago
|
#include <iostream>
|
||
16 years ago
|
|
||
5 years ago
|
#include "MathFunctions.h"
|
||
16 years ago
|
|
||
|
// a hack square root calculation using simple operations
|
||
|
double mysqrt(double x)
|
||
|
{
|
||
9 years ago
|
if (x <= 0) {
|
||
16 years ago
|
return 0;
|
||
9 years ago
|
}
|
||
12 years ago
|
|
||
16 years ago
|
// if we have both log and exp then use them
|
||
5 years ago
|
#if defined(HAVE_LOG) && defined(HAVE_EXP)
|
||
|
double result = exp(log(x) * 0.5);
|
||
5 years ago
|
std::cout << "Computing sqrt of " << x << " to be " << result
|
||
|
<< " using log and exp" << std::endl;
|
||
5 years ago
|
#else
|
||
|
double result = x;
|
||
16 years ago
|
|
||
|
// do ten iterations
|
||
5 years ago
|
for (int i = 0; i < 10; ++i) {
|
||
9 years ago
|
if (result <= 0) {
|
||
16 years ago
|
result = 0.1;
|
||
|
}
|
||
5 years ago
|
double delta = x - (result * result);
|
||
9 years ago
|
result = result + 0.5 * delta / result;
|
||
5 years ago
|
std::cout << "Computing sqrt of " << x << " to be " << result << std::endl;
|
||
9 years ago
|
}
|
||
5 years ago
|
#endif
|
||
16 years ago
|
return result;
|
||
|
}
|