> 
 > 
5. Blocks Coding with Raspberry Pi
Math
Math

This documentation covers fundamental math operations including addition, subtraction, multiplication, division, remainders, minimum and maximum values, square roots, rounding, absolute values, and generating random numbers.

Adding/Subtracting/Multiplying/Dividing Two Values

0 + 0

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>count</variable></variables><block type=\"math_arithmetic\" disabled=\"true\" x=\"67\" y=\"68\"><field name=\"OP\">ADD</field><value name=\"A\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value><value name=\"B\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}

This is used to return the sum/difference/product/quotient of two numbers.

Parameters: Condition:

  • Number (Default: 0)
  • Operator: + (Default)/-/×/÷/**

Output: Number
Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>count</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">count</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">3</field></shadow></value><next><block type=\"basic_consoleLogNum\"><value name=\"num\"><block type=\"math_arithmetic\"><field name=\"OP\">ADD</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">count</field></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">2</field></shadow></value></block></value></block></next></block></statement></block></xml>"}

The output:

Add - Output

Setting a Number

0

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>count</variable></variables><block type=\"math_number\" disabled=\"true\" x=\"-22\" y=\"247\"><field name=\"NUM\">0</field></block></xml>"}

This block is used to set the value to number or decimal number.

Parameters: Condition: Number (Default: 0)

Output: Number
Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>count</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">count</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">6.1</field></shadow></value><next><block type=\"basic_consoleLogNum\"><value name=\"num\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value></block></next></block></statement></block></xml>"}

The output:

0 - Output

Showing Remainder Between Two Numbers

Reminder of 0 ÷ 1

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>count</variable></variables><block type=\"math_modulo\" disabled=\"true\" x=\"-22\" y=\"292\"><value name=\"DIVIDEND\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value><value name=\"DIVISOR\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">1</field></shadow></value></block></xml>"}

This block is used to show the remainder between two values.

Parameters: Condition: Number (Default: 0)

Output: Number
Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>count</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">count</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">3</field></shadow></value><next><block type=\"basic_consoleLogNum\"><value name=\"num\"><block type=\"math_modulo\"><value name=\"DIVIDEND\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">count</field></block></value><value name=\"DIVISOR\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value></block></value></block></next></block></statement></block></xml>"}

The output:

Remainder of - Output

Finding the Minimum Values

Min of 0 and 0

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>count</variable></variables><block type=\"math_op2\" disabled=\"true\" x=\"-22\" y=\"382\"><field name=\"op\">min</field><value name=\"x\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value><value name=\"y\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}

This block is used to take the smaller value between two numbers.

Parameters: Condition: Number (Default: 0)

Output: Number
Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>count</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">count</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">3</field></shadow></value><next><block type=\"basic_consoleLogNum\"><value name=\"num\"><block type=\"math_op2\"><field name=\"op\">min</field><value name=\"x\"><block type=\"variables_get\"><field name=\"VAR\">count</field></block></value><value name=\"y\"><shadow type=\"math_number\"><field name=\"NUM\">4</field></shadow></value></block></value></block></next></block></statement></block></xml>"}

The output:

Min of - Output

Finding the Maximum Values

Max of 0 and 0

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>count</variable></variables><block type=\"math_op2\" disabled=\"true\" x=\"-22\" y=\"427\"><field name=\"op\">max</field><value name=\"x\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value><value name=\"y\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}

This block is used to take the larger value between two numbers.

Parameters: Condition: Number (Default: 0)

Output: Number
Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>count</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">count</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">3</field></shadow></value><next><block type=\"basic_consoleLogNum\"><value name=\"num\"><block type=\"math_op2\"><field name=\"op\">max</field><value name=\"x\"><block type=\"variables_get\"><field name=\"VAR\">count</field></block></value><value name=\"y\"><shadow type=\"math_number\"><field name=\"NUM\">4</field></shadow></value></block></value></block></next></block></statement></block></xml>"}

The output:

Max of - Output

Taking the Square Root

Square Root

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>count</variable></variables><block type=\"math_op3\" disabled=\"true\" x=\"-22\" y=\"472\"><value name=\"x\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}

This block is used to take the square root of the value.

Parameters: Condition: 

  • Number (Default: 0)
  • Square root (Default)/sin/cos/tan/atan2/integer ÷/integer ×

Output: Number
Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"basic_consoleLogNum\"><value name=\"num\"><block type=\"math_op3\"><value name=\"x\"><shadow type=\"math_number\"><field name=\"NUM\">-10</field></shadow></value></block></value></block></statement></block></xml>"}

The output:

Absolute - Output

Converting the Number to an Approximate Value

Round

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"math_js_round\" disabled=\"true\" x=\"-22\" y=\"607\"><field name=\"OP\">round</field><value name=\"ARG0\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}

This block is used to replace the number with an approximate value. If the fractional part is more than one half, the number will increase.

Parameters: Condition: 

  • Number (Default: 0)
  • Square root (Default)/sin/cos/tan/atan2/integer ÷/integer ×

Output: Number
Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"basic_consoleLogNum\"><value name=\"num\"><block type=\"math_js_op\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" op-type=\"unary\"></mutation><field name=\"OP\">sqrt</field><value name=\"ARG0\"><shadow type=\"math_number\"><field name=\"NUM\">4</field></shadow></value></block></value></block></statement></block></xml>"}

The output:

Square Root - Output

Changing the Number to Absolute Value

Absolute of 0

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"math_op3\" disabled=\"true\" x=\"-22\" y=\"472\"><value name=\"x\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}

This block is used to make the value of the number to absolute value.

Parameters: Condition: Number (Default: 0)

Output: Number

Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"basic_consoleLogNum\"><value name=\"num\"><block type=\"math_op3\"><value name=\"x\"><shadow type=\"math_number\"><field name=\"NUM\">-10</field></shadow></value></block></value></block></statement></block></xml>"}

The output:

Absolute - Output

Choosing the Random Value

Pick Random 0 to 10

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"device_random\" disabled=\"true\" x=\"-22\" y=\"652\"><value name=\"min\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value><value name=\"limit\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">10</field></shadow></value></block></xml>"}

This block is used to choose the random number between the range.

Parameters: Condition: Number (Default: 0)

Output: Number

Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"basic_consoleLogNum\"><value name=\"num\"><block type=\"device_random\"><value name=\"min\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value><value name=\"limit\"><shadow type=\"math_number\"><field name=\"NUM\">10</field></shadow></value></block></value></block></statement></block></xml>"}

The output:

Random - Output

Table of Content