Precision problem

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
#include <iostream>
using namespace std;
int main()
{
	cout.precision(50);
cout<<"Anmol's Square Root Calculator!"<<endl;
cout<<""<<endl;
cout << "This program will compute the square root\n";
cout << "of a number using the Babylonian algorithm!\n";
cout<<""<<endl;
cout<<"Only positive numbers work with this algorithm!"<<endl;
cout<<""<<endl;
cout<<"All cycle #1 approximate square roots are guessed\n"<<"using 1 as the first approximate."<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout<<""<<endl;
char playAgain='y';
while (playAgain !='n') 
{
int count(25), cycle(1);
double guess, sqrt, num; 
cout << "Please enter the number you would like to compute the square root of: ";
cin >> num;
cout<<""<<endl;
do{
if (num <= 0)
{
	cout<<""<<endl;
cout<<"Invalid input. Please re-enter a valid number: ";
cin >> num;
cout<<""<<endl;
}
}
while (num <= 0);
cout<<""<<endl;
cout<<""<<endl;
cout<<""<<endl;

for (guess=1; count!=0; count--)
{ 
guess =(guess + (num/guess))/2;
cout<<""<<endl;
cout<<"Cycle "<<cycle<<" Aproximate: "<<guess<<endl;
sqrt = guess;

cycle++;
}
cout<<""<<endl;

cout << "The square root of "<< num << " is " << sqrt<<"!"<<endl;;
cout <<""<< endl;

do { cout <<""<<endl;
		cout << "Would you like to calculate again? (y/n): ";
		cin >> playAgain;
		cout <<" "<<endl;
		do { cout <<""<<endl;
		if ((playAgain !='y' && playAgain !='n'))
		{
		cout << "Invalid input. Only y/n: ";
		cin >> playAgain;
		cout <<" "<<endl;
		
		} }while (playAgain !='y' && playAgain !='n');
		
		} while (playAgain !='y' && playAgain !='n');
		 
	}
cout<<"Thank you for using a program made by Anmol Sethi!"<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout<<""<<endl;
cin.get();
return 0;
}


Well I am 12 and my name is Anmol. For my math class we are learning about square roots. According to my teacher its only possible to find the square root using a calculator or a table which is pretty obviously false as the calcuator needs a method to compute the sqrt. After extensive research I made this code using the Babylonian algorithm. But im facing a problem
In this code I specifically set the precision of the decimals to 15. However it outputs numbers at 12 decimals. But in this code it outputs at 15 decimals.


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
#include <iostream>
using namespace std;
int main()
{
	cout.precision(18);
cout<<"Anmol's Square Root Calculator!"<<endl;
cout<<""<<endl;
cout << "This program will compute the square root\n";
cout << "of a number using the Babylonian algorithm!\n";
cout<<""<<endl;
cout<<"Only positive numbers work with this algorithm!"<<endl;
cout<<""<<endl;
cout<<"All cycle #1 approximate square are guessed\n"<<"using 1 as the approximate."<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout<<""<<endl;
char playAgain='y';
while (playAgain !='n') 
{
	
	
	
int count(25), cycle(1);
double guess(1), sqrt, x, num; 
cout << "Please enter the number you would like to know the square root of: ";
cin >> num;
cout<<""<<endl;
do{
if (num <= 0)
{
	cout<<""<<endl;
cout<<"Invalid input. Please re-enter a valid number: ";
cin >> num;
cout<<""<<endl;
}
}
while (num <= 0);
cout<<""<<endl;
cout<<""<<endl;
cout<<""<<endl;

while (count > 0)
{
x = 
guess =(guess + (num/guess))/2;
cout<<""<<endl;
cout<<"Cycle "<<cycle<<" Aproximate: "<<guess<<endl;
sqrt = guess;

count-=1;
cycle+=1;
}
cout<<""<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout << "The sqaure root of "<< num << " is " << sqrt<<"!"<<endl;;
cout <<""<< endl;

do { cout <<""<<endl;
		cout << "Would you like to calculate again? (y/n): ";
		cin >> playAgain;
		cout <<" "<<endl;
		do { cout <<""<<endl;
		if ((playAgain !='y' && playAgain !='n'))
		{
		cout << "Invalid input. Only y/n: ";
		cin >> playAgain;
		cout <<" "<<endl;
		
		} }while (playAgain !='y' && playAgain !='n');
		
		} while (playAgain !='y' && playAgain !='n');
		 
	}
cout<<"Thank you for using a program made by Anmol Sethi!"<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout<<""<<endl;
cin.get();
return 0;
}


Im very confused on why this is happening. I tried cout << fixed << showpoint; but it looks unpleasant when the num outputs 15 zeros for no reason. Can anyone enlighten me on why this is happening and how to fix it? In a book I read its more efficient to use a for loops for algorithms so I wanna use the for instead of the while loop. Also I have a second question. Why cant I go over the 15 decimals? If I set the precision to 30/40 it does nothing to my output even though in my computer calculator it goes to 30/40. Is there another integer type I should use instead of double?

Sorry if any of it looks like a big wall of text xD.

Im in a rush to type this cause its 12:05am for me atm.

Thanks in advance :D

p.s Im gonna be posting alot on this forum now. I used to use it alot about a year ago but I quit c++ programming for a while because I couldnt understand it further when I was smaller xD. So im reading a book now for it and I hopefully get all the way through the console applications and on to windows. I am using visual studio ultimate 2012 if it matters. xD
Last edited on
Topic archived. No new replies allowed.