mirror of
https://github.com/Kitware/CMake.git
synced 2025-10-18 08:51:52 +08:00
Help/guide/tutorial: Adopt tutorial code
This commit is contained in:

committed by
Betsy McPhail

parent
d2fde94809
commit
862cfc0e6c
23
Help/guide/tutorial/Step4/MathFunctions/mysqrt.cxx
Normal file
23
Help/guide/tutorial/Step4/MathFunctions/mysqrt.cxx
Normal file
@@ -0,0 +1,23 @@
|
||||
#include "MathFunctions.h"
|
||||
#include <iostream>
|
||||
|
||||
// a hack square root calculation using simple operations
|
||||
double mysqrt(double x)
|
||||
{
|
||||
if (x <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
double result = x;
|
||||
|
||||
// do ten iterations
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
if (result <= 0) {
|
||||
result = 0.1;
|
||||
}
|
||||
double delta = x - (result * result);
|
||||
result = result + 0.5 * delta / result;
|
||||
std::cout << "Computing sqrt of " << x << " to be " << result << std::endl;
|
||||
}
|
||||
return result;
|
||||
}
|
Reference in New Issue
Block a user