Thread: My Battle formula

  1. #1
    Registered User
    Join Date
    Oct 2003

    My Battle formula

    	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. #2
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    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.
    Do not make direct eye contact with me.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > (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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2003
    how would I go about doing that?

  5. #5
    Registered User
    Join Date
    Oct 2003
    ah, nevermind I got it. I changed one variable to a double

  6. #6 JasonD's Avatar
    Join Date
    Mar 2003
    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 subscribe to a feed

Similar Threads

  1. Zeller's Formula
    By unejam2005 in forum C++ Programming
    Replies: 6
    Last Post: 11-14-2005, 09:48 PM
  2. how to change char to formula
    By kosong in forum C Programming
    Replies: 2
    Last Post: 06-09-2003, 04:31 AM
  3. Need help with this formula -b/2a..Please read.
    By foofoo in forum C Programming
    Replies: 4
    Last Post: 06-04-2002, 11:59 PM
  4. Formula string conversion
    By Gr3g in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2002, 08:28 PM
  5. The battle.
    By Sentaku senshi in forum A Brief History of
    Replies: 10
    Last Post: 03-25-2002, 05:28 PM