Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » zMUD General Discussion
jon_stroble
Newbie


Joined: 08 Dec 2001
Posts: 3
Location: USA

PostPosted: Tue Dec 11, 2001 6:19 am   

#MATH function
 
Hello again,

I have created a trigger to count the number of hits I take during a battle, and also the number of times my opponent misses me. The running total is stored in two variables (@hits and @misses, respectively).

At the end of the battle, I have another trigger that spits out the final tally of hits and misses using #SHOW.

I would -like- also to show the hit percentage using this formula:
hits/(hits+misses)*100

Is this type of division possible using the #MATH function?

Thanks again,
jon.
Reply with quote
undergod
Wanderer


Joined: 27 Jun 2001
Posts: 82

PostPosted: Tue Dec 11, 2001 7:54 am   
 
quote:

I would -like- also to show the hit percentage using this formula:
hits/(hits+misses)*100

Is this type of division possible using the #MATH function?



Yes it is. Try something like this. Create a new variable, (percent for example) and do this:

#math percent {(@hits/(@hits+@misses))*100}

that should work :)
Reply with quote
jon_stroble
Newbie


Joined: 08 Dec 2001
Posts: 3
Location: USA

PostPosted: Tue Dec 11, 2001 8:07 am   
 
Thanks for the suggestion! I gave it a try, and similar to my earlier attempts I was unable to come up with the correct result. Apparently the division (/) operator does not handle anything after the decimal place?
Reply with quote
Emit
Magician


Joined: 24 Feb 2001
Posts: 342
Location: USA

PostPosted: Tue Dec 11, 2001 9:16 am   
 
ah, i had a similar problem, and worked around the decimal issue like this:
100*hits/(hits+misses)

the way your tried first won't work, because zmud drops everything after the decimal place after performing the hits/(hits+misses) part of the operation. when you multiply and then divide, it will still drop the decimal part, but the number it leaves over will be within .9999999... of the correct percent, expressed in whole numbers(if i'm understanding it correctly)
Emit
Reply with quote
MattLofton
GURU


Joined: 23 Dec 2000
Posts: 4834
Location: USA

PostPosted: Wed Dec 12, 2001 1:45 am   
 
quote:

ah, i had a similar problem, and worked around the decimal issue like this:
100*hits/(hits+misses)

the way your tried first won't work, because zmud drops everything after the decimal place after performing the hits/(hits+misses) part of the operation. when you multiply and then divide, it will still drop the decimal part, but the number it leaves over will be within .9999999... of the correct percent, expressed in whole numbers(if i'm understanding it correctly)
Emit




Pretty much true, except when one of the variables in the calculation is a floating-point number. 5/2 will probably give you 2, but 5/2.5 will give you a blank line.

li'l shmoe of Dragon's Gate MUD
Reply with quote
franky on holy mission 3
Newbie


Joined: 14 Dec 2001
Posts: 1
Location: Austria

PostPosted: Fri Dec 14, 2001 9:39 am   
 
hmmm how the heck the var should get out of the normal numbers?
there is no half-hit but only a hit or a miss :P
Reply with quote
Darker
GURU


Joined: 24 Sep 2000
Posts: 1237
Location: USA

PostPosted: Fri Dec 14, 2001 3:05 pm   
 
Ahh, the grief of percentages.



zMUD 6 Online Help: All the power you'll ever need.
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5182

PostPosted: Sat Dec 15, 2001 4:10 am   
 
Zmud's script language is mostly based around integer math. Which means you have to smarter then the computer!
As undergod showed do your multiplication first to provided enough room to not lose decimal places. If you wish to get behind the decimal point you may wish to design your own short version of a floating point operation. Since all the items your working with are integers it is really easy through the use of the %mod function. Quick example is:
#SHOW %eval(@hits*100/(@hits+@misses)).%mod(%eval(%hits*1000/(@hits+@misses)),10)
That gives you one decimal place. I am sure you can figure how to use the MATH command if you wish.
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » zMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net