Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Markus Ferrell
CMake
Commits
2eec7a47
Commit
2eec7a47
authored
Oct 13, 2022
by
Markus Ferrell
Browse files
Tutorial: Refactor MathFunctions code
parent
f6af01b5
Changes
10
Hide whitespace changes
Inline
Side-by-side
Help/guide/tutorial/Step2/MathFunctions/MathFunctions.cxx
0 → 100644
View file @
2eec7a47
#include
"MathFunctions.h"
#include
"mysqrt.h"
namespace
mathfunctions
{
double
sqrt
(
double
x
)
{
return
detail
::
mysqrt
(
x
);
}
}
\ No newline at end of file
Help/guide/tutorial/Step2/MathFunctions/MathFunctions.h
View file @
2eec7a47
double
mysqrt
(
double
x
);
#pragma once
namespace
mathfunctions
{
double
sqrt
(
double
x
);
}
Help/guide/tutorial/Step2/MathFunctions/mysqrt.cxx
View file @
2eec7a47
#include
"mysqrt.h"
#include
<iostream>
// a hack square root calculation using simple operations
...
...
Help/guide/tutorial/Step2/MathFunctions/mysqrt.h
0 → 100644
View file @
2eec7a47
#pragma once
namespace
mathfunctions
{
namespace
detail
{
double
mysqrt
(
double
x
);
}
}
\ No newline at end of file
Help/guide/tutorial/Step3/MathFunctions/CMakeLists.txt
View file @
2eec7a47
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.
...
...
Help/guide/tutorial/Step3/MathFunctions/MathFunctions.cxx
0 → 100644
View file @
2eec7a47
#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
Help/guide/tutorial/Step3/MathFunctions/MathFunctions.h
View file @
2eec7a47
double
mysqrt
(
double
x
);
#pragma once
namespace
mathfunctions
{
double
sqrt
(
double
x
);
}
Help/guide/tutorial/Step3/MathFunctions/mysqrt.cxx
View file @
2eec7a47
#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
;
}
}
}
Help/guide/tutorial/Step3/MathFunctions/mysqrt.h
0 → 100644
View file @
2eec7a47
#pragma once
namespace
mathfunctions
{
namespace
detail
{
double
mysqrt
(
double
x
);
}
}
\ No newline at end of file
Help/guide/tutorial/Step3/tutorial.cxx
View file @
2eec7a47
...
...
@@ -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
;
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment