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.
23 lines
456 B
23 lines
456 B
5 years ago
|
#include <iostream>
|
||
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
|
|
||
5 years ago
|
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
|
}
|
||
16 years ago
|
return result;
|
||
|
}
|