summaryrefslogtreecommitdiffstats
path: root/Tests/Tutorial/Step6/MathFunctions
diff options
context:
space:
mode:
Diffstat (limited to 'Tests/Tutorial/Step6/MathFunctions')
-rw-r--r--Tests/Tutorial/Step6/MathFunctions/CMakeLists.txt32
-rw-r--r--Tests/Tutorial/Step6/MathFunctions/MakeTable.cxx35
-rw-r--r--Tests/Tutorial/Step6/MathFunctions/mysqrt.cxx31
3 files changed, 37 insertions, 61 deletions
diff --git a/Tests/Tutorial/Step6/MathFunctions/CMakeLists.txt b/Tests/Tutorial/Step6/MathFunctions/CMakeLists.txt
index 70a35f6..2946075 100644
--- a/Tests/Tutorial/Step6/MathFunctions/CMakeLists.txt
+++ b/Tests/Tutorial/Step6/MathFunctions/CMakeLists.txt
@@ -1,24 +1,14 @@
-# first we add the executable that generates the table
-add_executable(MakeTable MakeTable.cxx)
-
-# add the command to generate the source code
-add_custom_command (
- OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.h
- DEPENDS MakeTable
- COMMAND MakeTable
- ARGS ${CMAKE_CURRENT_BINARY_DIR}/Table.h
- )
-
-set_source_files_properties (
- mysqrt.cxx PROPERTIES
- OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/Table.h
- )
+add_library(MathFunctions mysqrt.cxx)
-# add the binary tree directory to the search path for include files
-include_directories( ${CMAKE_CURRENT_BINARY_DIR} )
+# state that anybody linking to us needs to include the current source dir
+# to find MathFunctions.h, while we don't.
+# state that we depend on Tutorial_BINARY_DIR but consumers don't, as the
+# TutorialConfig.h include is an implementation detail
-# add the main library
-add_library(MathFunctions mysqrt.cxx)
+target_include_directories(MathFunctions
+ INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
+ PRIVATE ${Tutorial_BINARY_DIR}
+ )
-install (TARGETS MathFunctions DESTINATION bin)
-install (FILES MathFunctions.h DESTINATION include)
+install(TARGETS MathFunctions DESTINATION lib)
+install(FILES MathFunctions.h DESTINATION include)
diff --git a/Tests/Tutorial/Step6/MathFunctions/MakeTable.cxx b/Tests/Tutorial/Step6/MathFunctions/MakeTable.cxx
index cebd50f..ee58556 100644
--- a/Tests/Tutorial/Step6/MathFunctions/MakeTable.cxx
+++ b/Tests/Tutorial/Step6/MathFunctions/MakeTable.cxx
@@ -1,32 +1,25 @@
// A simple program that builds a sqrt table
-#include <math.h>
-#include <stdio.h>
+#include <cmath>
+#include <fstream>
+#include <iostream>
int main(int argc, char* argv[])
{
- int i;
- double result;
-
// make sure we have enough arguments
if (argc < 2) {
return 1;
}
- // open the output file
- FILE* fout = fopen(argv[1], "w");
- if (!fout) {
- return 1;
+ std::ofstream fout(argv[1], std::ios_base::out);
+ const bool fileOpen = fout.is_open();
+ if (fileOpen) {
+ fout << "double sqrtTable[] = {" << std::endl;
+ for (int i = 0; i < 10; ++i) {
+ fout << sqrt(static_cast<double>(i)) << "," << std::endl;
+ }
+ // close the table with a zero
+ fout << "0};" << std::endl;
+ fout.close();
}
-
- // create a source file with a table of square roots
- fprintf(fout, "double sqrtTable[] = {\n");
- for (i = 0; i < 10; ++i) {
- result = sqrt(static_cast<double>(i));
- fprintf(fout, "%g,\n", result);
- }
-
- // close the table with a zero
- fprintf(fout, "0};\n");
- fclose(fout);
- return 0;
+ return fileOpen ? 0 : 1; // return 0 if wrote the file
}
diff --git a/Tests/Tutorial/Step6/MathFunctions/mysqrt.cxx b/Tests/Tutorial/Step6/MathFunctions/mysqrt.cxx
index 458ed63..b9ad20a 100644
--- a/Tests/Tutorial/Step6/MathFunctions/mysqrt.cxx
+++ b/Tests/Tutorial/Step6/MathFunctions/mysqrt.cxx
@@ -1,11 +1,8 @@
#include "MathFunctions.h"
#include "TutorialConfig.h"
-#include <stdio.h>
+#include <iostream>
-// include the generated table
-#include "Table.h"
-
-#include <math.h>
+#include <cmath>
// a hack square root calculation using simple operations
double mysqrt(double x)
@@ -14,27 +11,23 @@ double mysqrt(double x)
return 0;
}
- double result;
-
// if we have both log and exp then use them
- double delta;
-
- // use the table to help find an initial value
- result = x;
- if (x >= 1 && x < 10) {
- result = sqrtTable[static_cast<int>(x)];
- }
+#if defined(HAVE_LOG) && defined(HAVE_EXP)
+ double result = exp(log(x) * 0.5);
+ std::cout << "Computing sqrt of " << x << " to be " << result << " using log"
+ << std::endl;
+#else
+ double result = x;
// do ten iterations
- int i;
- for (i = 0; i < 10; ++i) {
+ for (int i = 0; i < 10; ++i) {
if (result <= 0) {
result = 0.1;
}
- delta = x - (result * result);
+ double delta = x - (result * result);
result = result + 0.5 * delta / result;
- fprintf(stdout, "Computing sqrt of %g to be %g\n", x, result);
+ std::cout << "Computing sqrt of " << x << " to be " << result << std::endl;
}
-
+#endif
return result;
}