數學
本文件涵蓋了基本的數學運算,包括加法、減法、乘法、除法、餘數、最小值和最大值、平方根、四捨五入、絕對值以及生成隨機數。
兩個值相加/相減/相乘/除以
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>"}
這用於返回兩個數位的總和/差值/乘積/商。
參數: 條件:
- 數字(預設值:0)
- 運算符:+(預設)/-/×/÷/**
輸出: 數位
示例代碼:
{"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>"}
輸出:

設置數位
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>"}
此塊用於將值設置為數位或十進位數。
參數: 條件:數字(預設值:0)
輸出: 數位
示例代碼:
{"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>"}
輸出:

顯示兩個數位之間的餘數
提醒 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>"}
此塊用於顯示兩個值之間的餘數。
參數: 條件:數字(預設值:0)
輸出: 數位
示例代碼:
{"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>"}
輸出:

查找最小值
0 和 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>"}
此塊用於取兩個數字之間的較小值。
參數: 條件:數字(預設值:0)
輸出: 數位
示例代碼:
{"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>"}
輸出:

查找最大值
最大值 0 和 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>"}
此塊用於在兩個數字之間取較大的值。
參數: 條件:數字(預設值:0)
輸出: 數位
示例代碼:
{"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>"}
輸出:

取平方根
平方根
{"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>"}
此塊用於取值的平方根。
參數: 條件:
- 數字(預設值:0)
- 平方根(預設)/sin/cos/tan/atan2/integer ÷/integer ×
輸出: 數位
示例代碼:
{"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>"}
輸出:

將數字轉換為近似值
圓
{"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>"}
此塊用於將數位替換為近似值。 如果小數部分超過二分之一,則該數位將增加。
參數: 條件:
- 數字(預設值:0)
- 平方根(預設)/sin/cos/tan/atan2/integer ÷/integer ×
輸出: 數位
示例代碼:
{"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>"}
輸出:

將數位更改為絕對值
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>"}
此塊用於將數位的值轉換為絕對值。
參數: 條件:數字(預設值:0)
輸出: 數
範例代碼:
{"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>"}
輸出:

選擇隨機值
隨機選擇 0 到 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>"}
此塊用於在範圍之間選擇隨機數。
參數: 條件:數字(預設值:0)
輸出: 數
範例代碼:
{"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>"}
輸出:

目録