summaryrefslogtreecommitdiff
path: root/mysqrt.cpp
diff options
context:
space:
mode:
authorMichael Abed <michaelabed@gmail.com>2012-03-02 14:19:26 -0500
committerMichael Abed <michaelabed@gmail.com>2012-03-02 14:19:26 -0500
commit973562e3bbffbe2502649ce30d55e7cc6cbc6c3c (patch)
treed6fe33a799e9ca0034e9e98b6b155b0706937f83 /mysqrt.cpp
parentc2391ab3040f0ce641eb77b2fe5a693b9f6e2444 (diff)
downloadec327-lab2-973562e3bbffbe2502649ce30d55e7cc6cbc6c3c.tar.gz
ec327-lab2-973562e3bbffbe2502649ce30d55e7cc6cbc6c3c.tar.bz2
ec327-lab2-973562e3bbffbe2502649ce30d55e7cc6cbc6c3c.zip
i did things
Diffstat (limited to 'mysqrt.cpp')
-rw-r--r--mysqrt.cpp14
1 files changed, 8 insertions, 6 deletions
diff --git a/mysqrt.cpp b/mysqrt.cpp
index 088fdde..18bb632 100644
--- a/mysqrt.cpp
+++ b/mysqrt.cpp
@@ -5,18 +5,20 @@ using namespace std;
double mysqrt(double x)
{
bool goodenough = false;
- double guess;
- double last = 0.0;
+ double lastGuess, nextGuess;
+
+ lastGuess = x / 10.0;
if (x <= 0)
return x;
while (!goodenough) {
- guess = (last + (x / last)) / 2.0;
- last = guess;
+
+ nextGuess = (lastGuess + (x / lastGuess)) / 2.0;
+ lastGuess = nextGuess;
- goodenough = abs(guess - last) <= 0.000000001;
+ goodenough = abs(nextGuess - lastGuess) <= 0.000000000001;
}
- return guess;
+ return nextGuess;
}