-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnewtonraphson.cpp
103 lines (88 loc) · 1.54 KB
/
newtonraphson.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
// enabling C99 gives us modern functionality like efficient float to int cast
#define _ISOC9X_SOURCE 1
#define _ISOC99_SOURCE 1
bool isEven(int n);
int int_sq(int n);
int int_2x(int n);
bool isInteger(double n);
extern "C"
{
int initializer();
double nr_sqrt(double n, double x0, int iterations, bool printIts);
int passarrays(double in[4], double out[4]);
};
int initializer()
{
// open a file for logging
FILE *fp;
fp = fopen("newtonraphson.log", "w");
fprintf(fp, "Testing...\n");
return 1;
}
int passarrays(double in[4], double out[4])
{
memcpy(out, in, 4 * sizeof(double));
return 0;
}
bool isEven(int n)
{
return n % 2 == 0;
}
int int_sq(int n)
{
int x, sq;
if (n == 0)
{
return 0;
}
else
{
x = n >> 1;
if (isEven(n))
{
return int_sq(x) << 2;
}
else
{
return (int_sq(x) << 2) + (x << 2) + 1;
}
}
}
int int_2x(int n)
{
return n << 1;
}
bool isInteger(double n)
{
return fmod(n, 1.0) == 0;
}
double nr_sqrt(double n, double x0, int iterations, bool printIts)
{
double xm1, root;
for (int i = 0; i < iterations; ++i)
{
if (i == 0)
{
xm1 = x0;
}
// if integer, use integer algorithm
if (isInteger(xm1))
{
root = xm1 - (int_sq(lrint(xm1)) - n) / (int_2x(lrint(xm1)));
}
else
{
root = xm1 - (pow(xm1, 2) - n) / (2 * xm1);
}
if (printIts)
{
printf("at iteration %i - %f\n", i, xm1);
}
xm1 = root;
}
return root;
}