Working with Built-in Mathematical Functions

In addition to all of the mathematical operations that you can put together using the numbers code blocks previously discussed in this chapter, Scratch provides one additional multi purpose code block, as shown in Figure 8.8.

Figure 8.8. This code block can assist you in setting up extremely complex calculations.


This code block is designed to perform any of 12 different mathematical functions, which can be selected from the code block’s drop-down list. The functions that this code block can perform are outlined in the following list:

  • abs. Returns the absolute, non-negative value of a number.

  • sqrt. Returns the square root of a number.

  • sin. Returns a value representing the sine of an angle.

  • cos. Returns a value representing the cosine of an angle.

  • tan. Returns a value representing the tangent of an angle.

  • asin. Returns the arc sine for the specified numeric value.

  • acos. Returns the arc cosine for the specified numeric value.

  • atan. Returns the arc tangent for the specific numeric value.

  • ln. Returns the inverse of the natural exponent of a specified value (i.e., the opposite of e^).

  • log. Returns the natural log of a number.

  • e^. Returns the natural exponent of a specified value.

  • 10^. Returns the value of a number raised to the 10th power.

These code blocks can be real time savers when developing applications that require the use of any of the mathematical functions supported by the code block, saving you the trouble of implementing the underlying programming logic yourself to retrieve similar results. As a result, not only will you spend less time working on the development of your application, but the programming logic that you have to develop will be simplified and easier to maintain, since this code block can do most of the heavy lifting for you.

To specify which function you want to work with, all you have to do is select it from the code block’s drop-down list. For example, the following examples demonstrate the use of two different functions provided by this code block:

This example consists of two sets of code blocks. The first set of code blocks returns the absolute value of –4.4, which is 4.4, and assigns that value to a variable named Result. The second set of blocks returns the square root of 9, which is 3, and assigns that value to a variable named Result.

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

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