Math problem... Large products cause answers to become random.

Oct 30, 2011 at 6:56pm
Hi, i have written his program for my c++ class, and everything works perfectly.

the program basically asks for two numbers and raises them to each other and themselves, without using the pow() statement (so it just added a little more work for me)

(ex. if you enter 5 and 2 it will find 5^2, 5^5, 2^5, and 2^2)

it's a useless program but it is supposed to introduce us into strings, you will see what i mean in my program below.

anyway my problem is this, it works perfectly with small numbers like 5 and 2 but if i enter 10 and 9, when it raises 10^10 it equals 1410065408 and when it raises 9^10 it equals -808182895.

it can't be my math because 10^9 work fine and 9^9 work and small numbers all work perfectly.

I don't know if it is a c++ issue or a problem with how i wrote my program.

Please Help.


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
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
	int num1, num2, num3, num4, ans1, ans2, ans3, ans4;
	cout<<"Please enter a number.\n";
	cin>>num1;
	cout<<"Please enter a second number.\n";
	cin>>num2;
	while(num1 != num2)
	{

	const double const1=num1;
	const double const2=num2;
	num3=num1;
	num4=num2;
	string str1, str2, str3, str4, str5, str6, str7, str8, str9, str10;
	std::stringstream ss1, ss2, ss3, ss4, ss5, ss6;
	ss1<<num1;
	ss1>>str1;
	ss2<<num2;
	ss2>>str2;
	str3='^';
	str5=" = ";
	str4=str1;
	str10=str2;
	num2--;
	while (num2 != 0)
	{
		num1=num1*int(const1);
		num2--;
		str4=str4+str3+str1;
	}
	ans1=num1;
	ss3<<ans1;
	ss3>>str6;
	num1=int(const1);
	num2=int(const2);
	cout<<"\n"<<num1<<'^'<<num2<<" = "<<str4+str5+str6<<"\n";
	str4=str1;
	num3--;
	while (num3 != 0)
	{
		num1=num1*int(const1);
		num3--;
		str4=str4+str3+str1;
	}
	ans2=num1;
	ss4<<ans2;
	ss4>>str7;
	num1=int(const1);
	num2=int(const2);
	num3=int(const1);
	num4=int(const2);
	cout<<num1<<'^'<<num1<<" = "<<str4+str5+str7<<"\n";
	str4=str1;
	num1--;
	while (num1 != 0)
	{
		num2=num2*int(const2);
		num1--;
		str10=str10+str3+str2;
	}
	ans3=num2;
	ss5<<ans3;
	ss5>>str8;
	num1=int(const1);
	num2=int(const2);
	cout<<num2<<'^'<<num1<<" = "<<str10+str5+str8<<"\n";
	str10=str2;
        num4--;
	while (num4 != 0)
	{
		num2=num2*int(const2);
		num4--;
		str10=str10+str3+str2;
	}
	ans4=num2;
	ss6<<ans4;
	ss6>>str9;
	num2=int(const2);
	cout<<num2<<'^'<<num2<<" = "<<str10+str5+str9<<"\n";
	cout<<"\nPlease enter a number.\n";
	cin>>num1;
	cout<<"\nPlease enter another number.\n";
	cin>>num2;
	}

	return 0;
}

Last edited on Oct 30, 2011 at 6:59pm
Oct 30, 2011 at 7:20pm
a signed integer only goes up to 2,147,483,647. then it wraps around so you will get funny numbers coming out.

use doubles if you want cater for bigger numbers.
Oct 30, 2011 at 7:34pm
thanks, that worked great, but now i have one more question now i get the answers in scientific notation.

how do i make it display the entire number instead of it going into scientific notation
Last edited on Oct 30, 2011 at 7:40pm
Oct 30, 2011 at 8:15pm
If you want every single digit of a big power then you will have to use a bignum library
The built in types all have fixed precisions

I haven't used bignums so I'll pass to other experts, some googling found this

http://gmplib.org/

Oct 30, 2011 at 8:29pm
Is the point of this exercise to make you implement arbitrary precision arithmetic, or just to make you implement simple operations like exponentiation using built-in integers? If the former, you shouldn't use GMP, since it misses the point of the problem. If the latter, you don't need to use GMP, since the program isn't expected to be able to compute 10^100.
Oct 30, 2011 at 8:41pm
@helios

I think the point of the exercise is to introduce us into strings not the actual arithmetic, we learned basic arithmetic like this already, and thanks to mik2718's suggestion about using double instead of int, my program is technically perfect and will get me an A

but for my own pleasure i wanted to know how to express the entire number instead of it automatically switching to scientific notation
Oct 30, 2011 at 8:45pm
Oct 30, 2011 at 8:57pm
I thought setprecision decides how many digits are shown after the decimal point, and not where the decimal point lies.

Please correct me if i am wrong.

P.s. Sorry if these are dumb questions i am still new to c++
Oct 30, 2011 at 9:11pm
setprecision() is used to set how many significant digits you want shown. For example, 1E+9+1 would show up as 1E+9 with setprecision(1), and as 1000000001 with setprecision(10).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <iomanip>

int main(){
	double a=1E+9+1;
	std::cout <<a<<std::endl;
	std::cout <<std::setprecision(9);
	std::cout <<a<<std::endl;
	std::cout <<std::setprecision(10);
	std::cout <<a<<std::endl;
	a=3.1415926535897932384626433832795*1000000;
	std::cout <<std::setprecision(10);
	std::cout <<a<<std::endl;
}

1e+009
1e+009
1000000001
3141592.654
Oct 30, 2011 at 9:18pm
Interesting .... I didnt know setprecision could be used like that

Thank you very much
Topic archived. No new replies allowed.