The Math block does a math calculation on its inputs, and outputs the result. You can do a simple math operation with one or two inputs, or enter a formula with up to four inputs.
Choose the Math Operation
Mode Selector Inputs Output
Choose the math operation you want to use by selecting a mode with the Mode Selector. After selecting the mode, you can choose values for the inputs. The inputs available will change depending on the mode.
Simple Math Operation Modes
The simple math operation modes calculate the Result with a single math operation using one or two Inputs. These modes are shown in the table below.
Mode
Inputs Used
Output Result
Add
A, B
A + B
Subtract
A, B
A – B
Multiply
A, B
A × B
Divide
A, B
A ÷ B
Absolute Value
A
A if A ≥ 0 -A if A < 0
The result is always ≥ 0.
Square Root
A
√A
Exponent
A (base), N (Exponent)
AN
Advanced
A, B, C, D
A + B – C* D
Tips and Tricks
If the input values for a math operation result in an illegal operation, such as division by zero or the square root of a negative number, the output result will be an error value. An error value may be interpreted as zero when used as the input for another programming block.
Example
This program subtracts 50 from the Reflected Light Intensity output of the Color Sensor block and uses the result as a Power input for a motor. This will make the motor turn backwards when the color sensor sees something dark and forwards when the sensor sees something light.
Advanced Mode
In the Advanced mode, the Math block can calculate a mathematical expression using up to four inputs and several math operations in one step.
Use Data Wires to connect up to four Numeric values to the A, B, C, and D inputs. Unneeded inputs can be left blank or 0.
Click the Block Text Field at the top of the block to enter the mathematical expression in text form to calculate. The expression can include the inputs by name as “A”, “B”, “C”, and “D”, numeric constants such as “50”, and math symbols such as “+”. You can also use functions from the list displayed and additional parenthesis to change the order of operations.
The result of the expression calculation is output in Result.
Example
In this program the Math block calculates a motor power using inputs from the Color Sensor and two Variables. The Reflected Light Intensity from the Color Sensor is wired to the A input, and the variables named “Gain” and “Power” are used for B and C. The expression “(A-50)*B+C” in the Math block subtracts 50 from the light intensity, multiplies the result by value of “Gain”, and then adds the value of “Power”.
Inputs and Outputs
The inputs of the Math block provide the values for the math calculation. You can enter the input values directly into the block. Alternatively, the input values can be supplied by Data Wires from the outputs of other Programming Blocks.
Inputs
Type
Notes
A
Numeric
First operand of a simple math operation
B
Numeric
Second operand of a simple math operation
A
Numeric
Base value in Exponent mode
N
Numeric
Exponent value in Exponent mode
C
Numeric
Input for Advanced mode
D
Numeric
Input for Advanced mode
The output of the Math block gives the result of the calculation. To use the output, use a Data Wire to connect it to another Programming Block.