Math
Your "Magic Calculator"
Calculating, Converting, and Transforming Numbers
What it is
The Math category contains blocks for working with numbers and mathematical operations.
These blocks allow the program to:
- Create numerical values.
- Add, subtract, multiply, and divide.
- Calculate remainders and whole-number division.
- Calculate powers.
- Perform bitwise operations.
- Modify the value of a variable.
- Use mathematical constants.
- Generate random numbers.
- Perform mathematical functions.
- Convert between number systems.
- Round values.
- Find minimum and maximum values.
- Constrain a value to a range.
- Map a value from one range to another.
What it does
Math blocks transform numerical information.
For example, a raw sensor value could be converted into a more useful range:
Sensor → Math → New Value → Serial Port → PC
Or a calculation could determine how an actuator behaves:
Sensor → Math → Logic → Control → Actuator
Think of Math as the numerical toolbox of your program. It takes numbers, calculates with them, converts them, or transforms them into more useful values.
Fun Guide to Math Blocks (Clu-blocks Pro)
What it is: Blocks for doing math and calculations.
What it does: Performs operations with numbers.
Example: Addition, subtraction, multiplication, division, dividing time, calculating distances.
It's the robot's internal calculator
Important! Math blocks help your robot think with numbers from simple addition to random values and even converting between number systems. They’re like your robot’s built-in calculator and logic engine!
Using the confirmed Clu-Blocks Pro generation, I recommend organizing this relatively large category into these families:
NUMBERS → CALCULATE → UPDATE → CONSTANTS → RANDOM → CONVERT → ROUND → LIMIT → MAP
| Block | What it does | Dropdown options | What the options mean |
|---|---|---|---|
| A number block | — | You can type any number here to use in your code | |
| Arithmetic Operations | +, −, ×, ÷, %, //, ** | - +: add- −: subtract- ×: multiply- ÷: divide- %: remainder- //: whole number division- **: power | |
| Arithmetic Operations | &, |, <<, >> | Compares two numbers bit by bit. A bit is 1 only when both bits are 1. | |
| Updating a Variable with Math | +=, -=, *=, /=, %=, **=, //= | - a += 1 Add to the current value.- a -= 1 Subtract from the current value.- a *= 1 Multiply the current value.- a /= 1 Divide the current value.- a %= 1 Store the resulting remainder.- a **= 1 Update using a power operation.- a //= 1 Update using floor division. | |
| Inserts a math constant | π, e | - π = 3.14159… (used in circles)- e = 2.718… (used in growth and logs) | |
| Generates a random number | int, float | - int: whole number- float: decimal number | |
| Applies a math function to a number | sin, cos, tan, asin, acos, atan, ln, log10, e^, 10^ | These are trig and log functions great for advanced math and angles | |
| Converts a value to another type | toInt, toFloat, transfer byte, bytetolnt | - toInt: makes it a whole number- toFloat: makes it a decimal- Others are for byte conversions | |
| Converts a number to another format | binary, octal, hex | Shows the number in binary (0s and 1s), octal, or hexadecimal | |
| Rounds a number | Round, Ceil, Floor, abs, sqrt | - Round: nearest whole- Ceil: round up- Floor: round down- abs: absolute value- sqrt: square root | |
| Finds the biggest or smallest number | max, min | - max: biggest- min: smallest | |
| Rounds a number to a specified number of decimal places. | —- | First value is the number; second value is the number of decimal places. Example: round(4.876, 2) returns 4.88. | |
| Sets the seed for random numbers | — | Makes random numbers more predictable (used in games or testing) | |
| Converts a number between bases | two, eight, Ten, 16 | - two: binary- eight: octal- Ten: decimal- 16: hexadecimal | |
| Restricts a number to a minimum and maximum limit. | — | max(1000, 1) ensures the value is at least 1; min(..., 100) ensures it does not exceed 100. | |
| Maps a number from one range to another | — | Useful for scaling values (e.g. sensor readings to screen size) |
Quick Tips for Students
- Use basic math blocks for scores, timing, brightness, and movement.
- Use conversion blocks when working with sensors, bytes, or number systems.
- Use random blocks for games, surprises, or unpredictable behavior.
- Use mapping when you want to turn one range of values into another (like turning 0–1023 into 0–100%).
1. Numerical Value
The basic Math value block provides a number that can be connected to another compatible block.
0Numbers can be used almost everywhere in a program:
- Number → Math
- Number → Logic
- Number → Variable
- Number → Sensor Threshold
- Number → Servo / PWM / DAC
Numbers are the basic data used by the remaining Math blocks.
2. Arithmetic Operations
The actual generator confirms seven arithmetic operations.
| Operation | MicroPython | Main Idea |
|---|---|---|
| Addition | 1 + 1 | Add two values. |
| Subtraction | 1 - 1 | Subtract one value from another. |
| Multiplication | 1 * 1 | Multiply values. |
| Division | 1 / 1 | Divide one value by another. |
| Remainder | 1 % 1 | Obtain the remainder of a division. |
| Floor Division | 1 // 1 | Perform floor division. |
| Power | 1 ** 1 | Raise one value to a power. |
3. Remainder %
1 % 1It returns the remainder after division.
7 % 2produces:1, because dividing 7 by 2 leaves a remainder of 1.
A useful robotics application later is determining whether numbers follow a repeating numerical pattern.
4. Floor Division //
1 // 1This is different from normal division: 1 / 1
The important distinction is:
/→ normal division//→ floor division
This is an intermediate concept and does not need to be introduced with the first four arithmetic operations.
5. Power **
1 ** 1This raises one value to the power of another.
Conceptually: Base → Power → Result
This is another operation I would introduce only when an exercise requires it.
6. Bitwise Operations
The actual Clu-Blocks Pro generation confirms four bit-level mathematical operations:
| Operation | MicroPython |
|---|---|
| Bitwise AND | 0 & 0 |
| Bitwise OR | 0 \| 0 |
| Left Shift | 0 << 0 |
| Right Shift | 0 >> 0 |
These operate on the binary representation of integer values.
Advanced Concept: Bitwise operations should be introduced after students understand ordinary arithmetic and binary numbers.
An important distinction is:
- Logic AND →
and - Bitwise AND →
& - Logic OR →
or - Bitwise OR →
|
They are not the same operations.
7. Updating a Variable with Math
The category can generate several compound-assignment operations.
| Operation | Generated MicroPython | Meaning |
|---|---|---|
| Add to | a += 1 | Add to the current value. |
| Subtract from | a -= 1 | Subtract from the current value. |
| Multiply by | a *= 1 | Multiply the current value. |
| Divide by | a /= 1 | Divide the current value. |
| Remainder assignment | a %= 1 | Store the resulting remainder. |
| Power assignment | a **= 1 | Update using a power operation. |
| Floor-division assignment | a //= 1 | Update using floor division. |
a += 18. Mathematical Constants
math.pi
math.eThese allow programs to use the constants directly without manually typing approximate decimal values.
9. Random Integer
random.randint(1, 100)This provides a random integer using the supplied limits.
The visible conceptual structure is: Random Integer → From → To
This is useful in activities involving unpredictable choices, simulations, games, or random behaviors.
10. Random Decimal Value
The corresponding random floating-point operation generates:
random.uniform(1, 100)This is different from randint():
11. Sine
math.sin(90)This performs the sine operation available through the Math block.
sin, cos, tan, asin, acos, atan, ln, log10, e^, 10^
Since the supplied generator reference confirms sin() but does not list other trigonometric functions, I would document sine only here rather than assuming additional hidden options.
12. Convert to Integer
int(x)This applies integer conversion to the supplied value.
This becomes useful when a program needs numerical information in integer form.
13. Binary, Octal, and Hexadecimal Representations
The Math category provides number-system conversion operations.
Binary
bin(15)
Octal
oct(15)
Hexadecimal
hex(15)These allow the same numerical value to be represented using different number systems.
| System | Common Prefix |
|---|---|
| Binary | 0b |
| Octal | 0o |
| Hexadecimal | 0x |
Students have already encountered hexadecimal values in the Infrared (IR) category, such as: 0x45
This gives the Math category a natural connection with the IR exercises.
14. Rounding
round(0.998)This rounds the supplied value.
There is also a more configurable rounding operation:
Python
round(4.8, 2)
The second numerical value specifies the rounding precision supplied to Python's round() operation.
The important student concept is:
Number → Round → Rounded Number
15. Maximum
The category can generate:
Python
max(1, 2)
This returns the maximum of the supplied values.
Conceptually:
Value A + Value B → MAX → Larger Value
16. Minimum
The corresponding minimum operation generates:
Python
min(1, 2)
Conceptually:
Value A + Value B → MIN → Smaller Value
So:
max → largest
min → smallest
17. Random Seed
The actual generation includes:
Python
random.seed(time.ticks_ms())
This initializes the random-number generator using the current system tick value supplied by:
Python
time.ticks_ms()
For your beginner students, I would classify this as an advanced Random operation.
They do not need to understand random seeding before using:
Python
random.randint(...)
or:
Python
random.uniform(...)
18. Converting Between Number Bases
[ INSERT BLOCK IMAGE HERE: Base Conversion ]
The generated code confirms conversions involving bases:
- 2
- 8
- 10
- 16
Examples supplied by Clu-Blocks Pro include:
Python
int(str(1010), 2)
Python
hex(int(str(1010), 8))
Python
oct(int(str(1010), 10))
Python
bin(int(str(1010), 16))
These operations provide the bridge between representations of numbers in different bases.
Number Bases
| Base | Name |
|---|---|
| 2 | Binary |
| 8 | Octal |
| 10 | Decimal |
| 16 | Hexadecimal |
This is a more advanced mathematical topic, but it has a practical connection to your course because students already use hexadecimal commands in IR communication.
19. Constrain a Value
[ INSERT BLOCK IMAGE HERE: Constrain ]
The actual generated operation is:
Python
min(max(1000, 1), 100)
This limits the supplied value according to the minimum and maximum boundaries represented by the block.
Conceptually:
Value → Minimum / Maximum Limits → Constrained Value
A useful student mental model is:
Constrain keeps a value inside a specified range.
If a value attempts to go outside the selected boundaries, the expression limits the resulting value to the permitted range.
20. Map a Value
[ INSERT BLOCK IMAGE HERE: Map ]
Clu-Blocks Pro imports:
Python
from mixpy import math_map
and the supplied example generates:
Python
math_map(50, 1, 100, 1, 1000)
This transforms a value from one numerical range into another numerical range.
The five values represent the structure:
VALUE
FROM RANGE
TO RANGE
Conceptually:
Original Range → MAP → New Range
For example:
Sensor Range → Map → Control Range
This is one of the most useful Math concepts for robotics because physical sensor readings and actuator-control values often use different numerical ranges.
Map vs. Constrain
These two operations are related, but they have different purposes.
Constrain
Keep a value between limits.
Map
Transform a value from one numerical range into another.
A useful memory rule is:
CONSTRAIN sets boundaries. MAP changes scale.
Math with Variables
Many Math operations become easier to understand when combined with Variables.
For example:
Python
counter += 1
Conceptually:
Read Variable → Calculate → Store Back
This is extremely useful for:
- Counters
- Event totals
- Increasing levels
- Decreasing values
- Repeated calculations
Math and Logic Are Different
Students should also distinguish Math operations from Logic operations.
Math
Produces or transforms numerical information:
5 + 3 → 8
Logic
Asks a question and normally produces:
TRUE / FALSE
For example:
8 > 5 → TRUE
A complete program can therefore use:
Sensor → Math → Logic → Control → Output
Quick Guide for Students
- Do I need basic calculation? Use: + , − , × , ÷
- Do I need a remainder? Use: %
- Do I need floor division? Use: //
- Do I need a power? Use:
** - Do I need to increase a variable? Use:
+= - Do I need a random whole number? Use: random integer
- Do I need a random decimal value? Use: random uniform
- Do I need the larger of two values? Use: max
- Do I need the smaller value? Use: min
- Do I need to round a number? Use: round
- Do I need hexadecimal representation? Use: hex
- Do I need binary representation? Use: bin
- Do I need to keep a value inside limits? Use: constrain
- Do I need to transform one range into another? Use: map
Quick Reference
| Family | Confirmed Operations | Main Idea |
|---|---|---|
| Arithmetic | +, -, *, / | Basic calculation. |
| Advanced Arithmetic | %, //, ** | Remainder, floor division, power. |
| Bitwise | &, \|, <, >> | Integer bit operations. |
| Variable Update | +=, -=, *=, /=, %=, **=, //= | Calculate and store back. |
| Constants | math.pi, math.e | Mathematical constants. |
| Random Integer | random.randint() | Random integer. |
| Random Decimal | random.uniform() | Random floating-point value. |
| Sine | math.sin() | Sine operation. |
| Integer Conversion | int() | Convert to integer. |
| Binary | bin() | Binary representation. |
| Octal | oct() | Octal representation. |
| Hexadecimal | hex() | Hexadecimal representation. |
| Round | round() | Round a number. |
| Maximum | max() | Find the maximum. |
| Minimum | min() | Find the minimum. |
| Random Seed | random.seed(time.ticks_ms()) | Initialize the random generator using system ticks. |
| Base Conversion | Bases 2, 8, 10, 16 | Convert numerical representations. |
| Constrain | min(max(...)) | Keep a value inside limits. |
| Map | math_map() | Transform one numerical range into another. |
