ScriptUO

Official ScriptUO EasyUO Scripts => Script Debug => Topic started by: Xclio on July 01, 2009, 12:39:16 AM

Title: Calculating a Percentage
Post by: Xclio on July 01, 2009, 12:39:16 AM
Alright well I am trying to calculate a percentage of successful attempts but I can't seem to get it to work in any fashion....look at what I have here and see if there is a fix...if it is even possible....or something, it is begining to make me mad as I have tried a few variations and this was the one that I thought would work...unfortunatly it doesn't.

Code: [Select]
sub UpdateAttempt
  menu set EUOEdit3 %sucessattempts
  menu set EUOEdit4 %failedattempts
  set %totalattempts ( %sucessattempts + %failedattempts )
  set %percent ( %sucessattempts / %totalattempts )
  set %sucesspercent ( %percent * 100 )
  menu set EUOedit5 %sucesspercent
return
Title: Re: Calculating a Percentage
Post by: rana70 on July 01, 2009, 01:45:36 AM
Hi,

your line
Code: [Select]
set %percent ( %sucessattempts / %totalattempts )is the problem .... you will get always 0 out of it !

Reason:
you will everytime end up with more total attempts than sucess attempts,
in the best case (100%) you will have an equal amount !

So it is usually like Devide:less / more -> result is always 0.xxxxx
EUO cuts off everything behind the .


try this

Code: [Select]
For %i 1 20
{
 Set %sucessattempts ( 20 - %i )
 Set %failedattempts %i
 GoSUB Calculate
 Display Results:$ GoodAttempt: %sucessattempts FailedAttempts: %failedattempts Total: %totalattempts Prozent: %percent $

}
SUB Calculate
 set %totalattempts ( %sucessattempts + %failedattempts )
 Set %sucessattemptsExtended %sucessattempts * 100
 set %percent ( %sucessattemptsExtended / %totalattempts )
RETURN
halt

Title: Re: Calculating a Percentage
Post by: Xclio on July 01, 2009, 05:20:17 AM
That is what I figured was happening but didn't really know why....guess EUO just sucks at handling math that requires decimals, my whole assuming is that it would be 0.xxxxx and thus the reason I was mutiplying by 100 in the last line.  I will give the code you put together a try and see how she turns out when I get home this evening.
Title: Re: Calculating a Percentage
Post by: Cerveza on July 01, 2009, 05:40:38 AM
I use percentages in my Taming Appraiser (http://www.scriptuo.com/index.php?topic=1089.0) which your welcome to check out and use if you need.
Title: Re: Calculating a Percentage
Post by: Superslayer on July 01, 2009, 12:21:21 PM
Hi,

your line
Code: [Select]
set %percent ( %sucessattempts / %totalattempts )is the problem .... you will get always 0 out of it !

Reason:
you will everytime end up with more total attempts than sucess attempts,
in the best case (100%) you will have an equal amount !

So it is usually like Devide:less / more -> result is always 0.xxxxx
EUO cuts off everything behind the .


try this

Code: [Select]
For %i 1 20
{
 Set %sucessattempts ( 20 - %i )
 Set %failedattempts %i
 GoSUB Calculate
 Display Results:$ GoodAttempt: %sucessattempts FailedAttempts: %failedattempts Total: %totalattempts Prozent: %percent $

}
SUB Calculate
 set %totalattempts ( %sucessattempts + %failedattempts )
 Set %sucessattemptsExtended %sucessattempts * 100
 set %percent ( %sucessattemptsExtended / %totalattempts )
RETURN
halt



This is great Rana, TY. Mind if I use it in my WT script?
Title: Re: Calculating a Percentage
Post by: KilroyIsDead on July 01, 2009, 12:49:06 PM
Yeah I had a similar problem in trying to determine the hourly rates of resource gathering

Most of us all know it as...

GoodTries
---------- * 100
AllTries

Where we think divide first then multiply by 100 to get a percentage

But EUO only supports interger division so the first part 9/10 is reduced to an integer of 0 = GINT(9/10)

So if you multiply by 100 first then divide, it's mo better

This snippet shows teh bad way and teh good way, plus maybe you want to show tenths? or maybe you want to round?

Code: [Select]
set !Numerator 9876
set !Denominator 13789

set !Result !Numerator / !Denominator * 100
display ok !Result

set !Result 100 * !Numerator / !Denominator
display ok !Result

;For tenths multiply by an extra power of 10 (from 100 to 1000)
set !Result 1000 * !Numerator / !Denominator
display ok !Result
;add an decimal near the end
str len !Result
str ins !Result #dot #StrRES
set !Result #StrRES
display ok !Result , %

str right !Result 1
if #STRRES >= 5
{
  ;round up
  set !Result 100 * !Numerator / !Denominator + 1
}
else
{
  ;round down (GINT)
  set !Result 100 * !Numerator / !Denominator
}
display ok Which Rounds to: , #spc , !Result

halt


Title: Re: Calculating a Percentage
Post by: Xclio on July 01, 2009, 01:48:07 PM
Rana, Thanks I didn't use your code exactly but I did use your math calculations to get what I needed ;)  Here's the code for those that might be interested.  I am adding to the %sucessattempts and the %failedattempts earlier in the script and in this portion I am just updating the menu.

Code: [Select]
sub UpdateAttempt
  menu set EUOEdit3 %sucessattempts
  menu set EUOEdit4 %failedattempts
  set %totalattempts ( %sucessattempts + %failedattempts )
  set %percent ( %sucessattempts / %totalattempts )
  set %sucesspercent ( %percent * 100 )
  menu set EUOedit5 %totalattempts
return
Title: Re: Calculating a Percentage
Post by: rana70 on July 02, 2009, 12:57:24 AM
Hi,

the important thing is you got your problem solved ....  ;)

cu
Title: Re: Calculating a Percentage
Post by: Xclio on July 02, 2009, 06:07:43 AM
Werd
Title: Re: Calculating a Percentage
Post by: Cerveza on July 02, 2009, 06:12:13 AM
For maximum accuracy... you should multiply your numbers by 100 before you divide them. Then when you divide them you'll get a more accurate percentage. It's because EUO has troubles with decimals.

If you look in my taming appraiser, there's a sub to get 2 decimal places when dividing.