Re: [Circle] [NEWBIE] stupid code question

From: Rasmus 'Con' Ronlev (
Date: 08/18/96

On Sun, 18 Aug 1996 wrote:

> I've added the following snippet of code to fight.c
> int percent;
> percent = (GET_HIT(victim) / GET_HIT_MAX(victim)) * 100
> Problem is, it's getting the right GET_HIT and GET_HIT_MAX values but
> not doing anything in percent.
> Percent continues to show 0 and doesn't change.

The problem is dividing using integers (which GET_HIT and GET_MAX_HIT
return)... an integer division in C, is allways ROUNDED DOWN to the next
'whole' number, in this case you'd allways get xx / xx+somefin i.e. it'll
allways be less than 1 the result, thus beeing rounded down to 0, and
well.. 0 times 100 is 0... *grin*

Instead you could do : percent = (GET_HIT(ch) * 100) / GET_MAX_HIT(ch);

Easiest... other things might include using whats called casting. Casting
is something that the evil makers of C made (or the ANSI comitee, don't
know really, but it's there nevertheles, and it allways confuses me.. *g*)

casting is to add a datatypes in parantheses in a statement, or multiple
places like (int), (double) etc...

This way you could do:

int percent;

percent = (double) GET_HIT(ch)/GET_MAX_HIT(ch) * 100;

which will convert the GET_HIT and GET_MAX_HIT stuff into calculations
using double datatypes instead of int in the division. using double
datatypes means that you use dicimal precision in the division, and you
end up with the same result as the above solution more or less at least.

Any corrections should be sent to the LIST, and not me personally, as it's
of no interest to my person (not anything I use :).


     Rasmus Rønlev DOEK'94      WWW:
     Student instructor         MUD: 5000
       Student, B.Sc in Computer Science and Business Administration.

| Ensure that you have read the CircleMUD Mailing List FAQ: |
|   |

This archive was generated by hypermail 2b30 : 12/07/00 PST