Velocity Reviews > Loop doesn't behave the way it's supposed to.

# Loop doesn't behave the way it's supposed to.

Stephan Aspridis
Guest
Posts: n/a

 01-03-2004
Hi folks,

a happy new year. I have a little problem with a program I am writing at
the moment. A loop doesn't behave the way I'd like to (namely, the
"break" is ignored). This is the code in question:

----------------------------------
star[s].planet[0].orbit_distance =
pow(SOLDENSITY/EARTHDENSITY,1.0/3.0)*sqrt(star[s].mass)*get_random(50.0,150.0)/100.0;

for (p=0;p<orbcount-1;p++){
x = p+1;
star[s].planet[x].orbit_distance =
star[s].planet[p].orbit_distance*get_random(1250.0,2500.0)/1000.0;
if (star[s].planet[p].orbit_distance>star[s].orbit_limit)
break;
}//end for

star[s].number_of_orbits = p;
----------------------------------

"orbcount" is a variable of type int which normally has a value between
1 and 20. "star[s].orbit_limit" is an upper limit (type double).

The general idea was that upon reaching the limit, the loop ends and
"star[s].number_of_orbits" gets the last value of "p".

I asked the same question in de.comp.lang.c and the folks there came up
with this solution:

----------------------------------
ptr = star[s].planet;
e = ptr+orbcount;
d =
pow(SOLDENSITY/EARTHDENSITY,1.0/3.0)*sqrt(star[s].mass)*get_random(50.0,150.0)/100.0;
while (d<=star[s].orbit_limit&&ptr<e){
ptr->orbit_distance = d;
d *= get_random(1250.0,2500.0)/1000.0;
++ptr;
}//end while
star[s].number_of_orbits = ptr-star[s].planet;
----------------------------------

Problem is: It doesn't work either. I get the same error. To illustrate
what I mean here is an example of a system generated with this program:

-------------------------------
stellar characteristics of 1
type : K3 V
mass : 0.689 solar mass
luminosity : 0.225 solar lum
temperature : 4518 K
peak wavelength : 596 nm
diameter : 1081470 km
gravity : 31.519 G
lifespan : 30.581 billion years
rotational period : 193.5 hours
axial inclination : 18.0 deg.
number of orbits : 13
orbital limit : 4.286 AU <------ this is the limit...
eco zone
-inner boundary : 0.463 AU
-outer boundary : 0.743 AU
%
planets of 1 present at:
0.549 AU
1.208 AU
2.257 AU
5.395 AU <----- ...that is ignored
11.480 AU
14.862 AU
35.608 AU
51.075 AU
113.629 AU
224.052 AU
445.400 AU
765.494 AU
1840.211 AU
-------------------------------

Any idea anyone? I use lcc-win32 and gcc, so it probably isn't the compiler.

regards,
Stephan

Joona I Palaste
Guest
Posts: n/a

 01-03-2004
Stephan Aspridis <(E-Mail Removed)> scribbled the following:
> Hi folks,

> a happy new year. I have a little problem with a program I am writing at
> the moment. A loop doesn't behave the way I'd like to (namely, the
> "break" is ignored). This is the code in question:

> ----------------------------------
> star[s].planet[0].orbit_distance =
> pow(SOLDENSITY/EARTHDENSITY,1.0/3.0)*sqrt(star[s].mass)*get_random(50.0,150.0)/100.0;

> for (p=0;p<orbcount-1;p++){
> x = p+1;
> star[s].planet[x].orbit_distance =
> star[s].planet[p].orbit_distance*get_random(1250.0,2500.0)/1000.0;
> if (star[s].planet[p].orbit_distance>star[s].orbit_limit)
> break;

Probably stupid question, since I was too lazy to work out the
mathematical algorithm, but are you sure it isn't because you are
assigning to star[s].planet[x].orbit_distance, but comparing
star[s].planet[p].orbit_distance? Those are two different variables.

> }//end for

> star[s].number_of_orbits = p;
> ----------------------------------

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Stronger, no. More seductive, cunning, crunchier the Dark Side is."
- Mika P. Nieminen

Peter Pichler
Guest
Posts: n/a

 01-03-2004
"Joona I Palaste" <(E-Mail Removed)> wrote in message
news:bt7b2u\$ost\$(E-Mail Removed)...
> Stephan Aspridis <(E-Mail Removed)> scribbled the following:
> > Hi folks,

>
> > a happy new year. I have a little problem with a program I am writing at
> > the moment. A loop doesn't behave the way I'd like to (namely, the
> > "break" is ignored). This is the code in question:

>
> > ----------------------------------
> > star[s].planet[0].orbit_distance =
> >

pow(SOLDENSITY/EARTHDENSITY,1.0/3.0)*sqrt(star[s].mass)*get_random(50.0,150.
0)/100.0;
>
> > for (p=0;p<orbcount-1;p++){
> > x = p+1;
> > star[s].planet[x].orbit_distance =
> > star[s].planet[p].orbit_distance*get_random(1250.0,2500.0)/1000.0;
> > if (star[s].planet[p].orbit_distance>star[s].orbit_limit)
> > break;

>
> Probably stupid question, since I was too lazy to work out the
> mathematical algorithm, but are you sure it isn't because you are
> assigning to star[s].planet[x].orbit_distance, but comparing
> star[s].planet[p].orbit_distance? Those are two different variables.

I noticed that too, but shouldn't that only result in unnecessarily
calculating one too many orbits? start[s].planet[p].orbit_distance
is always known at the moments of the comparison...

Peter

Joona I Palaste
Guest
Posts: n/a

 01-03-2004
Peter Pichler <(E-Mail Removed)> scribbled the following:
> "Joona I Palaste" <(E-Mail Removed)> wrote in message
> news:bt7b2u\$ost\$(E-Mail Removed)...
>> Stephan Aspridis <(E-Mail Removed)> scribbled the following:
>> > Hi folks,

>>
>> > a happy new year. I have a little problem with a program I am writing at
>> > the moment. A loop doesn't behave the way I'd like to (namely, the
>> > "break" is ignored). This is the code in question:

>>
>> > ----------------------------------
>> > star[s].planet[0].orbit_distance =
>> >

> pow(SOLDENSITY/EARTHDENSITY,1.0/3.0)*sqrt(star[s].mass)*get_random(50.0,150.
> 0)/100.0;
>>
>> > for (p=0;p<orbcount-1;p++){
>> > x = p+1;
>> > star[s].planet[x].orbit_distance =
>> > star[s].planet[p].orbit_distance*get_random(1250.0,2500.0)/1000.0;
>> > if (star[s].planet[p].orbit_distance>star[s].orbit_limit)
>> > break;

>>
>> Probably stupid question, since I was too lazy to work out the
>> mathematical algorithm, but are you sure it isn't because you are
>> assigning to star[s].planet[x].orbit_distance, but comparing
>> star[s].planet[p].orbit_distance? Those are two different variables.

> I noticed that too, but shouldn't that only result in unnecessarily
> calculating one too many orbits? start[s].planet[p].orbit_distance
> is always known at the moments of the comparison...

You are right. Well, the only thing *I* can figure is that maybe the
orbit_distance field rolls over, and thus can never become bigger than
the orbit_limit field. As the OP hasn't shown us the definition of the
structures in question, I'm just shooting in the dark here.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"A computer program does what you tell it to do, not what you want it to do."
- Anon

Peter Pichler
Guest
Posts: n/a

 01-03-2004
"Stephan Aspridis" <(E-Mail Removed)> wrote in message
news:bt79ub\$a0o\$04\$(E-Mail Removed)-online.com...
> Hi folks,
>
> a happy new year. I have a little problem with a program I am writing at
> the moment. A loop doesn't behave the way I'd like to (namely, the
> "break" is ignored). This is the code in question:
>
> ----------------------------------
> star[s].planet[0].orbit_distance = pow(SOLDENSITY/EARTHDENSITY,1.0/3.0)
> *sqrt(star[s].mass)*get_random(50.0,150.0)/100.0;
>
> for (p=0;p<orbcount-1;p++){
> x = p+1;
> star[s].planet[x].orbit_distance = star[s].planet[p].orbit_distance
> *get_random(1250.0,2500.0)/1000.0;
> if (star[s].planet[p].orbit_distance > star[s].orbit_limit)
> break;
> }//end for

So, the next planet's orbit distance is between 1.5 and 2.5 times the
current distance, right? And you want to break the loop based on the limit,
but only after calculating the next orbit distance, right?

> planets of 1 present at:
> 0.549 AU
> 1.208 AU
> 2.257 AU
> 5.395 AU <----- ...that is ignored
> 11.480 AU

....

Unless I am missing something blatantly obvious, you should be calculating
orbits up to this point. In addition to what Joona said, I can come up with
two possible explanations: 1) you initialize star[s].orbit_limit too late or
2) you have some kind of an order-of-magnitude problem (initializing orbit
limit to 4000 rather than 4, for example)

Peter

Stephan Aspridis
Guest
Posts: n/a

 01-03-2004
Joona I Palaste wrote:

> You are right. Well, the only thing *I* can figure is that maybe the
> orbit_distance field rolls over, and thus can never become bigger than
> the orbit_limit field. As the OP hasn't shown us the definition of the
> structures in question, I'm just shooting in the dark here.
>

Here are the structures:

struct planet_record{
unsigned char core_type[20];
short int ident;
short int orbit_zone;
short int core_ident;
short int number_of_rings;
short int number_of_small_moons;
short int number_of_large_moons;
double orbit_distance;
double orbit_eccentricity;
double orbit_inclination;
double orbit_period;
double orbit_velocity;
double orbit_temperature;
double exosphere_temperature;
double max_temperature;
double min_temperature;
double aphel_sub;
double diameter;
double density;
double width;
double mass;
double surface;
double gravity;
double escape_velocity;
double base_temp;
double axial_inclination;
double rotation;
double geosync_orbit;
double horizon_at_eye_level;
double horizon_at_10m;
double horizon_at_100m;
double magnetic_field;
};

struct star_record{
unsigned char type[16];
short int ident;
short int number_of_orbits;
double mass;
double inclination;
double rotation;
double main_sequence_life;
double luminosity;
double temperature;
double main_sequence_temperature;
double diameter;
double gravity;
double lambda_max;
double period1_2;
double period12_3;
double period12_34;
double period3_4;
double mean_separation1_2;
double mean_separation12_3;
double mean_separation12_34;
double mean_separation3_4;
double orbit_limit;
struct planet_record planet[20];
};

Stephan Aspridis
Guest
Posts: n/a

 01-03-2004
Peter Pichler wrote:

>
>
> So, the next planet's orbit distance is between 1.5 and 2.5 times the
> current distance, right? And you want to break the loop based on the limit,
> but only after calculating the next orbit distance, right?
>
>

Yes, exactly.

>>planets of 1 present at:
>> 0.549 AU
>> 1.208 AU
>> 2.257 AU
>> 5.395 AU <----- ...that is ignored
>> 11.480 AU

>
> ...
>
> Unless I am missing something blatantly obvious, you should be calculating
> orbits up to this point. In addition to what Joona said, I can come up with
> two possible explanations: 1) you initialize star[s].orbit_limit too late or
> 2) you have some kind of an order-of-magnitude problem (initializing orbit
> limit to 4000 rather than 4, for example)
>

No to your first point. star[s].orbit_limit is initialized before this
loop. As for your second point - it gets exactly the value you see in
my OP. That were the first things I tried.

regards,
Stephan

Stephan Aspridis
Guest
Posts: n/a

 01-03-2004
Joona I Palaste wrote:

>
> You are right. Well, the only thing *I* can figure is that maybe the
> orbit_distance field rolls over, and thus can never become bigger than
> the orbit_limit field.

How can this happen?

regards,
Stephan

Peter Pichler
Guest
Posts: n/a

 01-03-2004
"Stephan Aspridis" <(E-Mail Removed)> wrote:
> Joona I Palaste wrote:
> > You are right. Well, the only thing *I* can figure is that maybe the
> > orbit_distance field rolls over, and thus can never become bigger than
> > the orbit_limit field.

>
> How can this happen?

It is very unlikely in your case. Joona didn't see your structures'
definitions, so he took a wild guess.

Peter

John L
Guest
Posts: n/a

 01-04-2004

"Stephan Aspridis" <(E-Mail Removed)> wrote in message news:bt79ub\$a0o\$04\$(E-Mail Removed)-online.com...
> Hi folks,
>
> a happy new year. I have a little problem with a program I am writing at
> the moment. A loop doesn't behave the way I'd like to (namely, the
> "break" is ignored). This is the code in question:
>
> ----------------------------------
> star[s].planet[0].orbit_distance =
> pow(SOLDENSITY/EARTHDENSITY,1.0/3.0)*sqrt(star[s].mass)*get_random(50.0,150.0)/100.0;
>
> for (p=0;p<orbcount-1;p++){
> x = p+1;
> star[s].planet[x].orbit_distance =
> star[s].planet[p].orbit_distance*get_random(1250.0,2500.0)/1000.0;
> if (star[s].planet[p].orbit_distance>star[s].orbit_limit)
> break;
> }//end for
>
> star[s].number_of_orbits = p;
> ----------------------------------
>
> "orbcount" is a variable of type int which normally has a value between
> 1 and 20. "star[s].orbit_limit" is an upper limit (type double).
>
> The general idea was that upon reaching the limit, the loop ends and
> "star[s].number_of_orbits" gets the last value of "p".
>
> I asked the same question in de.comp.lang.c and the folks there came up
> with this solution:
>
> ----------------------------------
> ptr = star[s].planet;
> e = ptr+orbcount;
> d =
> pow(SOLDENSITY/EARTHDENSITY,1.0/3.0)*sqrt(star[s].mass)*get_random(50.0,150.0)/100.0;
> while (d<=star[s].orbit_limit&&ptr<e){
> ptr->orbit_distance = d;
> d *= get_random(1250.0,2500.0)/1000.0;
> ++ptr;
> }//end while
> star[s].number_of_orbits = ptr-star[s].planet;
> ----------------------------------
>
> Problem is: It doesn't work either. I get the same error. To illustrate
> what I mean here is an example of a system generated with this program:
>
> -------------------------------
> stellar characteristics of 1
> type : K3 V
> mass : 0.689 solar mass
> luminosity : 0.225 solar lum
> temperature : 4518 K
> peak wavelength : 596 nm
> diameter : 1081470 km
> gravity : 31.519 G
> lifespan : 30.581 billion years
> rotational period : 193.5 hours
> axial inclination : 18.0 deg.
> number of orbits : 13
> orbital limit : 4.286 AU <------ this is the limit...
> eco zone
> -inner boundary : 0.463 AU
> -outer boundary : 0.743 AU
> %
> planets of 1 present at:
> 0.549 AU
> 1.208 AU
> 2.257 AU
> 5.395 AU <----- ...that is ignored
> 11.480 AU
> 14.862 AU
> 35.608 AU
> 51.075 AU
> 113.629 AU
> 224.052 AU
> 445.400 AU
> 765.494 AU
> 1840.211 AU
> -------------------------------
>
> Any idea anyone? I use lcc-win32 and gcc, so it probably isn't the compiler.
>

If the loop is OK, and if there is no problem anywhere else,
then is number of orbits == 13 because orbcount == 13 or is
orbit_limit an order of magnitude out?

John.