 The place to discuss the ADRIFT Interactive Fiction toolkit

## How to calculate percentages in Action Set Variable?

This forum is the place to learn about and discuss ADRIFT 5. Feel free to mention any bugs you find here, but please also add these to the Bugs & Enhancements list.

### How to calculate percentages in Action Set Variable?

I am wanting to alter integer variables with percentages. Lets say you have variable A set to 60 and you want to reduce that amount by 20%,
which would be 60 x .2 = 12, the 60 - 12 giving the final result of 48 (right?) So how do i do this in Action? %A% * .2 doesn't work..Adrift doesn't like decimal points?

Would it be possible (once i know to how to do the calculation syntax wise properly) to do it in one line? Decrease A by %A% x .2
or do i break this up into two actions
Result = %A% x .2
and then A = %A% - Result or decrease A by Result. DazaKiwi

Posts: 743
Joined: Thu Oct 21, 2010 9:05 am
Location: New Zealand
Points: 61

### Re: How to calculate percentages in Action Set Variable?

Computers are very particular about how they store numbers. Adrift supports integers which are whole numbers only. http://wiki.adrift.co/Expressions#Integer_Expressions

To do decimal numbers you would need to use a different number type such as a float or a double, neither of which are currently supported by Adrift.

Alternatively, did you think about trying division instead of multiplying by decimal numbers? .2 or 20% is the same as 1/5th or dividing something by five.

With A equal to 60,
To reduce A by 20%, keeping 80%: Set A equal to (A / 5) * 4
To reduce A by 40%, keeping 60%: Set A equal to (A / 5) * 3
To reduce A by 60%, keeping 40%: Set A equal to (A / 5) * 2
To reduce A by 80%, keeping 20%: Set A equal to (A / 5) * 1

Keep in mind that Adrift will be rounding to the nearest whole integer when it does the math.
ElliotM

Posts: 1227
Joined: Tue Apr 17, 2007 8:04 pm
Location: The Midwest in the USA
Points: 13

### Re: How to calculate percentages in Action Set Variable?

Thanks ElliotM Math hasn't always been my strong suit. Adrift rounding down or up to nearest whole integer is good.

You have shown most examples of division with even numbers what about the odd numbers? if i was to reduce by 10,30,50,70 percent? what do I (A/12) * Y is there a formula to work out what number Y is?

Thanks This is most handy. DazaKiwi

Posts: 743
Joined: Thu Oct 21, 2010 9:05 am
Location: New Zealand
Points: 61

### Re: How to calculate percentages in Action Set Variable?

You're welcome, Daza. Just to be thorough, I'll add to my previous explanation by saying that because percentages require decimals, we have to think in fractions instead. To be more correct, I showed how to convert percentages to fractions based on fifths, not how to do it by even or odd numbers. If you are interested in percentages from fractions based on tenths, such as 10,20,30, etc., then just divide by 10 instead of five.

To reduce A by 10%, keeping 90%: Set A equal to (A / 10) * 9
To reduce A by 20%, keeping 80%: Set A equal to (A / 10) * 8
To reduce A by 30%, keeping 70%: Set A equal to (A / 10) * 7
To reduce A by 40%, keeping 60%: Set A equal to (A / 10) * 6
To reduce A by 50%, keeping 50%: Set A equal to (A / 10) * 5
To reduce A by 60%, keeping 40%: Set A equal to (A / 10) * 4
To reduce A by 70%, keeping 30%: Set A equal to (A / 10) * 3
To reduce A by 80%, keeping 20%: Set A equal to (A / 10) * 2
To reduce A by 90%, keeping 10%: Set A equal to (A / 10)

90% written as a fraction is 9/10, 80% is 8/10, and so forth. Hope that clears things up for you. ElliotM

Posts: 1227
Joined: Tue Apr 17, 2007 8:04 pm
Location: The Midwest in the USA
Points: 13

### Re: How to calculate percentages in Action Set Variable?

DazaKiwi wrote:if i was to reduce by 10,30,50,70 percent? what do I (A/12) * Y is there a formula to work out what number Y is?

To get 10%, 30%, 50%, or 70% you would use the corresponding formula above in my previous post. If you were to use twelfths or A/12, then you would be looking at 1/12 = 8.3%, 2/12 = 16.6%, etc.

To convert a percentage to a fraction put it over 100 and reduce. Or you could leave it as is and do the following:

To reduce A by 10%, keeping 90%: Set A equal to (A / 100) * 90
To reduce A by 20%, keeping 80%: Set A equal to (A / 100) * 80
To reduce A by 30%, keeping 70%: Set A equal to (A / 100) * 70
To reduce A by 40%, keeping 60%: Set A equal to (A / 100) * 60
To reduce A by 50%, keeping 50%: Set A equal to (A / 100) * 50
To reduce A by 60%, keeping 40%: Set A equal to (A / 100) * 40
To reduce A by 70%, keeping 30%: Set A equal to (A / 100) * 30
To reduce A by 80%, keeping 20%: Set A equal to (A / 100) * 20
To reduce A by 90%, keeping 10%: Set A equal to (A / 100) * 10

What all of these formulas do is they determine what 1/X is and then we multiply by the number of parts we wanted to keep. The fraction 9/10'ths is nine parts of one over ten total parts. We figure out what 1/10th is and then we multiple by 9 to keep 9/10ths or 90% of A. The math process I used to figure all of this out is based on multiplication with fractions.
ElliotM

Posts: 1227
Joined: Tue Apr 17, 2007 8:04 pm
Location: The Midwest in the USA
Points: 13

### Re: How to calculate percentages in Action Set Variable?

Thanks for the further info. Fractions were always a difficult thing for me to tackle when i was at school, so this info is invaluable  DazaKiwi

Posts: 743
Joined: Thu Oct 21, 2010 9:05 am
Location: New Zealand
Points: 61

### Re: How to calculate percentages in Action Set Variable?

That will be a lot more accurate if you do the multiplication first and then the division.
saabie

Posts: 1024
Joined: Fri Aug 12, 2011 2:07 am
Points: 50

### Re: How to calculate percentages in Action Set Variable?

That's a good point, you'll lose less accuracy by doing the division last rather doing it first and compounding the introduced inaccuracy by multiplying it.

Set A equal to (A * 90) / 100
Set A equal to (A * 80) / 100
etc.
ElliotM

Posts: 1227
Joined: Tue Apr 17, 2007 8:04 pm
Location: The Midwest in the USA
Points: 13

### Re: How to calculate percentages in Action Set Variable?

Reading the formulae, I also noticed that multiplying will make the figures more accurate. This is because if ADRIFT always rounds numbers to an integral value, it will round when dividing by 5 or 10. Ex:

if you do 70% of the number 22, ADRIFT would divide by 22/10 and round to 2, then multiply by 7 to get 14.
If you do 70% of 22 by multiplying first, you multiply 22 x 7 to get 154, then divide by 10 to round to a more accurate 15.

The exact number is 15 2/5, but 14 is nearly 1 1/2 off.

Posts: 161
Joined: Sat Sep 05, 2015 2:33 am
Points: 10

### Re: How to calculate percentages in Action Set Variable?

Apologies for the epic necroing of this thread but the issue I've run into in one of my works in progress is exactly the same one mentioned here, although I still can't figure out how to fix it.

Basically, I need to reduce a variable by a percentage but no matter how I word it, it doesn't work. What I have now is a task which sets the variable %y-health% to

(%y-health% / 100) * 50

but %y-health% (the health of the player) is always set to 0 afterwards. Which has the unfortunate effect of killing the player even if they've won the fight. Hardly ideal.

Do the spaces make a difference? I've tried it with and without them but the end result is always the same. Sometimes I get a bad expression error pop up but not always and I can't seem to reproduce it now.

Does anyone have any ideas how to get this working?
David Whyld

Posts: 7004
Joined: Sat Dec 18, 2004 5:15 pm
Location: United Kingdom
Points: 35

### Re: How to calculate percentages in Action Set Variable?

David Whyld wrote:Apologies for the epic necroing of this thread but the issue I've run into in one of my works in progress is exactly the same one mentioned here, although I still can't figure out how to fix it.

Basically, I need to reduce a variable by a percentage but no matter how I word it, it doesn't work. What I have now is a task which sets the variable %y-health% to

(%y-health% / 100) * 50

but %y-health% (the health of the player) is always set to 0 afterwards. Which has the unfortunate effect of killing the player even if they've won the fight. Hardly ideal.

Do the spaces make a difference? I've tried it with and without them but the end result is always the same. Sometimes I get a bad expression error pop up but not always and I can't seem to reproduce it now.

Does anyone have any ideas how to get this working?

You need to take into account that whenever ADRIFT makes a division, the result is rounded to the nearest integer. If y-health is below or equal to 100, the first part of the equation (%y-health%/100) can only become 0 or 1. In your example, the total result will, therefore, give 0 whenever %y-health% is below 50.

To fix this, you must force ADRIFT to do the multiplication before the division by using parentheses as shown below:
`(%y-health%*50)/100`
No need for spaces ----------------------------------------------------------------------
The Bash Saga:
1. The Dragon Diamond 2. The Way Home 3. Stone of Wisdom
---------------------------------------------------------------------- Denk

Posts: 465
Joined: Mon Feb 22, 2016 6:21 pm
Location: Hjørring, Denmark
Points: 251

### Re: How to calculate percentages in Action Set Variable?

Thanks muchly. That did the trick. At this part of the game, the player only has 12 health so the way I was doing it you'd always have less than 50 health, leading to the player dying every time.
David Whyld

Posts: 7004
Joined: Sat Dec 18, 2004 5:15 pm
Location: United Kingdom
Points: 35

### Re: How to calculate percentages in Action Set Variable?

David Whyld wrote:Thanks muchly. That did the trick. At this part of the game, the player only has 12 health so the way I was doing it you'd always have less than 50 health, leading to the player dying every time.

12 Health? Are you making a combat scenario? I thought you hated those… D-Day in progress 86Kb (Slowly drifting)
Just a Fairy Tale: 97kb P/o Prune

Posts: 4608
Joined: Mon Jun 24, 2002 8:18 am
Location: Denmark
Points: 93

### Re: How to calculate percentages in Action Set Variable?

It's something I'm messing around with. Like most of my ideas these days, it'll probably go nowhere but it was bugging me that I couldn't seem to get it to work.
David Whyld

Posts: 7004
Joined: Sat Dec 18, 2004 5:15 pm
Location: United Kingdom
Points: 35 