Wednesday, March 28, 2012

Simple Arithmatic!

I do not know what i'm doing wrong but I can seem to get simple arthimatic to work. I've tried everything. When I do simple stuff like:

shotAccuracy = (shotsOnTarget);

works but if I want to anything else like:

shotAccuracy = (100 / totalShots) * (shotsOnTarget);

it doesn't work and returns zero as the answer. Couls somebody please tell me why I can't get simple arthimatic to work. Thank you

Hi,

If totalShots is aninteger then division by 100 would be rounded off. For e.g.: 100/999 = 0 as everything after the decimal goes off. So you need to usefloats for that.

One more thing: the formula for shotAccuracy should be:

shotAccuracy = (shotsOnTarget / totalShots ) * 100;

This will give you the accuracy in percentage. Make sure that all the variables are infloat.

Regards,

Vivek


You shuold always multiple by 100 before division. This reduces the chance of lossing value in division rounding.

shotAccuracy = (shotsOnTarget * 100) / totalShots;
Thanks for your help guys.

0 comments:

Post a Comment