No edit summary |
No edit summary |
||
Line 68: | Line 68: | ||
You can use it with variables instead of numbers, as following: |
You can use it with variables instead of numbers, as following: |
||
− | *set var round |
+ | *set var round(var1) |
where "var1" is equal to 20.5; "var" will be equal to 21. |
where "var1" is equal to 20.5; "var" will be equal to 21. |
||
+ | |||
+ | Remember, never make any spaces between "round" and the "(var1)" brackets. |
||
Revision as of 14:06, 27 August 2012
There are several arithmetic operators you can use as a ChoiceScript programmer. For each example below, I'm going to use a variable named "var" with a value of 50. The basic ones:
Addition:
*set var + 5
Var is now equal to 55.
Substraction:
*set var - 5
Var is now equal to 45.
Multiplication:
*set var * 5
Var is now equal to 250.
Division:
*set var / 5
Var is now equal to 10.
Then the more advanced ones:
Fairmath:
This operator is a little different than the others; it can be used on variables that are percentages only (from 0 to 100). Fairmath is of two types, "%+" and "%-".
They are used this way:
*set var %+ 20
Var is now equal to 60.
*set var %- 20
Var is now equal to 40.
The idea of fairmath is that the closer a variable is to 100, the harder it is to increase, and the closer a variable is to 0, the harder it is to decrease:
Fair Addition: (x %+ y) = (x + (100-x)*(y/100))
- High variables are harder to increase: (90 %+ 20) = (92 + 2) = 92
- Low variables are easier to increase: (10 %+ 20) = (10 + 18) = 28
Fair Substraction: (x %- y) = (x - x*(y/100))
- High variables are easier to decrease: (90 %- 20) = 90 - 18) = 72
- Low variables are harder to decrese: (10 %- 20) = (10 - 2 = 8
50 is equally easy to increase or decrease, as you've seen above.
Modulo:
This operator is indeed a little weird, but it is useful in two main ways; you can check if a number is evenly divisible to another by writing:
X % Y = 0
so that if we do:
var % 10 = 0
it is true, because 50 / 10 = 5, but if we do:
var % 40 = 0
it is false, because 50 / 40 = 1.25.
You can also get the fractional part of a number, so that if we do this:
*set var / 40
the result will be 1.25; then if we do:
var % 1
the result will be 0.25.
Round()
The *round() is used for setting a variable to the nearest integer, as following:
*set var round(30.5)
this will automatically set "var" to 31.
You can use it with variables instead of numbers, as following:
*set var round(var1)
where "var1" is equal to 20.5; "var" will be equal to 21.
Remember, never make any spaces between "round" and the "(var1)" brackets.
Related Articles: