From 8932ac3c9881f800310e03fb147dce3261989eca Mon Sep 17 00:00:00 2001 From: Michael Abed Date: Mon, 27 Feb 2012 11:12:18 -0500 Subject: initial commit --- mysqrt.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 mysqrt.cpp (limited to 'mysqrt.cpp') diff --git a/mysqrt.cpp b/mysqrt.cpp new file mode 100644 index 0000000..29fedf2 --- /dev/null +++ b/mysqrt.cpp @@ -0,0 +1,21 @@ + +#include "mysqrt.h" + +using namespace std; +double mysqrt(double x) +{ + bool goodenough = false; + double guess; + double last = x / 5.0; + + if (x <= 0) + return x; + + while (!goodenough) { + guess = (last + (x / last)) / 2.0; + last = guess; + + goodenough = abs(guess - last) <= 0.000000001; + } + return guess; +} -- cgit v1.2.3