Jump to content
  • Advertisement
Sign in to follow this  
DarkAnima

trouble with doubles

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Quote:
Original post by Dranith
Just 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 this post


Link to post
Share on other sites
Quote:
Original post by rozz666
Quote:
Original post by Dranith
Just 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!