My Battle formula

This is a discussion on My Battle formula within the C++ Programming forums, part of the General Programming Boards category; Code: double damage = (player1.attack * player1.curWeapon->power) /(mob.defense * mob.armor) * (player1.curWeapon->power * rando); return damage; VC6 is returning 0 ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    83

    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. #2
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > (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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

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

  6. #6
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    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, 08: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 Cprogramming.com
    Replies: 10
    Last Post: 03-25-2002, 04:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21