-
My Battle formula
Code:
double damage = (player1.attack * player1.curWeapon->power)
/(mob.defense * mob.armor) * (player1.curWeapon->power * rando);
return damage;
VC6 is returning 0 every time. I debugged, here are the values.
attack = 14
armor = 12
defense = 12
power = 7
rando = 1.5
I did it all in a calculator, and it came out to 8 or something. What's wrong?? Please help me out man!
-
That isn't enough to determine the problem. Only thing that *might* be true, but we can't figure out with that code, is either you used the wrong armor / some other value when calculating, or "rando" stands for random and its generating a 0, thus multiplying by anything returns 0.
-
> (player1.attack * player1.curWeapon->power)
> /(mob.defense * mob.armor)
( 14 * 7 ) / ( 12 * 12 )
Is zero, when performed using integer arithmetic.
The final multiplication is therefore irrelevant.
You need to make part of this initial sub-expression of type double if you want to avoid this
-
how would I go about doing that?
-
ah, nevermind I got it. I changed one variable to a double
-
The other possibility is to multiply first, and then divide. Ensure that you do not get any overflows doing this. Check the formula against extreme values that would create the largest number after the multiply, and see if it still fits within the maximum size of the integer type you are using. This way, you needn't use floating point numbers at all.