diff options
Diffstat (limited to 'Tests/Tutorial/Step5/MathFunctions')
-rw-r--r-- | Tests/Tutorial/Step5/MathFunctions/CMakeLists.txt | 24 | ||||
-rw-r--r-- | Tests/Tutorial/Step5/MathFunctions/MakeTable.cxx | 35 | ||||
-rw-r--r-- | Tests/Tutorial/Step5/MathFunctions/MathFunctions.h | 1 | ||||
-rw-r--r-- | Tests/Tutorial/Step5/MathFunctions/mysqrt.cxx | 44 |
4 files changed, 104 insertions, 0 deletions
diff --git a/Tests/Tutorial/Step5/MathFunctions/CMakeLists.txt b/Tests/Tutorial/Step5/MathFunctions/CMakeLists.txt new file mode 100644 index 0000000..7dc4d82 --- /dev/null +++ b/Tests/Tutorial/Step5/MathFunctions/CMakeLists.txt @@ -0,0 +1,24 @@ +# first we add the exetuable 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 the binary tree directory to the search path for include files +include_directories( ${CMAKE_CURRENT_BINARY_DIR} ) + +# add the main library +add_library(MathFunctions mysqrt.cxx) + +install_targets (/bin MathFunctions) +install_files (/include FILES MathFunctions.h) diff --git a/Tests/Tutorial/Step5/MathFunctions/MakeTable.cxx b/Tests/Tutorial/Step5/MathFunctions/MakeTable.cxx new file mode 100644 index 0000000..23fa7d4 --- /dev/null +++ b/Tests/Tutorial/Step5/MathFunctions/MakeTable.cxx @@ -0,0 +1,35 @@ +// A simple program that builds a sqrt table +#include <stdio.h> +#include <math.h> + +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; + } + + // crate a source file with a tabel fo 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; +} diff --git a/Tests/Tutorial/Step5/MathFunctions/MathFunctions.h b/Tests/Tutorial/Step5/MathFunctions/MathFunctions.h new file mode 100644 index 0000000..cd36bcc --- /dev/null +++ b/Tests/Tutorial/Step5/MathFunctions/MathFunctions.h @@ -0,0 +1 @@ +double mysqrt(double x); diff --git a/Tests/Tutorial/Step5/MathFunctions/mysqrt.cxx b/Tests/Tutorial/Step5/MathFunctions/mysqrt.cxx new file mode 100644 index 0000000..33659b7 --- /dev/null +++ b/Tests/Tutorial/Step5/MathFunctions/mysqrt.cxx @@ -0,0 +1,44 @@ +#include <stdio.h> +#include "MathFunctions.h" +#include "TutorialConfig.h" + +// include the generated table +#include "Table.h" + +#include <math.h> + +// a hack square root calculation using simple operations +double mysqrt(double x) +{ + if (x <= 0) + { + 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)]; + } + + // do ten iterations + int i; + for (i = 0; i < 10; ++i) + { + if (result <= 0) + { + result = 0.1; + } + delta = x - (result*result); + result = result + 0.5*delta/result; + fprintf(stdout,"Computing sqrt of %g to be %g\n",x,result); + } + + return result; +} |