hi all,
i am looking forward to finding someone who could help me out. from
the following code,
i get 45 for the variable "numSusceptible" and 50 for "numNode". and
then i get 5 for "(numNodes - numSusceptible)". however, i get zero
when i calculate "(numNodes - numSusceptible) / numNodes". i don't
understand what is going on here. I would appreciate it if anyone
could help me understand. many thanks in advance.
private void getPrevalence() {
double prevalence = 0;
int numSusceptible = 0;
String string;
for (int i = 0; i < numNodes; i++) {
GayNode iNode = (GayNode) agentList.get(i);
string = iNode.getInfectStatus();
System.out.printf("status = %s ", string);
if (string.equals("s")) {
numSusceptible += 1;
}
}
prevalence = (numNodes - numSusceptible) / numNodes;
System.out.printf("\nSusceptibles = %d ", numSusceptible);
System.out.printf("numNodes = %d ", numNodes);
System.out.printf("prevalence =%.4f \n", prevalence);
//return prevalence;
}I
Rhino - 27 Dec 2005 19:34 GMT
> hi all,
> i am looking forward to finding someone who could help me out. from
[quoted text clipped - 23 lines]
> //return prevalence;
> }I
Your problem is that Java is doing integer arithmetic when you expect it to
do decimal arithmetic.
When you see the equation:
prevalence = (50- 45) /50;
you expect Java to evaluate it as:
prevalence = 5/50;
prevalance = 0.1;
But Java actually sees 5/50 as an _integer_ expression so it gives you 0
because 0 is the integer that is closest to the value of 5/50. That probably
seems odd because you've defined prevalence as being a double so you expect
the result to be 0.1 but this is the way Java works by default.
If you want Java to use decimal arithmetic, you need to write something like
this:
prevalence = ((double) (numNodes - numSusceptible))/((double) numNodes);
In this version of the statement, the expression (numNodes -
numSusceptible), which is an integer since both variables are integers,
gives an integer result but the '(double)' that precedes the expression
casts (converts) the integer result to a double; thus, 5 turns into 5.0.
Also, the '(double)'
that prededes numNodes in the denominator casts 50 to 50.0. When you divide
5.0 by 50.0, the result is the double value 0.1, not the integer value 0.
If you don't like using casts, the other option you have is to make the
variables numNodes and numSusceptible doubles instead of ints; then you
won't need the casts and your expression to calculate prevalence doesn't
need to change at all.
Rhino
Mark Haase - 27 Dec 2005 22:43 GMT
> But Java actually sees 5/50 as an _integer_ expression so it gives you 0
> because 0 is the integer that is closest to the value of 5/50. That probably
> seems odd because you've defined prevalence as being a double so you expect
> the result to be 0.1 but this is the way Java works by default.
Its actually not the integer closest to 5/50, its the integer result of
division. There's a difference, because if you divided 9 / 10, you would
still get zero even though 1 is closer.
Integer division is like the division you did in 5th grade: 50/8 = 6 +
2R, where 6 is the integer quotient and 2R means 2 left over (in other
words, 2/8 or .25). So you can think of integer division as performing
proper division and throwing away everything after the decimal point --
even if you cast the result to double afterwards.
Example:
marks:~ mehaase$ cat Test.java
class Test {
public static void main(String args[]) {
double test = 9/10;
System.out.println("test is " + test);
}
}
marks:~ mehaase$ javac Test.java
marks:~ mehaase$ java Test
test is 0.0
There is a Math.round() operation if you want to round 9/10 up to 1.
|\/| /| |2 |<
mehaase(at)gmail(dot)com
Rhino - 28 Dec 2005 16:19 GMT
>> But Java actually sees 5/50 as an _integer_ expression so it gives you 0
>> because 0 is the integer that is closest to the value of 5/50. That
[quoted text clipped - 27 lines]
>
> There is a Math.round() operation if you want to round 9/10 up to 1.
I stand corrected.
Rhino
Jonie - 28 Dec 2005 19:33 GMT
Thank you very much, guys. Have a happy new year!!
Thomas Hawtin - 27 Dec 2005 19:55 GMT
> i get 45 for the variable "numSusceptible" and 50 for "numNode". and
> then i get 5 for "(numNodes - numSusceptible)". however, i get zero
> when i calculate "(numNodes - numSusceptible) / numNodes". i don't
> understand what is going on here. I would appreciate it if anyone
> could help me understand. many thanks in advance.
/ on integers does an integer division. 5 divided by 50 is 0 remainder
5. I think it's daft using the same symbol for different operations, but
it is traditional. To get a sensible floating point divide, cast at
least one of the operands to double:
prevalence = (numNodes - numSusceptible) / (double)numNodes;
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Roedy Green - 28 Dec 2005 01:28 GMT
>however, i get zero
>when i calculate "(numNodes - numSusceptible) / numNodes". i don't
>understand what is going on here. I would appreciate it if anyone
>could help me understand. many thanks in advance.
see http://mindprod.com/jgloss/division.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.