[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
3/4 = 0 ???
From: |
DARREN MATTHEW SCHREIBER |
Subject: |
3/4 = 0 ??? |
Date: |
Wed, 21 Jul 1999 12:12:40 -0700 (PDT) |
I am trying to debug this simple method from a Schelling model. The
function is to look at its neighbors, see their color, and report the
fraction of neighbors that are of a different color. The result of the
internal print statements look correct except that the math is wrong.
Fraction is defined as Others/Neighbors, but if it outputs others=3 and
neighbors=4 then fraction comes out as 0... why?
Any number divided by itself gives one, but any other division results in
0.000. This just doesn't make any sense to me and I am wondering if there
is something about float that I need to know?
Darren
-(float)getFractionOf: (int) otherColor {
float fraction=0.0;
int xvals[4]={0,0,1,-1};
int yvals[4]={-1,1,0,0};
int i,j;
int numNeighbors=0; //added for Version 2.5
int numOthers=0; //added for Version 2.5
Person * neighbor;
if(myType==1)
{
// This enforces a VonNeuman neighborhood
for(i=0;i<4;i++)
{
//printf(" %d : %d, %d ",i, myX+xvals[i], myY+yvals[i]);
if ((((myX + xvals[i]) < worldSize) && ((myX + xvals[i])
>=0))
&& (((myY + yvals[i]) < worldSize) && ((myY +
yvals[i]) >= 0)))
//keeps us on a 2d grid
{
numNeighbors=numNeighbors + 1;
if ((neighbor=[myWorld getObjectAtX: myX + xvals[i] Y:
myY+yvals[i]]))
if ([neighbor getColor]==otherColor)
numOthers=numOthers+1;
}
printf("X %d , Y %d ",myX, myY);
printf("Others %d/Neighbors%d ", numOthers,
numNeighbors);
if (numNeighbors != 0)
fraction=(numOthers/numNeighbors);
else
fraction=100.0; // This is a warning flag for division
by zero
printf("Fraction %3.3f \n", fraction);
}
}
else
{
// This enforces a Moore neighborhood
printf("Moore");
for(i=-1;i<2;i++)
for(j=-1;j<2;j++)
if (((myX+xvals[i]<worldSize) && (myY+yvals[i]<worldSize))
&& (!(myX+xvals[i]<0) && !(myY+yvals[i]<0))) //keeps
us on a 2d grid
{
numNeighbors=numNeighbors + 1;
if ((neighbor=[myWorld getObjectAtX: myX + xvals[i] Y:
myY+yvals[i]]))
if ([neighbor getColor]==otherColor)
numOthers=numOthers+1;
}
}
return fraction;
}
==================================
Swarm-Support is for discussion of the technical details of the day
to day usage of Swarm. For list administration needs (esp.
[un]subscribing), please send a message to <address@hidden>
with "help" in the body of the message.
- Zone usage: alloc: vs. allocBlock:, Ken Cline, 1999/07/20
- Re: Zone usage: alloc: vs. allocBlock:, Marcus G. Daniels, 1999/07/21
- Re: Zone usage: alloc: vs. allocBlock:, Ken Cline, 1999/07/21
- Re: Zone usage: alloc: vs. allocBlock:, Marcus G. Daniels, 1999/07/21
- Re: Zone usage: alloc: vs. allocBlock:, Ken Cline, 1999/07/21
- 3/4 = 0 ???,
DARREN MATTHEW SCHREIBER <=
- Re: 3/4 = 0 ???, Marcus G. Daniels, 1999/07/21
- Re: 3/4 = 0 ???, surak, 1999/07/21
- Re: 3/4 = 0 ???, Rick Riolo, 1999/07/21