Math#
Introduction#
The Math blocks in VEXcode AIR handle mathematical calculations. These blocks allow for performing arithmetic and evaluating conditions.
Below is a list of available blocks:
math operator — Performs addition, subtraction, multiplication, or division.
comparison operator — Compares two values using greater than, less than, or equal operators.
logical operator — Evaluates multiple conditions using and or or.
not operator — Inverts a Boolean value.
range operator — Checks if a number falls within a range.
pick random — Generates a random number within a specified range.
round number — Rounds a number to the nearest whole number.
math functions — Applies operations such as square root, trigonometry, logarithms, and exponentiation.
atan2 — Computes the inverse tangent of Y/X.
remainder — Returns the remainder of a division operation.
math operator#
The math operator block performs basic arithmetic on the values placed on either side. It returns a result based on the project’s print precision, which defaults to 0 decimal places but can be adjusted using the set print precision block for screen or Console.
([0] [math_plus v] [0])
Parameters |
Description |
---|---|
value 1 |
The first number used in the operation. |
operator |
The mathematical operator to use:
|
value 2 |
The second number used in the operation. |
Example
when started :: hat events
[Perform basic addition and show two decimal places.]
set print precision to [0.01 v] on console
print ([10] [math_plus v] [10.5]) on console ▶
comparison operator#
The comparison operator block compares two values using a selected operator and returns either True or False.
True — The comparison is correct (e.g., 5 > 3 returns True).
False — The comparison is incorrect (e.g., 2 ≥ 5 returns False).
<[0] [math_equal v] [0]>
Parameter |
Description |
---|---|
value 1 |
The first value to compare. |
operator |
The comparison to use:
|
value 2 |
The second value to compare. |
Example
when started :: hat events
[Fly to position (0, 300, 500).]
take off to [500] [mm v] ▶
wait (1) seconds
move [forward v]
wait until <([y v] position in [mm v]) [math_greater_than v] [300]>
wait (1) seconds
land ▶
logical operator#
The logical operator block checks multiple conditions and returns either True or False based on the selected operator.
True — The logic condition is met.
False — The logic condition is not met.
<<> [and v] <>>
Parameter |
Description |
---|---|
condition 1 |
The first Boolean condition to evaluate. |
operator |
The logical operator to use:
|
condition 2 |
The second Boolean condition to evaluate. |
Example
when started :: hat events
[Display when buttons 5 and 7 are pressed together.]
wait until <<controller button [5 v] pressed?> [and v] <controller button [7 v] pressed?>>
print [Both buttons pressed!] on console ▶
not operator#
The not operator block inverts the result of a Boolean block. If the input is True, it returns False, and if the input is False, it returns True.
True — The input condition would normally return False.
False — The input condition would normally return True.
<not <>>
Parameter |
Description |
---|---|
condition |
The Boolean condition to be inverted. |
Example
when started :: hat events
[Move with controller while button 5 is not pressed.]
take off to [500] [mm v] ▶
while <not <controller button [5 v] pressed?>>
move with controller
end
land ▶
range operator#
The range operator block checks if a value falls within a specified range. It returns either True or False.
True — If the value is within the range.
False — If the value is outside the range.
<[0] [math_less_than v] [0] [math_less_than v] [0]>
Parameter |
Description |
---|---|
first operator |
The comparison operator to use:
|
value |
The value to check. |
second operator |
The comparison operator to use:
|
Example
when started :: hat events
[Hover at approximately 1000mm then land.]
take off to [500] [mm v] ▶
climb [up v]
wait until <[1000] [math_less_than v] (altitude in [mm v]) [math_less_than v] [1100]>
hover
wait (3) seconds
land ▶
pick random#
The pick random block generates a random number within a specified range. The number returned will be an integer if both parameters are whole numbers or a decimal if either parameter contains a decimal.
pick random [1] to [10]
Parameters |
Description |
---|---|
min |
The lowest possible number that can be picked. |
max |
The highest possible number that can be picked. |
Examples
when started :: hat events
[Display a number between 1 and 10.]
print (pick random [1] to [10]) on console ▶
when started :: hat events
[Display a decimal number between 1 and 10.5.]
set print precision to [All Digits v] on console
print (pick random [1] to [10.5]) on console ▶
round number#
The round number block rounds a given number to the nearest whole number. It follows standard rounding rules:
If the decimal is 0.5 or greater, the number rounds up.
If the decimal is less than 0.5, the number rounds down.
(round [0] to [0] decimal places)
Parameter |
Description |
---|---|
number |
The number to round. |
decimal places |
The amount of decimals places to round to. |
Example
when started :: hat events
[Display the rounded result of 10 / 3.]
set print precision to [0.01 v] on Console
print (round ([10] [math_division v] [3]) to [1] decimal places) on Console ▶
math functions#
The math functions block applies a selected mathematical function to a given number and returns the result. It supports operations such as absolute value, rounding, square roots, trigonometric functions, logarithms, and exponentiation.
[abs v] of [0]
Parameter |
Description |
---|---|
function |
The mathematical operation to apply to the input value:
|
number |
The number to apply the function to. |
Example
when started :: hat events
[Display the square root of 16.]
print ([sqrt v] of [16]) on Console ▶
atan2#
The atan2 block calculates the principal value of the inverse tangent of Y/X. It returns the angle in degrees between the positive X-axis and the point (X, Y).
(atan2 of x:[4] y:[3])
Parameter |
Description |
---|---|
X |
The horizontal position of the point, measured from the origin (0, 0). |
Y |
The vertical position of the point, measured from the origin (0, 0). |
Example
when started :: hat events
[Calculate the angle from current position (4, 3).]
print (atan2 of x:[4] y:[3]) on Console ▶
remainder#
The remainder block calculates the remainder when dividing one number by another.
remainder of [0] / [0]
Parameter |
Description |
---|---|
dividend |
The number to be divided. |
divisor |
The number to divide by. |
Example
when started :: hat events
[Display the remainder of 10 / 3.]
print (remainder of [10] / [3]) on Console ▶