© Pradeeka Seneviratne  2019
Pradeeka SeneviratneBBC micro:bit Recipeshttps://doi.org/10.1007/978-1-4842-4913-0_8

8. Using Mathematical Functions

Pradeeka Seneviratne1 
(1)
Udumulla, Mulleriyawa, Sri Lanka
 

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 ).
    ../images/474604_1_En_8_Chapter/474604_1_En_8_Fig1_HTML.jpg
    Figure 8-1.

    Full code listing

  • After flashing the code, micro:bit will display the following result on the LED screen.

Addition: 10 Subtraction: 6 Multiplication: 16 Division: 4

How It Works

Mathematical operation blocks accept integers and floating-point numbers (includes positive and negative) as inputs. Figure 8-2 shows how to assign −0.8 and 2 to the variables x and y, respectively, for calculating the sum.
../images/474604_1_En_8_Chapter/474604_1_En_8_Fig2_HTML.jpg
Figure 8-2.

Assigning a floating-point number to a variable

The micro:bit will display following result on the LED screen.
1.2

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 ).
    ../images/474604_1_En_8_Chapter/474604_1_En_8_Fig3_HTML.jpg
    Figure 8-3.

    Full code listing

  • When you run the code, the micro:bit display will show the following output.

2

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.

If you want to find the smaller value from more than two numbers, you can use a nested min of blocks . Figure 8-4 shows the code to find the smaller value from the numbers 8, 2, 3, and 5. It has three nested min of blocks.
../images/474604_1_En_8_Chapter/474604_1_En_8_Fig4_HTML.jpg
Figure 8-4.

Finding the smaller value from more than two numbers

If you click on the drop-down box of the min block, you can choose the max option from the list. This will change the functionality of the block and can be used to find the larger value of two numbers. As an example, Figure 8-5 shows how to find the larger value from two numbers.
../images/474604_1_En_8_Chapter/474604_1_En_8_Fig5_HTML.jpg
Figure 8-5.

Finding the larger value from two numbers

The micro:bit display will show the following answer as the output.
8

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 ).
    ../images/474604_1_En_8_Chapter/474604_1_En_8_Fig6_HTML.jpg
    Figure 8-6.

    Full code listing

  • When you run the code, micro:bit will display the following as the output.

8

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.

Here are some more examples:
  • 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 ).
    ../images/474604_1_En_8_Chapter/474604_1_En_8_Fig7_HTML.jpg
    Figure 8-7.

    Full code listing

  • When you run the code, the micro:bit displays the following as the output.

2

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.

If you click on the drop-down list of the square root block, you can find a list of some useful mathematical functions. When you choose a function from the list, the name of the block will change and show the respective function name. They are the following:
  • 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 ).
    ../images/474604_1_En_8_Chapter/474604_1_En_8_Fig8_HTML.jpg
    Figure 8-8.

    Full code listing

  • When you run the code, the micro:bit displays the following as the output.

4

How It Works

The round block rounds any decimal number to the nearest whole number. Here are some examples:
  • 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

Let’s generate random numbers between 5 and 12 included.
  • 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 ).
    ../images/474604_1_En_8_Chapter/474604_1_En_8_Fig9_HTML.jpg
    Figure 8-9.

    Full code listing

When you run the code, the micro:bit display shows one of the following numbers as the output.
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15

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.

The example below shows how to build a code to generate random numbers between 2.5 and 4.7 included (Figure 8-10 ).
../images/474604_1_En_8_Chapter/474604_1_En_8_Fig10_HTML.jpg
Figure 8-10.

Generating random numbers between two numbers

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 ).
    ../images/474604_1_En_8_Chapter/474604_1_En_8_Fig11_HTML.jpg
    Figure 8-11.

    Full code listing

  • When you run the above code, the micro:bit display shows the output as 409.2 .

How It Works

The map block converts a value in one number range to a value in another number range. Following are the parameters of the map block.
  • 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.

As an example, you can scale a length in the range 0–10 cm to the range 0–100 cm. Table 8-1 shows some mapped lengths from a small range to a large range.
Table 8-1

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

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.50.87