In Pokecheck's VS. Player, we calculate the final modifier in pieces based on if situation->then chain each modifier with current modifier step by step.
I'd just gather up all the modifiers that you have to chain and then loop through them. You can't do it all in one calculation as the bitshifting drops an (albeit small) fraction of the number each time.
Modifier chaining is rounded @ 0.5, and every other calc is rounded down. You're doing it right for the first half when modifiers are involved, but when you calculate final damage after altering with the random factor you're supposed to drop any decimal.
"D' = (D * (100-R)) ÷ 100"
remember, "In this article, ÷ always denotes the in-game unsigned divmod function, i.e. the unrounded/truncated integer division."
Honko has programmed his damage calculator to use the BW damage formula, you can see how all the damage chaining works
here.
also
here's the source for Pokecheck's battle video parser (VS. Player)