init
This commit is contained in:
2
Step2/MathFunctions/CMakeLists.txt
Normal file
2
Step2/MathFunctions/CMakeLists.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
# TODO 1: Add a library called MathFunctions
|
||||
# Hint: You will need the add_library command
|
1
Step2/MathFunctions/MathFunctions.h
Normal file
1
Step2/MathFunctions/MathFunctions.h
Normal file
@@ -0,0 +1 @@
|
||||
double mysqrt(double x);
|
22
Step2/MathFunctions/mysqrt.cxx
Normal file
22
Step2/MathFunctions/mysqrt.cxx
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <iostream>
|
||||
|
||||
// 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