-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.cpp
More file actions
50 lines (42 loc) · 1.45 KB
/
functions.cpp
File metadata and controls
50 lines (42 loc) · 1.45 KB
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
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
const float tol = .00001;
float sqrtRecur(float num1, float guess)
{
//cout << "num: " << num1 << "guess: " << guess << endl;
float guess2 = guess - ((guess*guess-num1) / (2*guess));
if(fabs(guess2-guess)<tol) {return guess2;}
else
{
cout << "recursive estimate: " << fixed << setprecision(5) << guess2 << endl;
//cout << "kyle" << endl;
return sqrtRecur(num1,guess2);
}
}
int main()
{
float num;
float guess;
float difference = 9999999;
int iterations = 1;
cout << "Please enter a number to evaluate the square root of: "; cin >> num;
cout << "Please enter a guess of the square root: "; cin >> guess;
float holdGuess = guess;
while(difference > tol)
{
float guess2 = guess - ((guess*guess-num) / (2*guess));
cout << "iter: " << iterations << ", est: " << fixed << setprecision(5) << guess << endl;
difference = fabs(guess2-guess);
guess = guess2;
iterations++;
}
cout << "[iterative] sqrt(" << num << ") = " << guess << endl << endl;
cout << "recursive estimate: " << num << endl;
float est = sqrtRecur(num,holdGuess);
cout << "[recursive] sqrt(" << num << ") = " << fixed << setprecision(5) << est << endl << endl;
float builtIn = sqrt(num);
cout << "[built-in] sqrt(" << num << ") = " << fixed << setprecision(5) << builtIn << endl;
cout << "[verifying] " << builtIn << "^2 = " << builtIn*builtIn << endl << endl;
}