Skip to content
Permalink
Browse files
Add files via upload
  • Loading branch information
clarkh9 committed Dec 22, 2022
1 parent 6efa1db commit 8963b0f732a4dbca8f05e23985c45c54c79a656c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
@@ -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
@@ -0,0 +1,24 @@
#pragma once

#ifndef SQRT
#define SQRT
#include <iostream>
#include <stdint.h>
#include <stdlib.h>

#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

0 comments on commit 8963b0f

Please sign in to comment.