Thread: My Battle formula

1. 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!

2. 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.

3. > (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

4. how would I go about doing that?

5. ah, nevermind I got it. I changed one variable to a double

6. 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.

Popular pages Recent additions