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. 6 1475
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
On Apr 6, 11:34*am, Francogrex <fra...@grex.orgwrote:
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
Francogrex wrote:
On Apr 6, 11:34 am, Francogrex <fra...@grex.orgwrote:
>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
On Apr 6, 11:53*am, Alan Johnson <aw...@yahoo.comwrote:
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.
Francogrex wrote:
On Apr 6, 11:34*am, Francogrex <fra...@grex.orgwrote:
>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] = ....
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: pentium77 |
last post by:
Hi,
Just wondering what's the algo used to compute hashcode in Java ? Does
anybody here know ?
Thanks,
-MK.
|
by: Navin |
last post by:
Ado Sort -Relate, Compute By, or Sort operations cannot be done on
column(s) whose key length is unknown or exceeds 10 KB.
hi,
guys
i have asp application running on iis 5.0 windows 2000
i use...
|
by: wildbill |
last post by:
I'm looking for an example of how to compute an age of a record in a
query and then display that on a form. For example: I have record
that shows it was entered on 8/2/2005. I would like to be...
|
by: Michael Howes |
last post by:
I have a single DataTable in a DataSet. It has 4 columns and i'd like to
get a handful of counts of unique items in 3 of the 4 columns.
Can a DataTables Select or Compute methods to COUNT DISTINCT?...
|
by: skirkby |
last post by:
This will be obvious to some - but not me I'm afraid...
I am using an SQL data link from my ASP application to a SPROC - this all
works fine on standard SELECT statements and JOIN in to a...
|
by: Spoon |
last post by:
Hello,
Consider:
#define BUFFER_SIZE 1234 /* or some other value */
uint8_t buffer;
int do_stuff(uint8_t *buf);
where do_stuff() does something with each octet in the buffer.
|
by: MLH |
last post by:
If 3 things can be in one of 2 states,
the number of possible combinations
is equal to 2^3.
But if I have 3 things, 2 of which can
be in 2 states and the other in 3 states,
what's the simplest...
|
by: csledge |
last post by:
Hi,
I am trying to compute a 64 bit result from 2 32 bit
registers, How do I get the carry into the higher word ?
Also is %lld correct ?
#include<stdio.h>
long long int64( long x, int y);...
|
by: gchq |
last post by:
Hi there
I am attempting to compute some values from a DataTable. It works fine as long as there is a value to retrieve, otherwise it blows out!
This will work as there are valid dates:-
...
|
by: milirica |
last post by:
I have a code, where I should compute the App.Compute Time, for n=1,5,10,20,30,40,50.Is there any GOOD EXPERT that can make this clear to me ? If yes than PLease write here
The code is:
#include...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |