Re: [CODE] Order of Evaluation and Shorthand Problem

From: Dan Merillat (harik@chaos.ao.net)
Date: 04/15/00


LegalWriter writes:
> I'm having a problem with the order in which a simple expression is being
> evaluated in conjunction with a shorthand assignment operator.
>
> The relevant lines are:
>      (GET_RV(victim, DAM_GEN_INERTIA) = 50;
>      dam = 230;
>      dam *= (GET_RV(victim, DAM_GEN_INERTIA) / 100);
>
> After this line executes, dam = 0.  In actuality, it should equal 115, since
> (by my understanding of the order of evaluation) 50 divided by 100 equals
> 1/2.  230 multiplied by 1/2 equals 115.  Yes?

Yup.  In floating point.

In integer math, 1/2 = 0

What you need to do is split your function like this:

        dam *= (GET_RV(victim, DAM_GEN_INERTIA));
        dam /= 100;

Of course, dam needs to be an int (not a char, quite often it's a char/byte value)
or you are quite likely to overflow.

--Dan


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     |  http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html  |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 04/10/01 PDT