I have the following in a program (part of a much larger function, but this is the relevant testing bit):
int test = 100 + (100 * (9 / 100)); sprintf (buf, 'Test: %d\n\r', test); display_to_pc (buf, player);
Basically it amounts to:
x = a + (a * (b / 100))
Where a is a given figure, b is a percentage modifier, and x is the result (the original plus a percentage of the original)… I hope that makes sense.
It gives me:
Test: 100
I thought the math in my head might be wrong, but I’ve checked several calculators and even the expression evaluator in my IDE, and they all give me the expected result of 109 for the first expression.
Can anyone enlighten me as to what I’m missing here?
Thanks much. 🙂
Replace
with
and it will work as expected.
9 / 100is performed using integer division; the nearest integer to.09is0(and0 * 100is still0).Doing the multiplication first results in
900 / 100, which gives you the9that you were expecting.If you need more than integer precision, you may need to go the floating point route.