Skip to content

modulus

MicroBlaster edited this page Jul 9, 2020 · 3 revisions

Purpose: Sets var to the remainder after dividing var by value.

Syntax: modulus var {value}

var: The variable that will devide by the value.

{value}: The amount the variable will be devided by (must be a number <> 0).

Notes: This command is a typical way to perform basic mathematics within TWX script.

Since mathematical and logical operators were introduced in v2.00, it is also possible to add to a variable using the following method:

setVar $value ($value % 1)

Example:

# Calculate seconds and ms from a counter
$ElapsedTime := 12345

# Old style - before operators were supported
setVar $SS $ElapsedTime 
divide $SS 1000
setVar $MS $ElapsedTime 
modulus $MS 1000
echo "Esapsed tiem is " $SS " seconds and " $MS " MS.*"

# You can use operators to make this look much cleaner and easier to read.
$SS := $ElapsedTime / 1000
$MS := $ElapsedTime % 1000
echo "Esapsed tiem is " $SS " seconds and " $MS " MS.*"

# Either method has the same results and echoes:
# Esapsed tiem is 12 Seconds and 345 MS.

Clone this wiki locally