0% found this document useful (0 votes)
31 views

Mathematical Functions

The document discusses mathematical functions in Scratch including absolute value, floor, ceiling, square root, trigonometric, logarithmic, and exponential functions. It provides examples of how to use these functions to solve practical problems.

Uploaded by

Tchaick
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Mathematical Functions

The document discusses mathematical functions in Scratch including absolute value, floor, ceiling, square root, trigonometric, logarithmic, and exponential functions. It provides examples of how to use these functions to solve practical problems.

Uploaded by

Tchaick
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

M at he m at ic a l F unc t ions

Scratch supports a large number of mathematical


functions. This appendix provides a quick review
of these functions with some practical examples of
their uses.

Math Functions in Scratch


The sqrt of block from the Operators palette contains 14 math functions
that you can choose from its drop-down menu. Table 1 briefly describes
these functions.
Table 1: Scratch’s Mathematical Functions

Function Description
Returns the absolute value of x. For example, abs(5) = 5,
abs(0) = 0, and abs(–4) = 4. Geometrically, abs(x) is the
distance between x and 0 on the number line. Similarly,
abs(x – y) is the distance between x and y on the on the
number line.
Returns the largest integer that is less than or equal to x. For
example, floor(2.1) = 2, floor(2.9) = 2, floor(–2.1) = –3.

Returns the smallest integer that is greater than or equal to x.


For example, ceiling(2.1) = 3, ceiling(2.9) = 9, ceiling(–2.1) = –2.

Returns the square root of x. This is another number y such


2
that y = x. For example, sqrt(16) = 4, sqrt(2) = 1.4142, and
sqrt(0) = 0. Passing a negative value for x returns NaN (short
for “not a number”).
Returns the sine of x, where x is an angle expressed in degrees.
For example, sin(0) = 0, sin(30) = 0.5, and sin(90) = 1.

Returns the cosine of x, where x is an angle expressed in deg­


rees. For example, cos(0) = 0, cos(60) = 0.5, and cos(90) = 0.

Returns the tangent of x, where x is an angle expressed in


degrees. For example, tan(0) = 0, and tan(45) = 1.

Returns the inverse sine, or arcsine, of x. The arcsine of x is the


angle whose sine is x. For example, asin(0.5) = 30.

Returns the inverse cosine, or arccosine, of x. The arccosine of


x is the angle whose cosine is x. For example, acos(0.5) = 60.

Returns the inverse tangent, or arctan, of x. The arctan of x is


the angle whose tangent is x. For example, atan(1) = 45.

Returns the natural logarithm of x. For example, ln(2.718) ≈ 1.

Returns the base-10 logarithm of x. For example, log(1000) = 3.

Returns the exponential function of x. For example, e1 ≈ 2.718.

Returns 10 to the power of x. For example, 102 = 100.

In the following sections, we’ll talk about some of these functions in


more detail.

2   Mathematical Functions
Trigonometric Functions
The basic trigonometric functions of an acute angle (one smaller than 90°)
in terms of the sides of a right triangle are defined in Figure 1. These func-
tions are simply names given to ratios that can be formed by the three sides
of a right triangle.

opposite
sin(θ) =
hypotenuse
se
adjacent te nu
cos(θ) = po opposite
hy
hypotenuse
θ
opposite
tan(θ) =
adjacent adjacent

Figure 1: Definitions of the basic trigonometric ratios

As an example, let’s say that we want to find the length of the shadow
cast by a building 50 meters high when the sun is 20 degrees above the hori-
zon, as illustrated in Figure 2.

50 m

20°

x
Figure 2: We can use trigonometry to find the length of a cast shadow.

Using the above definitions, we can write:

50
tan ( 20 ) =
x

From this, we can isolate x:

50
x=
tan 20

Mathematical Functions   3
We can then calculate and display x using the following command:

The block returns 137.37 meters.


The inverse trigonometric functions, also known as the arc functions,
are simply the reverse of the trigonometric functions. The arcsine of x is the
angle whose sine is x. Similarly, the arccosine of x is the angle whose cosine
is x, and the arctan of x is the angle whose tangent is x.

Logarithmic Functions
The base-10 logarithm, log(x), is the inverse function of 10x . That is, if
log(x) = y, then x = 10y. An example of the use of the base-10 logarithm is
the decibel (or dB) scale, which measures the loudness of sound. If I is the
intensity of sound in watts per square meter, then the sound level in deci-
bels is given by the following:

(
Sound level = 10 log I × 1012 dB )
For example, if the sound intensity is 10 –10 watts per square meter, we
can find the sound level in dB using the following blocks:

Executing these blocks gives an answer of 20 dB.


Scratch also has a command to calculate the natural logarithm of a
number. The natural logarithm of a positive number x, denoted by ln(x),
is defined in terms of the area under the curve y = 1/x. The function is not
defined for x ≤ 0.
As a practical example, let’s consider the problem of finding the half-
life of a radioactive material, which is defined as the time required for half
of the radioactive atoms in a sample to decay. The formula for calculating
half-life is

ln ( 2 )
Half-life = ,
k

where k is a constant that depends of the radioactive material. Polonium-210,


for example, has the constant k = 0.005. Using Scratch, we can find the half-
life of polonium-210 as follows:

This command gives an answer of about 139 days.

4   Mathematical Functions
The number e is the value that satisfies ln(e) = 1, and the exponential
function, exp(x) = ex , is the inverse of ln(x). That is, if ln(x) = y, then x = ey.
This function appears in many scientific formulas such as those modeling
population growth, compound interest, radioactive decay, heat transfer,
and so on.
As an example, let’s assume that the number of people in a city with an
initial population of 1,000 is expected to grow according to this formula:

Number after t years = 1, 000e 0.05t

To find the number of people after 10 years, we can re-create the for-
mula with Scratch commands and replace t with 10:

This gives an answer of about 1,649.


Note that Scratch does not have a built-in function for calculating xy.
However, you can create this function by noting that

x y = e y ln x , x > 0.

For example, if you want to find 64, you can use the following block:

This block gives the answer of 1,296.

Mathematical Functions   5

You might also like