trouble with doubles

This topic is 3794 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I've been working on a game for a while, now, and I have a decent, text-based version beginning to take form. Lately I've had trouble with a minor issue I discovered with a double value. In my game (an RPG) there is a status called "enfeebled" which lowers the defense of the effected character. Instead of the loss of defense being consistent, the double 'defmultiplier' is changed by the equation defender.defmultiplier=defender.defmultiplier*(3/(statushit+3)); where 'statushit' is a variable figured based on the opponent's ability to inflict the status and the defending character's ability to block the status change. I've gone through a step-by-step debug of that section of the program, and no matter the value of statushit variable, defender.defmultiplier always ends up going to 0.0000000 after that line of code is evaluated. I've tried declaring defmultiplier both as a float and as a double and the result is always the same. Does anyone have any ideas as to why the function is doing this?

Share on other sites
Maybe it's using integer division try:

defender.defmultiplier=defender.defmultiplier*(3.0/(statushit+3.0));

Share on other sites
*dances* yay, it worked! Thank you!

Share on other sites
Just to build on that in case you don't understand, the reason WHY it works is if you divide an int by an int, the result is an int. Any fractional portion of the int gets truncated (not rounded). So when you did (3/(statushit+3)), the denominator and the numerator were integers so the result was an integer. '3.0' represents a double constant(and '3.0f' represents a float constant), so when you replaced it you had (double/double) => double. Also as mentioned below me, (double/int) or (int/double) both result in a double value.

*Edit: Fixed some retardation

Share on other sites
Quote:
 Original post by DranithJust to build on that in case you don't understand, the reason WHY it works is if you divide an int by anything, or divide anything by an int, the result is an int. Any fractional portion of the int gets truncated (not rounded). So when you did (3/(statushit+3)), it did not matter at all what was in the denominator, since the numerator '3' represents an integer constant in C++. This means you had (int/*) => int. '3.0' represents a double constant(and '3.0f' represents a float constant), so when you replaced it you had (double/double) => double.

That's wrong. When one of the operands is double, then the result is also double.
E.g. 1/3.0 == 1.0/3 = 1.0/3.0 = 0.3333333333

The reason OP's formula didn't work was probably because statushit was an integer and the expression (3/(statushit+3) < 1 for statushit > 0 which was truncated to 0.

Share on other sites
Quote:
Original post by rozz666
Quote:
 Original post by DranithJust to build on that in case you don't understand, the reason WHY it works is if you divide an int by anything, or divide anything by an int, the result is an int. Any fractional portion of the int gets truncated (not rounded). So when you did (3/(statushit+3)), it did not matter at all what was in the denominator, since the numerator '3' represents an integer constant in C++. This means you had (int/*) => int. '3.0' represents a double constant(and '3.0f' represents a float constant), so when you replaced it you had (double/double) => double.

That's wrong. When one of the operands is double, then the result is also double.
E.g. 1/3.0 == 1.0/3 = 1.0/3.0 = 0.3333333333

lol yes, I got it backwards for whatever reason. I feel stupid now, thanks for pointing it out though.

1. 1
2. 2
3. 3
Rutin
14
4. 4
khawk
11
5. 5

• 9
• 9
• 11
• 11
• 23
• Forum Statistics

• Total Topics
633671
• Total Posts
3013268
×