user1151: Here's what I have so far:
total_epochs = current_block_height % 210000
total_coins = 0;

for(i = 1; i <= total_epochs; i++) {
  reward = 100 / 2^(i * 210000 / 210000);
  total_coins += 210000 * reward;
}

But this overaccounts for the total coins. Any simpler strategy that works?

user575: With a\b representing integer division and a%b the remainder of this division:
  blocks_per_epoch = 210000;
  initial_reward = 5 * 10^9;

  current_epoch = current_block_height \ blocks_per_epoch;
  blocks_in_current_epoch = (current_block_height % blocks_per_epoch) + 1;
  current_reward = initial_reward / 2 ^ (current_epoch);

  total_coins = 2 * blocks_per_epoch * (initial_reward - current_reward) +
     blocks_in_current_epoch * current_reward;

Note that this can be off by a few satoshis due to a different rounding.
If you work with integers for the reward, the division for current_reward can be replaced with a bitshift. If not, you can change initial_reward to 50 to give result in BTC instead of satoshis.

1151: <p>Here's what I have so far:</p>

<pre><code>total_epochs = current_block_height % 210000
total_coins = 0;

for(i = 1; i &lt;= total_epochs; i++) {
  reward = 100 / 2^(i * 210000 / 210000);
  total_coins += 210000 * reward;
}
</code></pre>

<p>But this overaccounts for the total coins. Any simpler strategy that works?</p>
 575: <p>With a\b representing integer division and a%b the remainder of this division:</p>

<pre><code>  blocks_per_epoch = 210000;
  initial_reward = 5 * 10^9;

  current_epoch = current_block_height \ blocks_per_epoch;
  blocks_in_current_epoch = (current_block_height % blocks_per_epoch) + 1;
  current_reward = initial_reward / 2 ^ (current_epoch);

  total_coins = 2 * blocks_per_epoch * (initial_reward - current_reward) +
     blocks_in_current_epoch * current_reward;
</code></pre>

<p>Note that this can be off by a few satoshis due to a different rounding.</p>

<p>If you work with integers for the reward, the division for current_reward can be replaced with a bitshift. If not, you can change initial_reward to 50 to give result in BTC instead of satoshis.</p>
