Let’s say I want to split a percentage of a given value across all players based on the total stock they have compared to each other. For this example, the value is 40
, and the percentage is 20%
, so 8
is to be split across the players. Two players each have 20
stock, and the others do not have stock (40
stock total).
float stockFraction = stock[i] / allStock;
int total = (int)(value * percentage * stockFraction);
Debug.LogFormat("{0} * {1} * ({2} / {3}) = {4}", value, percentage, stocks[i], allStock, (float)(total * percentage * stockFraction));
Whenever I run this batch of code in my game, both players get 40 * 0.2 * (20 / 40) = 0
.
However, this math is incorrect, because:
40 * 0.2 * (20/40) = 40 * 0.2 * 0.5 = 40 * 0.1 = 4
I get 0
regardless of whether the value is an int
or a float
. Is there something I do know about float arithmetic, or am I missing something in my equation(s)?