I am dividing two int values, and im expecting to get a double one. but it works very strange, it has the correct values just before division but it doesnt give the right answer.
public void Analyse() {
for (FlowPacket fp : this.flow.GetAll()) {
if (fp.direction==1){
this.sentPackets++;
this.sentLength = this.sentLength + fp.packetLength;
}
else{
this.receivedPackets++;
this.receivedLength = this.receivedLength + fp.packetLength;
}
}
if(this.receivedPackets==0)
this.receivedPackets = 1;
}
public double CalcRatio() {
return (this.sentPackets/this.receivedPackets);
}
----------------------------main--------------------------------
System.out.print("Sent packets: " + analyser.getTotalSentPackets() + " , ");
System.out.print("Received packets: " + analyser.getTotalReceivedPackets() + " , ");
System.out.print("ratio: " + analyser.CalcRatio() + " , ");
----------------------------outout------------------------------
Sent packets: 2694 , Received packets: 5753 , ratio: 0
(double)this.sentPackets/this.receivedPackets
... should fix it.
The result of the division is cast to a double AFTER integer division (with rounding down) is performed. Cast one of the integers to a double BEFORE dividing so that double division occurs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With