summaryrefslogtreecommitdiffstats
path: root/Help/guide/tutorial/Step12/MathFunctions/mysqrt.cxx
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2020-01-14 14:06:26 (GMT)
committerKitware Robot <kwrobot@kitware.com>2020-01-14 14:06:40 (GMT)
commit710371ca2f5032462e8dc352da869834bb421261 (patch)
tree10aaf87b7b2cedfc27a80ee0f55aa5aa9bea3071 /Help/guide/tutorial/Step12/MathFunctions/mysqrt.cxx
parenta2a91eb1416a5a48b7ba55973f3805998d0a643a (diff)
parenta7d25358a0ad1a9d8c80413dd620e773363e1b76 (diff)
downloadCMake-710371ca2f5032462e8dc352da869834bb421261.zip
CMake-710371ca2f5032462e8dc352da869834bb421261.tar.gz
CMake-710371ca2f5032462e8dc352da869834bb421261.tar.bz2
Merge topic 'tutorial'
a7d25358a0 Tutorial: Add the PRIVATE keyword to target_link_libraries command cf2afb1065 Tutorial: Remove 'Consumer' example 77b515f3eb Tutorial: Improve "MultiPackage" example 3a510a47b0 Tutorial: Add links to relevant CMake documentation Acked-by: Kitware Robot <kwrobot@kitware.com> Merge-request: !4113
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;
+}
+}
+}