diff options
Diffstat (limited to 'Help/guide/tutorial/Step6/MathFunctions')
4 files changed, 90 insertions, 0 deletions
| diff --git a/Help/guide/tutorial/Step6/MathFunctions/CMakeLists.txt b/Help/guide/tutorial/Step6/MathFunctions/CMakeLists.txt new file mode 100644 index 0000000..f64c6ac --- /dev/null +++ b/Help/guide/tutorial/Step6/MathFunctions/CMakeLists.txt @@ -0,0 +1,32 @@ +add_library(MathFunctions mysqrt.cxx) + +# state that anybody linking to us needs to include the current source dir +# to find MathFunctions.h, while we don't. +target_include_directories(MathFunctions +          INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} +          ) + +# does this system provide the log and exp functions? +include(CheckSymbolExists) +check_symbol_exists(log "math.h" HAVE_LOG) +check_symbol_exists(exp "math.h" HAVE_EXP) +if(NOT (HAVE_LOG AND HAVE_EXP)) +  unset(HAVE_LOG CACHE) +  unset(HAVE_EXP CACHE) +  set(CMAKE_REQUIRED_LIBRARIES "m") +  check_symbol_exists(log "math.h" HAVE_LOG) +  check_symbol_exists(exp "math.h" HAVE_EXP) +  if(HAVE_LOG AND HAVE_EXP) +    target_link_libraries(MathFunctions PRIVATE m) +  endif() +endif() + +# add compile definitions +if(HAVE_LOG AND HAVE_EXP) +  target_compile_definitions(MathFunctions +                             PRIVATE "HAVE_LOG" "HAVE_EXP") +endif() + +# install rules +install(TARGETS MathFunctions DESTINATION lib) +install(FILES MathFunctions.h DESTINATION include) diff --git a/Help/guide/tutorial/Step6/MathFunctions/MakeTable.cxx b/Help/guide/tutorial/Step6/MathFunctions/MakeTable.cxx new file mode 100644 index 0000000..ee58556 --- /dev/null +++ b/Help/guide/tutorial/Step6/MathFunctions/MakeTable.cxx @@ -0,0 +1,25 @@ +// A simple program that builds a sqrt table +#include <cmath> +#include <fstream> +#include <iostream> + +int main(int argc, char* argv[]) +{ +  // make sure we have enough arguments +  if (argc < 2) { +    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(); +  } +  return fileOpen ? 0 : 1; // return 0 if wrote the file +} diff --git a/Help/guide/tutorial/Step6/MathFunctions/MathFunctions.h b/Help/guide/tutorial/Step6/MathFunctions/MathFunctions.h new file mode 100644 index 0000000..cd36bcc --- /dev/null +++ b/Help/guide/tutorial/Step6/MathFunctions/MathFunctions.h @@ -0,0 +1 @@ +double mysqrt(double x); diff --git a/Help/guide/tutorial/Step6/MathFunctions/mysqrt.cxx b/Help/guide/tutorial/Step6/MathFunctions/mysqrt.cxx new file mode 100644 index 0000000..0637063 --- /dev/null +++ b/Help/guide/tutorial/Step6/MathFunctions/mysqrt.cxx @@ -0,0 +1,32 @@ +#include <cmath> +#include <iostream> + +#include "MathFunctions.h" + +// a hack square root calculation using simple operations +double mysqrt(double x) +{ +  if (x <= 0) { +    return 0; +  } + +  // if we have both log and exp then use them +#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 and exp" << std::endl; +#else +  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; +  } +#endif +  return result; +} | 
