This chapter presents how to use the built-in mathematical functions to add, subtract, multiply, or divide numeric values; create pseudorandom numbers; find the absolute values of numbers; calculate the remainders; find max and min values; and convert ASCII characters to text.
8-1. Using Basic Mathematical Operations
Problem
You want to use basic mathematical operations such as addition, subtraction, multiplication, and quotient division with two numbers.
Solution
In the Toolbox, click on the Variables category and then click on the Make a Variable… button. In the New variable name modal box, type x and click on the Ok button.
Repeat the above step again to create another variable named y.
Again, click on the Variables category. Then click and drag the set variable to block over, and place it inside the on start block. Then choose the variable x from the drop-down list and type 8 in the text box.
Repeat the above step to place another set variable to block just below the set x to block, and choose the variable y from the drop-down list and type 2 in the text box.
In the Toolbox, click on the Basic category. Then click and drag the show number block over, and place it inside the on start block just below the set y to block.
In the Toolbox, click on the Math category. Then click and drag the addition block over, and place it inside the placeholder of the show number block.
In the Toolbox, click on the Variables category. Then click and drag the variable x over, and place it inside the first placeholder of the addition block.
Repeat the above step again to place the variable y in the second placeholder of the addition block.
Right-click on the show number block, and from the shortcut menu, click Duplicate. Then place it inside the on start block just below the first show number block. After that, choose subtraction from the drop-down list.
Repeat the above step to create and place blocks for multiplication and quotient division.
In the Toolbox, click on the Basic category. Then click and drag the show string block over, and place it just above the addition block. Type “Addition” in the text box of the show string block.
Repeat the above step to create another three show string blocks for “Subtraction,” “Multiplication,” and “Division.” Place them just above the respective mathematical operation blocks.
- Once completed, your code should look something like this (Figure 8-1 ).
After flashing the code, micro:bit will display the following result on the LED screen.
How It Works
8-2. Finding Smaller and Larger Values of Two Numbers
Problem
You want to find the smaller value of two numbers.
Solution
In the Toolbox, click on the Variables category and then click on the Make a Variable… button. In the New variable name window, type x and click on the Ok button.
Follow the above step again to create another variable named y.
Again, go to the Variables category. Then click and drag the set variable to block over, and place it inside the on start block. Then from the drop-down list, choose the variable x and in the text box, type the value 2.
Follow the above step again to place another set variable to block just below the set x to block. Then from the drop-down list, choose the variable y and in the text box, type the value 8.
In the Toolbox, click on the Basic category. Then click and drag the show number block over, and place it inside the on start block just below the set y to block.
In the Toolbox, click on the Math category. Then click and drag the min of block over, and place it inside the placeholder of the show number block.
In the Toolbox, click on the Variables category. Then click and drag the variable x block over, and place it inside the first placeholder of the min of block.
Follow the above step again to place the variable y in the second placeholder of the min of block.
- Once completed, your code should look something like this (Figure 8-3 ).
When you run the code, the micro:bit display will show the following output.
How It Works
The min of block allows you to find the smaller value of two numbers. With the min of block, you can use variables, or you can type the values in the placeholders as inputs.
8-3. Finding Absolute Value of a Number
Problem
You want to find the absolute value of a number.
Solution
In the Toolbox, click on the Basic category. Then click and drag the show number block over, and place it inside the on start block.
In the Toolbox, click on the Math category. Then click and drag the absolute of block over, and place it inside the placeholder of the show number block.
Type the value 8 in the text box of the absolute of block.
- Once completed, your code should look something like this (Figure 8-6 ).
When you run the code, micro:bit will display the following as the output.
How It Works
The absolute value tells only how far a number is from zero. As an example, “5” is 5 away from zero, and “−5” is also 5 away from zero. So, the absolute value of 5 is 5, and the absolute value of −5 is also 5.
The absolute value of −8 is 8.
The absolute value of 2 is 2.
The absolute value of 0 is 0.
The absolute value of −156 is 156.
The absolute value of 3.7 is 3.7.
The absolute value of −3.7 is 3.7.
The absolute of block provides the absolute value of any number you have entered in the text box. It also accepts any number as a variable or number returned by another block.
8-4. Finding Square Root of a Number
Problem
You want to find the square root of a number.
Solution
In the Toolbox, click on the Basic category. Then click and drag the show number block over. and place it inside the on start block.
In the Toolbox, click on the Math category. Then click and drag the square root block over . and place it inside the placeholder of the show number block.
Type the value 4 in the text box of the square root block.
- Once completed, your code should look something like this (Figure 8-7 ).
When you run the code, the micro:bit displays the following as the output.
How It Works
The square root block outputs the square root of any positive number. It provides NaN (Not a Number) error message for any negative number, because negative numbers don’t have a square root.
sin
cos
tan
atan2
integer ÷
integer x
8-5. Rounding a Number
Problem
You want to round a number.
Solution
In the Toolbox, click on the Basic category. Then click and drag the show number block over, and place it inside the on start block.
In the Toolbox, click on the Math category. Then click and drag the round block over, and place it inside the placeholder of the show number block.
Type the value 4.3 in the text box of the round block.
- Once completed, your code should look something like this (Figure 8-8 ).
When you run the code, the micro:bit displays the following as the output.
How It Works
Example: Round 2.4 to the nearest whole number.
Answer: 2
(2.4 gets rounded down)
Example: Round 2.7 to the nearest whole number.
Answer: 3
(2.7 gets rounded up)
Example: Round 2.5 to the nearest whole number.
Answer: 3
(2.5 gets rounded up)
Example: Round 2.48 to the nearest whole number.
Answer: 2
(2.48 gets rounded down)
Example: Round 2.59
Answer: 3
(2.59 gets rounded up)
8-6. Generating Random Numbers
Problem
You want to generate random numbers, including minimum and maximum numbers provided.
Solution
In the Toolbox, click on the Basic category. Then click and drag the show number block over, and place it inside the on start block.
In the Toolbox, click on the Math category. Then click and drag the pick random block over, and place it inside the placeholder of the show number block.
Type 5 in the first text box of the pick random block as the minimum number.
Type 12 in the second text box of the pick random block as the maximum number.
- Once completed, your code should look something like this (Figure 8-9 ).
How It Works
The pick random block outputs a random number between the minimum number and maximum number included. You can generate integers or decimal numbers, including negative and positive. Decimal numbers can be generated by providing at least one number for the minimum or maximum number.
Here are some output numbers generated by the above code. Note that some numbers have two decimal places, and some have one decimal place.
3.98, 2.64, 3.6, 3.91, 2.97, 4.44, 4.11.
8-7. Mapping a Number in One Range to Another Range
Problem
You want to map numbers in the range from 0–5 to another range from 0–1023.
Solution
In the Toolbox, click on the Basic category. Then click and drag the show number block over, and place it inside the on start block.
In the Toolbox, click on the Math category. Then click and drag the map block over, and place it inside the placeholder of the show number block.
Type 2 in the first text box (map) as the voltage you want to map.
Type 0 in the second text box (from low) as the minimum number of the range to convert from.
Type 5 in the third text box (from high) as the maximum number of the range to convert from.
Type 0 in the fourth text box (to low) as the minimum number of the range to convert to.
Type 1023 in the fifth text box (to high) as the maximum number of the range to convert to.
- Once completed, your code should look something like this (Figure 8-11 ).
When you run the above code, the micro:bit display shows the output as 409.2 .
How It Works
value: a number to convert from one range to another.
from low: the minimum number of the range to convert from.
from high: the maximum number of the range to convert from.
to low: the minimum number of the range to convert to.
to high: the maximum number of the range to convert to.
Mapping Numbers from One Range to Another Range
Length in range (0–10 cm) | Length in range (0–100 cm) after mapped |
---|---|
0 | 0 |
1 | 10 |
2 | 20 |
3 | 30 |
4 | 40 |
5 | 50 |
6 | 60 |
7 | 70 |
8 | 80 |
9 | 90 |
10 | 100 |