![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Jan 2006
Location: London, Ontario, Canada
Posts: 16
Rep Power: 0
![]() |
Hanging For loop in C++
Hi everyone,
I am just curious if anyone has come across why a for loop used in a number crunching scheme would hang when and not return the command prompt. No compilation errors and the conditions for the loops are defined. Just to be more direct, here is the code. It might be too complex but it is fairly simple 10 loops represent summations of a mathematical function and 1 outer loop for indexing them all into an array. This is just the basics of it. void h1_tm(double *inner1_tm)
{
//h1 variables
double t1_1a, t1_2a, t1_3a, t1_4a, t1_5a; //variables
double re1_1a, im1_1a, re1_2a, im1_2a, tot1_a; //variables
double re1_1b, im1_1b, re1_2b, im1_2b, tot1_b; //conjugates
int i, j, l, m, n, p; //indices (int)
// double j, l, m, n; //indices (double)
double c = GSL_CONST_MKSA_SPEED_OF_LIGHT;
double k = (4.0*M_PI*f_0*R_E*sin(alpha))/c;
double w_0 = 2.0*M_PI*f_0; // radial frequency of GW signal
double w_r = R; // rotational frequency of Earth
double w_orb = 1.0; // Earth orbital rotational frequency
//outer loop for indexing total
for (i = 0; i <= 0; i++) {
//inner summation loops for functions and conjugates
for (j = 1; j <= 365; j++) {
for (j = 1; j <= 365; j++) {
for (p = 0; p <= 5; p++) {
for (p = 0; p <= 5; p++) {
for (n = -5; n <= 5; n++) {
for (n = -5; n <= 5; n++) {
for (l = 0; l <= 5; l++) {
for (l = 0; l <= 5; l++) {
for (m = l; m <= l; m++) {
for (m = l; m <= l; m++) {
//h1 functions
gsl_complex cpxr1_a = gsl_complex_rect(0.0, 1.0); // z = 0 + i
gsl_complex t1_1a = gsl_complex_pow_real(cpxr1_a, l+n);
im1_1a = GSL_IMAG(t1_1a);
re1_1a = GSL_REAL(t1_1a);
t1_2a = (pow(2.0,0.5)*pow(M_PI,(5.0/2.0)))/w_r;
t1_3a = (pow(k,l+0.5))/(pow(2.0,2.0*l+1.0));
t1_5a = gsl_sf_fact(p);
t1_4a = pow(((-k*k)/16.0),p)/t1_5a;
gsl_complex cpxp1_2a = gsl_complex_polar(1.0, M_PI*j*l-M_PI*l);
re1_2a = GSL_REAL(cpxp1_2a);
im1_2a = GSL_IMAG(cpxp1_2a);
tot1_a = im1_1a*re1_1a*t1_2a*t1_3a*t1_4a*im1_2a*re1_2a;
// cout << tot1_a << endl;
//h1 conjugates (all other terms same as above except for complex terms)
gsl_complex cpxr1_b = gsl_complex_rect(0.0, -1.0); // z = 0 - i
gsl_complex t1_1b = gsl_complex_pow_real(cpxr1_b, l+n);
im1_1b = GSL_IMAG(t1_1b);
re1_1b = GSL_REAL(t1_1b);
gsl_complex cpxp1_2b = gsl_complex_polar(1.0, -1.0*M_PI*j*l-M_PI*l);
re1_2b = GSL_REAL(cpxp1_2b);
im1_2b = GSL_IMAG(cpxp1_2b);
tot1_b = im1_1b*re1_1b*t1_2a*t1_3a*t1_4a*im1_2b*re1_2b;
// cout << tot1_b << endl;
inner1_tm[i] = tot1_b*tot1_a;
// cout << inner1_tm[i] << endl;
}
}
}
}
}
}
}
}
}
}
}
}Anyone have any ideas??? Thanks. :banana:
__________________
Greatness courts failure and solitude. --- Anonymous Last edited by can342man; Apr 27th, 2006 at 4:03 PM. |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
We're not mind-readers. Post some code. Read about how to post a sensible question somewhere. Eat that freakin' banana, it might help.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
I would just start the debugger and step through the loop once.
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 893
Rep Power: 4
![]() |
Whether it is hanging depends on how long you think it should take for 3.5 billion sets of calculations.
|
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
ROFL @ Steven and The Dark. See how editing the code in cleared that up?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
hmmm ..I wonder if he is still stepping
..how to keep a canman occupied
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
|
#7 |
|
Professional Programmer
|
What I want to know is what he THOUGHT that code was going to do...
|
|
|
|
|
|
#8 |
|
Newbie
Join Date: Jan 2006
Location: London, Ontario, Canada
Posts: 16
Rep Power: 0
![]() |
The code is just one part of a long calculation for the gravitational wave signal from a pulsar star. The mathematical functions are not that significant in this particular problem. It is mainly a for loop problem and hence the whole thing can be reduced to a simple for loop question.
I will try the debugger. Thanks
__________________
Greatness courts failure and solitude. --- Anonymous |
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Given that some of your inner loops are changing variables that control your outer loops, it is a little hard to predict the behaviour of your program at first glance.
I'm not necessarily sure that the loops are infinite, but you will certainly find the sequence entertaining when stepping through in a debugger. |
|
|
|
|
|
#10 |
|
Professional Programmer
|
1*365*365*6*6*11*11*6*6*1*1=20891811600
That's 20,891,811,600 times you are calculating whatever is inside the for loops. I think there is a mistake in your logic somewhere..? |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|