init
This commit is contained in:
5
Step3/MathFunctions/CMakeLists.txt
Normal file
5
Step3/MathFunctions/CMakeLists.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
add_library(MathFunctions mysqrt.cxx)
|
||||
|
||||
# TODO 1: State that anybody linking to MathFunctions needs to include the
|
||||
# current source directory, while MathFunctions itself doesn't.
|
||||
# Hint: Use target_include_directories with the INTERFACE keyword
|
1
Step3/MathFunctions/MathFunctions.h
Normal file
1
Step3/MathFunctions/MathFunctions.h
Normal file
@@ -0,0 +1 @@
|
||||
double mysqrt(double x);
|
24
Step3/MathFunctions/mysqrt.cxx
Normal file
24
Step3/MathFunctions/mysqrt.cxx
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "MathFunctions.h"
|
||||
|
||||
// a hack square root calculation using simple operations
|
||||
double mysqrt(double x)
|
||||
{
|
||||
if (x <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
return result;
|
||||
}
|
Reference in New Issue
Block a user