PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > How to compute 1/(2*i)?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
How to compute 1/(2*i)?

Réponse
 
LinkBack Outils de la discussion
Vieux 06/04/2008, 10h34   #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
Vieux 06/04/2008, 10h53   #2
Alan Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to compute 1/(2*i)?

Francogrex wrote:
> 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.


1 and 2 are int literals, and i is defined as an int, so the expression
1/(2*i) performs integer division. The easiest way to force floating
point division is to make one of your literals a floating point number.
That is: 1/(2.*i)

--
Alan Johnson
  Réponse avec citation
Vieux 06/04/2008, 10h54   #3
Francogrex
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to compute 1/(2*i)?

On Apr 6, 11:34am, Francogrex <fra...@grex.org> wrote:
> 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.


ok i figured it out: if it's written 1.0/(2.0*i) it works. strange
that you have to specify the points after the values, so by default c+
+ considers them as integers
  Réponse avec citation
Vieux 06/04/2008, 12h22   #4
ciccio
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to compute 1/(2*i)?

Francogrex wrote:
> On Apr 6, 11:34 am, Francogrex <fra...@grex.org> wrote:
>> 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.

>
> ok i figured it out: if it's written 1.0/(2.0*i) it works. strange
> that you have to specify the points after the values, so by default c+
> + considers them as integers


Yep,

And if you really want to get rid of the brackets, you type

0.5/i

The line you have
u[i]=u[i-1]*((1-(1/(2*i)))/(1+(x*x)));
Should be
u[i] = u[i-1]*(1.0-0.5/i)/(1.0+x*x);

If you work with floats or doubles, always define your constants as
floats or doubles, even if they can be represented as an integer.

The problem with yours was that 1/(2*i) = 0 (integer devisions)

Regards
  Réponse avec citation
Vieux 06/04/2008, 13h10   #5
brian tyler
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to compute 1/(2*i)?

> ok i figured it out: if it's written 1.0/(2.0*i) it works. strange
> that you have to specify the points after the values, so by default c+
> + considers them as integers


It would be stranger if it didn't. These pages might you
understand exactly what is going on
http://c.comsci.us/etymology/literals.html
http://en.wikipedia.org/wiki/Divisio...on_of_integers

Brian

  Réponse avec citation
Vieux 06/04/2008, 19h32   #6
Francogrex
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to compute 1/(2*i)?

On Apr 6, 11:53am, Alan Johnson <aw...@yahoo.com> wrote:
> > 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.

>
> 1 and 2 are int literals, and i is defined as an int, so the expression
> 1/(2*i) performs integer division. The easiest way to force floating
> point division is to make one of your literals a floating point number.
> That is: 1/(2.*i)


Hi thanks all. I just figured it out right after I posted. Sometimes
it's worthwhile that I try and be patient before I post.
  Réponse avec citation
Vieux 07/04/2008, 02h36   #7
stan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to compute 1/(2*i)?

Francogrex wrote:
> On Apr 6, 11:34am, Francogrex <fra...@grex.org> wrote:
>> 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.

>
> ok i figured it out: if it's written 1.0/(2.0*i) it works. strange
> that you have to specify the points after the values, so by default c+
> + considers them as integers


You do know that you can/should also move u[0]=1; outside the loop?
Further, you can precalculate the denminator (1+(x*x)) since x isn't
modified in the loop. It's probably more typical to see this type of
thing written as a for loop vice a while.

for (int i=0; i<9; ++i) {
u[i] = ....
}
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 18h39.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18868 seconds with 15 queries