diff --git a/src/sqrt.cpp b/src/sqrt.cpp new file mode 100644 index 0000000..6a99efa --- /dev/null +++ b/src/sqrt.cpp @@ -0,0 +1,32 @@ +/* Copyright (C) 2022 Harry Clark - Student No. 12340530 */ +/* Date: 22/12/22 */ + +#include "sqrt.h" + +#ifndef NEWTON_SQRT +#define NEWTON_SQRT + +int main(int* argc, char* argv) +{ + NEWTON::TOLERANCE = 1e-12; + NEWTON::ERROR = NEWTON::TOLERANCE + 1; + NEWTON::ITERATION = 0; + NEWTON::MAX_ITERATION = 100; + NEWTON::LATEST_RESULT; + + while (NEWTON::ERROR > NEWTON::TOLERANCE && NEWTON::ITERATION < NEWTON::MAX_ITERATION) + { + NEWTON::LATEST_RESULT = NEWTON::RESULT + (NEWTON::RESULT * NEWTON::RESULT - 1) / (2 * NEWTON::RESULT); + NEWTON::ERROR = fabs(NEWTON::LATEST_RESULT - NEWTON::RESULT); + NEWTON::ITERATION++; + } + + if(NEWTON::ERROR <= NEWTON::TOLERANCE) + { + std::cout << "The sqr is " << NEWTON::RESULT; + } + + return 0; +} + +#endif diff --git a/src/sqrt.h b/src/sqrt.h new file mode 100644 index 0000000..8119028 --- /dev/null +++ b/src/sqrt.h @@ -0,0 +1,24 @@ +#pragma once + +#ifndef SQRT +#define SQRT +#include +#include +#include + +#define NEWTON_EQ +#define NEWTON_DERIVATIVE + +typedef struct +{ + static double TOLERANCE; + static double ERROR; + static double RESULT; + static double LATEST_RESULT; + static double RESULT; + static int MAX_ITERATION; + static int ITERATION; + +} NEWTON; + +#endif