summaryrefslogtreecommitdiffstats
path: root/Help/guide/tutorial/Step12/MathFunctions/mysqrt.cxx
diff options
context:
space:
mode:
authorBetsy McPhail <betsy.mcphail@kitware.com>2020-01-08 17:36:47 (GMT)
committerBetsy McPhail <betsy.mcphail@kitware.com>2020-01-10 16:37:54 (GMT)
commit77b515f3eb0494721824e7ee47e32621b947c67f (patch)
tree488a67b9e80de4d9ffd266bd196acd535c333862 /Help/guide/tutorial/Step12/MathFunctions/mysqrt.cxx
parent3a510a47b00ab4442ec0a983748f745190fe8b03 (diff)
downloadCMake-77b515f3eb0494721824e7ee47e32621b947c67f.zip
CMake-77b515f3eb0494721824e7ee47e32621b947c67f.tar.gz
CMake-77b515f3eb0494721824e7ee47e32621b947c67f.tar.bz2
Tutorial: Improve "MultiPackage" example
Rename to Step 12 and ensure that it follows Step 11
Diffstat (limited to 'Help/guide/tutorial/Step12/MathFunctions/mysqrt.cxx')
-rw-r--r--Help/guide/tutorial/Step12/MathFunctions/mysqrt.cxx37
1 files changed, 37 insertions, 0 deletions
diff --git a/Help/guide/tutorial/Step12/MathFunctions/mysqrt.cxx b/Help/guide/tutorial/Step12/MathFunctions/mysqrt.cxx
new file mode 100644
index 0000000..8153f18
--- /dev/null
+++ b/Help/guide/tutorial/Step12/MathFunctions/mysqrt.cxx
@@ -0,0 +1,37 @@
+#include <iostream>
+
+#include "MathFunctions.h"
+
+// include the generated table
+#include "Table.h"
+
+namespace mathfunctions {
+namespace detail {
+// a hack square root calculation using simple operations
+double mysqrt(double x)
+{
+ if (x <= 0) {
+ return 0;
+ }
+
+ // use the table to help find an initial value
+ double result = x;
+ if (x >= 1 && x < 10) {
+ std::cout << "Use the table to help find an initial value " << std::endl;
+ result = sqrtTable[static_cast<int>(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;
+}
+}
+}