swarm-modeling
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Swarm-Modelling] possible problem comparing doubles


From: paul box
Subject: Re: [Swarm-Modelling] possible problem comparing doubles
Date: Wed, 28 Feb 2007 23:32:05 +0930

Hi Rick

I posted this at a point where I was punchy and running around in
circles.  I think I may have found the problem.

I had sent this message from the wrong account (not the one from which
I had subscribed to swarm.org), so thankfully, you and I are the only
ones who got this.  When I identified the problem, I was breathing a
sigh of relief that the message had not gone through to the modeling
list.


On 2/28/07, Rick Riolo <address@hidden> wrote:

hi paul,

from a quick scan of this, i do not see the problem.
i assume this is a typo:
      bestPoool = myPool;

its fine to do > < comparisons with doubles.

i guess i would suggest some brute for debugging, starting with
changing your loop to this:

  for (i=0; i<[[myPool getUpstreaPools] getCount]; i++){
    double aDen, bestDen;
    aPool = [[myPool getUpstreamPools] atOffset: i];
    aDen = [aPool getShrimpDensity];
    bestDen = [bestPool getShrimpDensity];
    fprintf( stderr, " ... print out aDen and bestDen " ... );
    if ( aDen < bestDen )
        bestPool = aPool;
        fprintf( stderr, " bestPool changed to ..." );
    }
  }

if that doesn't work, you should have some clues.
if it does work...i dunno, i don't know why it
wouldn't work the way you had it.

- r

Rick Riolo                           address@hidden
Center for the Study of Complex Systems (CSCS)
323 West Hall
University of Michigan         Ann Arbor MI 48109-1107
Phone: 734 763 3323                  Fax: 734 763 9267
http://cscs.umich.edu/~rlr

On Wed, 28 Feb 2007, paul box wrote:

> Date: Wed, 28 Feb 2007 15:32:07 +0930
> From: paul box <address@hidden>
> Reply-To: Agent-based modeling <address@hidden>
> To: Agent-based modeling <address@hidden>
> Subject: [Swarm-Modelling] possible problem comparing doubles
>
> Hello all
>
> I'm having a problem with a simulation, where the culprit line seems
> to be a comparison between doubles.
>
> I'm describing shrimp migrating upstream from pool to pool.  In this
> scenario, the only thing the shrimp wants is to find the least crowded
> pool upstream (upstream pool with the lowest shrimp density). The pool
> contains the following characteristics:
>
> @interface Pool {
>   ...
>  id <List> upstreamPoolList;  // any number of pools upstream
>  double shrimpDensity;  // number of shrimps / area
>  double area;               // pool surface area
> id <List> shrimpList;
>
>  ...
> }
>
>
>
> @implementation Pool {
>
>  ...
>  -(double) getShrimpDensity {return shrimpDensity;}
> - updateShrimpDensity {
>   shrimpDensity = (double) [shrimpList getCount] / area;
>   return self;
> }
>
> ...
> @end
>
> The shrimp contains the following:
>
> @interface Shrimp {
>
>   Pool * myPool;  // the pool I'm currently inhabiting
> }
>
> @implelmentation Shrimp {
>
> ...
> -step {
>   [self moveToPool: [self findBestPool]];
>   return self;
> }
>
> -findBestPool {
>   Pool * aPool;
>   Pool * bestPool;
>   int i;
>
>   bestPoool = myPool;
>
>   for (i=0; i<[[myPool getUpstreaPools] getCount]; i++){
>     aPool = [[myPool getUpstreamPools] atOffset: i];
>    if ([aPool getShrimpDensity] < [bestPool getShrimpDensity])
>     bestPool = aPool;
>    }
>
>   return bestPool;
> }
>
>
> This code was not producing results that made sense.  When I changed
> the [getShrimpDensity] to the following, it works fine:
>
> -(int) getShrimpDensity { return (int) (shrimpDensity * 100000);}
>
> That changed the statement in [findBestPool ] to compare integers
> instead of doubles.  I did not want to write the statement that way,
> because I don't want to be concerned in this stage as to whether
> 100000 is an appropriate scaling number or not.
>
> Is comparison of doubles in a logical statement a sin?  I have always
> avoided testing for equality in doubles, but I had thought that simply
> choosing the larger of two doubles a valid, if potentially sloppy,
> operation.
>
> The above code failed even when comparing doubles of several orders of
> magnitude difference (e.g., 70.334 > 0.00135, or 70.334 > 0).
>
> --
> //////////////////////////
> // Paul Box
> // Alice Springs, NT  Australia
> //
> _______________________________________________
> Modelling mailing list
> address@hidden
> http://www.swarm.org/mailman/listinfo/modelling
>
_______________________________________________
Modelling mailing list
address@hidden
http://www.swarm.org/mailman/listinfo/modelling



--
//////////////////////////
// Paul Box
// Alice Springs, NT  Australia
//


reply via email to

[Prev in Thread] Current Thread [Next in Thread]