Float Multiplication Error

closed account (L0Shb7Xj)
THIS PROBLEM HAS BEEN RESOLVED. THANK YOU FOR ALL YOUR HELP!

Hello. I'm trying to write a function for a game. One function is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
int damagecalc(aclass a, aclass b, anotherclass m)//Calculates damage done with one attack
{
        int ret_damage;                         //Final damage
        float damage;                           //Net Damage done
        float level;                            //Level of the attacker
        float attack;                           //Attack stat of the attacker
        float defense;                          //Defense stat of the attacked
        float base;                             //Base damage of the attack
        float modifier;                         //Formula as below

        float stab;                             //Same Type Attack Bonus
        float type;                             //Type Effectiveness
        float critical;                         //Critical Ratio
        float other;                            //Accounts for held items
        float randomnum;                        //A number from 0.85 to 1.00

        level=a.level;
        attack=a.attack;
        defense=b.defense;
        base=m.basepower;

        if (a.type1==m.type)
        stab=1.5;
        else if (a.type2==m.type)
        stab=1.5;
        else stab=1;

        type=typeeffectcalc();
        //Critical calcs go here, but for now,
        critical=1;

        int temp;

        temp=((rand()%16)+80)/100;

        randomnum=(float)temp;

        modifier=stab*type*critical*rand;       //Formula for modifier

        damage=((((2*level)+10)/250)*(attack/defense)*(base)+2)*modifier;
                    //Formula (It's harder than it looks) (That's what she said)
        
        ret_damage=(int)damage;

        return ret_damage;                      //Returns net damage
}


So, I'm getting an error:
invalid operands of types `float' and `int ()()' to binary `operator*'

and it highlights this line
modifier=stab*type*critical*rand;
(this line has been underlined above)

So, what am I doing wrong? All definitions outside of this section of the code are okay. I'm pretty sure that the problem is float multiplication.

Well, any help would be very appreciated.

Thanks,
WJr

Edit: Just a quick note. The components of the formula themselves can have a decimal point (hence the use of float), but the final result is rounded off and returned as an integer.

Edit 2: Just a side question. Is it possible to manually round off a float downwards to an integer? For eg, 2.112 becomes 2, 2.9999 also becomes 2.
Last edited on
Should it be:

 
modifier = stab * type * critical * randomnum;


?
closed account (Dy7SLyTq)
ihutch is right. you are calling rand, a variable that does not exist
closed account (L0Shb7Xj)
Aw, crap. I forgot to change that when I changed the variable name. But then shouldn't it just give an "undefined" error?
closed account (Dy7SLyTq)
no because im sure rand is declared externally in one of the c headers
to your second question. yes
#include <iomanip>
and then use setprecision
In fact, rand is defined in stdlib.h (or cstdlib) as a function which returns an int and requires no parameter.
Essentially, it has type int ()();
(Yes, it's a type. Yes, functions have types. Yes, you can store a pointer to a function as you would with a normal variable)

How to calculate a function's type:

ReturnValue (FunctionProperties)(FunctionParameters);

Let me show you another mistake you did:

1
2
int temp;
temp=((rand()%16)+80)/100;


No matter what, temp will always be 0 (or 1?).

Reason? Think of it like this:
1
2
int temp_min = 80/100; // Int doesn't store floating point. Either 0 or 1.
int temp_max = 96/100; // Same. 


Make it go like this:
1
2
3
// Multiplication is usually faster than division.
// Make a good use of math properties, (x * 0.01f) = (x/100.f)
float temp = (((float)rand())+80.f) * 0.01f;
Last edited on
closed account (L0Shb7Xj)
The real error here is that I'm a complete f***in' idiot. Thanks a lot, guys.
Topic archived. No new replies allowed.