Afficher un message
Vieux 06/04/2008, 11h34   #1
Francogrex
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How to compute 1/(2*i)?

Hi, I have a problem with this very simple program:

#include <iostream>
#include <cmath>
using namespace std;

int main(){
double n=16;
double t=2.78;
double x;
x=(t/sqrt(n));
int i=0;
double u[9];
while(i<8){
u[0]=1;
i=i+1;
u[i]=u[i-1]*((1-(1/(2*i)))/(1+(x*x)));
cout <<"Iteration " <<i<<" is: "<<u[i]<<"\n";
}
system("PAUSE");
return 0;
}

OUTPUT IS:
Iteration 1 is: 0.674297
Iteration 2 is: 0.454677
Iteration 3 is: 0.306588
Iteration 4 is: 0.206731
Iteration 5 is: 0.139398
Iteration 6 is: 0.093996
Iteration 7 is: 0.0633812
Iteration 8 is: 0.0427378
Press any key to continue . .

But the output should read:
0.337148733
0.170503902
0.095808624
0.056528074
0.034305063
0.021204166
0.013276636
0.008392877

the problem is that C++ is not doing this calculation 1/(2*i)
correctly. Is there a way to force him to do this division other than
using the brackets? Thanks.
  Réponse avec citation
 
Page generated in 0,04539 seconds with 9 queries