summaryrefslogtreecommitdiff
path: root/mysqrt.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'mysqrt.cpp')
-rw-r--r--mysqrt.cpp21
1 files changed, 21 insertions, 0 deletions
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;
+}