Trigonometric Functions
Math.cos(x)returns the cosine of a numeric expression x.
Math.sin(x)returns the sine of a numeric expression x.
Math.tan(x)returns the tangent of a numeric expression x.
Math.acos(x)returns the arc cosine of a numeric expression x.
Math.asin(x)returns the arc sine of a numeric expression x.
Math.atan(x)returns the arctangent of a numeric expression x.
Math.atan2(x,y)Returns the angle of the polar coordinate corresponding to (x,y)
Rounding
Math.round(x)returns a supplied numeric expression x rounded to the nearest integer. If the decimal portion of the number is 0.5 or greater, the return value is equal to the smallest integer greater than the number. Otherwise, round returns the largest integer less than or equal to the number. This is exactly as conventional rounding. For example, if we have a floating point number between 0 and 2, values from 0 and up to but not including 0.5, will be rounded to 0. Values from and including 0.5 and up to but not including 1.5 will be rounded to 1. Values from and including 1.5 up to and including 2, will be rounded to 2.
Figure 1 - Rounding example 1
Math.ceil(x)returns the smallest integer greater than or equal to its numeric argument x, i.e. rounding up to the nearest integer. For example, if we have a floating-point number between 0 and 2, the value 0 will be rounded to 0, and all values greater than 0 and up to and including 1 will be rounded to 1. All values greater than 1 and up to and including 2 will be rounded to 2.
Figure 2 - Rounding example 2
Math.floor(x)returns the greatest integer less than or equal to its numeric argument x, i.e. rounding down to the nearest integer. For example, if we have a floating point number between 0 and 2, all values from 0 and up to, but not including 1, will be rounded to 0. The value 1 and all values from 1 and up to but not including 2, will be rounded to 1, and the value 2 will be rounded to 2.
Figure 3 - Rounding example 3
Rounding to a Number with Two Digits
If we ask people to provide their yearly salary and we want to compute the monthly salary, this can be done by dividing the yearly salary by 12. However this might result in a number with many decimals. If for example we want the result with an accuracy of two digits and need to round it, we can do that by multiplying the result by 100, then do the rounding, and then divide the result with 100. Let us say the yearly salary is in the question yearly and the monthly salary should be stored in the question monthly. Then we can use a script as below to set the monthly value:
var yearly : int = f("yearly").toNumber();
var monthly : float = yearly/12;
f("monthly").set(Math.round(monthly*100)/100);Random
Math.random()returns a random number between 0 and 1 (float), e.g. 0.523. The number generated is from 0 (inclusive) to 1 (exclusive), that is, the returned number can be zero, but it will always be less than one.
This is an extremely powerful feature to use in your surveys when you want to pick responses randomly, or send random respondents to different parts of the questionnaire. Combined with conditions and arithmetic operations you can program extremely powerful solutions for the selections (go to SetRandomCategories for more information).
Picking n Random Items from the Answers to a Multi Question
If you have a multi question where the respondent picks some brands and want to proceed with more detailed questions on some of the brands the respondent chooses, but not all, you can use a script to randomly pick some of the brands the respondent selected. Let's say the multi question has question ID brands, and we have a hidden multi question with the same answer list that has question ID random_brands.
var fromForm = f("brands");
var toForm = f("random_brands");
const numberOfItems : int = 3;
var available = new Set();
available = available.union(fromForm);
var selected = new Set();
if(available.size() <= numberOfItems)
{
selected = available;
}
else
{
while(selected.size() < numberOfItems)
{
var codes = available.members();
var randomNumber : float = Math.random()*codes.length;
var randomIndex : int = Math.floor(randomNumber);
var selectedCode = codes[randomIndex];
available.remove(selectedCode);
selected.add(selectedCode);
}
}
toForm.set(selected);In this script we pick responses from a set of available responses (the answers to the multi question brands, stored in the variable available) and add them to the set stored in the variable selected. At the end we set the selected answers in the random_brands multi.
If there number of responses is less than or equal to numberOfItems (here: 3), we obviously include all those responses. But if there are more than 3 responses, we have to pick random responses from the answers given.
The random picking is done in this while loop:
while(selected.size() < numberOfItems)
{
var codes = available.members();
var randomNumber : float = Math.random()*codes.length;
var randomIndex : int = Math.floor(randomNumber);
var selectedCode = codes[randomIndex];
available.remove(selectedCode);
selected.add(selectedCode);
}
We run through the loop until we have numberofItems responses (here 3) in the selected set. When a random response is selected, we remove its code from the available set and insert it into the selected set. So available will at any time hold the responses that have not been picked yet. At the start of each iteration inside the loop we build an array codes with the available codes. The members method converts a set to an array.
Let us say we start with the codes "1","3","4" and "8". The array codes will consist of the following items in the first iteration:
codes[0] = "1"
codes[1] = "3"
codes[2] = "4"
codes[3] = "8"
Math.random will give a number between 0 (inclusive) and 1 (exclusive). When this is multiplied with codes.length (4 in our example), randomNumber will be set to a number between 0 (inclusive) and codes.length (4) (exclusive):
0<=randomNumber<codes.length
i.e.
0<=randomNumber<4
in our example. Let us say that the number returned from Math.random is 0.6283. Then randomNumber will be 4*0.6283 = 2.5132.
This number is rounded down to the nearest integer by using the Math.floor method in the next step:
var randomIndex : int = Math.floor(randomNumber);This means that randomIndex will be one of 0,1,2,...,codes.length-1, i.e. 0,1,2,3.
ImportantMath.floor must be used, not Math.round or Math.ceil. Using Math.floor is the only way to ensure that the probability of selecting the indexes is the same for all of them and that you get no errors.
Math.round would extend the set of possible picks with the index codes.length (i.e. the number 4 in our example). This would cause problems because 4 is not an index in the array. If we used Math.ceil, we would round up to the numbers 1,2,3 and 4, and could subtract 1 from these numbers to get the index. However, even though the probability of this happening is extremely small, there is a small chance the number 0 would be returned from Math.random(). And using Math.ceil on 0 would yield 0, and this would again cause problems.
In our example, where randomNumber was calculated to 2.5132, randomIndex will get the value 2.
codes[2] is "4", so the code 4 will be removed from the available set and added to the selected set.
The while loop will now continue to the next iteration with the remaining three codes, so in our example codes will be have the following items in the next iteration:
codes[0] = "1"
codes[1] = "3"
codes[2] = "8"
Then the script will randomly pick one of these, and continue with this process until 3 items are selected.
Picking random items like this is best suited for surveys where the respondent is not allowed to modify previous answers. If the respondent goes back to the brands question and then forwards again, the script is run again, possibly resulting in a different set. So then the respondent will get questions for other brands. This may be confusing and cause irritation for the respondent.
Randomly Assigning which Part of a Survey the Respondents Should Answer
To limit the number of questions each respondent has to answer in a long survey, you may want to split the survey into different parts, and randomly pick which part a particular respondent should answer.
This can be done by randomly setting the response to a hidden single question, and then route the respondents to their questions with conditions on this hidden question.
Let us say the hidden single question has question ID part. part can be set at the beginning of the survey with a script like this:
var form = f("part");
if(!form.toBoolean())
{
var codes = form.domainValues();
var randomNumber : float = Math.random()*codes.length;
var randomIndex : int = Math.floor(randomNumber);
var code = codes[randomIndex];
form.set(code);
}This is very similar to the previous example. Here we pick the code from an array of all codes from the answer list of the hidden single question part (using domainValues).
The condition with toBoolean is used so that the single question is set only the first time the script is run. So this solution will work even when the respondent is allowed to modify previous answers.
See also the built-in function SetRandomCategories(go to SetRandomCategories for more information).
Maximum and Minimum
Math.max({number1{, number2{...{, numberN}}}})
Math.min({number1{, number2{...{, numberN}}}})max returns the greater of zero or more supplied numeric expressions. min returns the lesser of zero or more supplied numeric expressions. The curly brackets are used to indicate that the numerical expressions number1,...,numberN are optional.
If no arguments are provided, the return value is equal to negative infinity for max and positive infinity for min. If any argument is NaN, the return value is also NaN (Not a Number).
However, we would recommend using the Forsta Plus functions Max and Min(go to Max and Min for more information) when working on questions, since these functions automatically converts the answers to numbers.
Absolute Value
Math.abs(number)abs returns the absolute value of a numeric expression number.
abs can for example be used when you want the difference between two numbers as a positive number, no matter which of them is the highest number.
Math.abs(x-y)If x is 10 and y is 4, x-y will return 6. If x is 4 and y is 10, x-y will return –6. The absolute value will be 6 for both.
Exponents Logarithms and Square Root
Math.exp(number)
exp returns e (the base of natural logarithms) raised to a power, enumber.
e is Euler's constant, approximately equal to 2.178.
Math.log(number)log returns the natural logarithm of a number. The base is e, Euler's constant, approximately equal to 2.178.
Math.pow(base, exponent)pow returns the value of a base expression taken to a specified power, baseexponent.
Math.sqrt(number)Returns the square root of a numeric expression number. If number is negative, the return value is zero.