Commit 2eec7a47 authored by Markus Ferrell's avatar Markus Ferrell
Browse files

Tutorial: Refactor MathFunctions code

parent f6af01b5
#include "MathFunctions.h"
#include "mysqrt.h"
namespace mathfunctions {
double sqrt(double x)
{
return detail::mysqrt(x);
}
}
\ No newline at end of file
double mysqrt(double x);
#pragma once
namespace mathfunctions {
double sqrt(double x);
}
#include "mysqrt.h"
#include <iostream>
// a hack square root calculation using simple operations
......
#pragma once
namespace mathfunctions {
namespace detail {
double mysqrt(double x);
}
}
\ No newline at end of file
add_library(MathFunctions mysqrt.cxx)
add_library(MathFunctions MathFunctions.cxx mysqrt.cxx)
# TODO 1: State that anybody linking to MathFunctions needs to include the
# current source directory, while MathFunctions itself doesn't.
......
#include "MathFunctions.h"
#include <cmath>
#ifdef USE_MYMATH
# include "mysqrt.h"
#endif
namespace mathfunctions {
double sqrt(double x)
{
// which square root function should we use?
#ifdef USE_MYMATH
return detail::mysqrt(x);
#else
return std::sqrt(x);
#endif
}
}
\ No newline at end of file
double mysqrt(double x);
#pragma once
namespace mathfunctions {
double sqrt(double x);
}
#include <iostream>
#include "mysqrt.h"
#include "MathFunctions.h"
#include <iostream>
namespace mathfunctions {
namespace detail {
// a hack square root calculation using simple operations
double mysqrt(double x)
{
......@@ -22,3 +24,5 @@ double mysqrt(double x)
}
return result;
}
}
}
#pragma once
namespace mathfunctions {
namespace detail {
double mysqrt(double x);
}
}
\ No newline at end of file
......@@ -6,9 +6,7 @@
#include "TutorialConfig.h"
// should we include the MathFunctions header?
#ifdef USE_MYMATH
# include "MathFunctions.h"
#endif
#include "MathFunctions.h"
int main(int argc, char* argv[])
{
......@@ -23,12 +21,7 @@ int main(int argc, char* argv[])
// convert input to double
const double inputValue = std::stod(argv[1]);
// which square root function should we use?
#ifdef USE_MYMATH
const double outputValue = mysqrt(inputValue);
#else
const double outputValue = sqrt(inputValue);
#endif
const double outputValue = mathfunctions::sqrt(inputValue);
std::cout << "The square root of " << inputValue << " is " << outputValue
<< std::endl;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment