what is wrong with my code?

Hi Im a new member and i need ur help. I have to write a program to solve the following equation

http://i52.tinypic.com/mjays.jpg

B= √(e^(x^1 ) )/3!+√(e^(x^2 ) )/5!+√(e^(x^4 ) )/7!-√(e^(x^6 ) )/3!-√(e^(x^9 ) )/5!-√(e^(x^12 ) )/7!+√(e^(x^16 ) )/3!+⋯etc

I wrote the following code for this purpose

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
//value of b for any x

#include <iostream>
#include <cmath>
using namespace std;
int main ()
{ 
long int  n ,x,f,k=3,a,b,j ;
float B=0;
cout<<"Enter The Number of Terms = "<<endl;
cin>>n;
cout<<"Enter The Value of x = "<<endl;
cin>>x;

for (int i = 1 ; i<=n ; i++)
{    
     f=1;
     if (k>7) // for the factorial 
     k=3;
     for (j=1;j<=k;j++)
     f=f*j;
    
     a=i*(i+1);    // for the power of x 2,6,12,20
     b=i*i;        // for the power of x 1,4,9,16 
     
   if ((i-1)%6<3){ // for the sign of the first three terms +, +, +
   
   if (i%2==0){
   B=B+sqrt(exp(pow(x,a)))/f;cout<<"one "<<B<<endl;}
   
   else {B=B+sqrt(exp(pow(x,b)))/f;cout<<"two "<<B<<endl;}}
   
   
   else if ((i-1)%6>=3){  // for the sign of the last three terms -, -, -
   if (i%2==0){
   B=B-sqrt(exp(pow(x,i*(i+1))))/f;cout<<"three "<<B<<endl;}
   else{
   B=B-sqrt(exp(pow(x,i*i)))/f;cout<<"four "<<B<<endl;}}
   k=k+2;
 }  
   
cout<<"The value of B= "<<B<<endl;

return 0 ;
}


but when i run the code it gives me the following results which are wrong


Enter The Number of Terms =
6
Enter The Value of x =
2
two 0.453047
one 6.58025e+011
two 1.#INF
three -1.#IND
four -1.#IND
three -1.#IND
The value of B= -1.#IND
Press any key to continue


So i dont know what is wrong with my code i wish u could help me.

Thanx in advance.
Heyas!

Say... that looks like a Taylor series of sorts. Anyways, before we go on, when I tried to compile this for myself to see what exactly was going on, I encountered 4 errors at lines 27, 29, 34, and 36, and they were roughly the same error:
call of overloaded pow(long int&, long int&)'' is ambiguous
or
call of overloaded pow(long int&, int)'' is ambiguous
. You should consider using doubles in place of long ints.

Also, I suspected that I had jumped to conclusions earlier, I'll reiterate the first recommendation I made. After computing the same problem in Mathematica, I'm pretty sure that you've exceeded the bounds of whatever you're using for holding numbers, be it a long int or double...
1
2
3
4
x := 2; 
Sqrt[(E^(x^1))]/3! + Sqrt[(E^(x^2))]/5! + Sqrt[(E^(x^4))]/7! - 
  Sqrt[(E^(x^6))]/3! - Sqrt[(E^(x^9))]/5! - 
  Power[(E^(x^12)), (2)^-1]/7! // N
-5.403415962412141*10^885

...you're going to need an arbitrary-precision arithmetic library, such as CLN: http://www.ginac.de/CLN/cln.html

Good luck!

-Albatross
Last edited on
yup u r absolutely right the problem is with the precision however i found that there were also error in the source code

Thanx for ur share.
Topic archived. No new replies allowed.