14
Microsoft® Small Basic The Math Object Estimated time to complete this lesson: 1 hour

3.3 the math object

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: 3.3   the math object

Microsoft® Small Basic

The Math Object

Estimated time to complete this lesson: 1 hour

Page 2: 3.3   the math object

The Math Object

In this lesson, you will learn about:

Using different properties of the Math object.

Using different operations of the Math object.

Page 3: 3.3   the math object

The Math Object

Do complex mathematical calculations boggle your mind at times? Don’t worry!

The Math object offers many mathematical functions that you can use in your programs.

It includes various operations and properties, such as:

• Cos•

GetRandomNumber

• Sin• SquareRoot• Remainder• Pi

• Abs• ArcSin• Floor• GetDegrees• Log• Min

Page 4: 3.3   the math object

Operations of the Math Object

Let’s learn about some operations of the Math object by writing a simple program.

In this example, you use the Sin and the Cos operation of the Math object to get the sine and the cosine of the angle you entered. You can also use the ArcSin operation to get the angle in radians from the sine value. Next, you can use the GetDegrees operation to convert the angle from radians to degrees.

Page 5: 3.3   the math object

The Pi Property

The value of pi is an important aspect of some mathematical calculations. You can use the Pi property of the Math object to retrieve the value of pi in your calculations.

Let’s use this property to calculate the area of the circle.

OUTPUT

In this example, you use the Pi property of the Math object to retrieve the value of pi. Then you use this value in the formula to get the area of the circle.

Page 6: 3.3   the math object

The Abs Operation

Abs is another useful operation provided by the Math object. Let’s check it out.

With the help of the Abs operation, you can get the absolute value of the given number. For example, if you subtract a large number from a smaller number, it will result in a negative numeral.

In this example, you subtract two numbers. If the first number is smaller than the second, Abs operation returns a positive number.

Page 7: 3.3   the math object

The Floor Operation

While creating your Small Basic program, how can you get the integer value of a decimal number? That’s what the Floor operation is created for. It gives an integer value which is less than or equal to the specified decimal number.

Let’s see how you can use this operation in a program to calculate a student’s average grade.

In this example, you enter the grades received by a student in six subjects. Then, you use the Floor operation to get the student’s average as an integer value.

Page 8: 3.3   the math object

The Log Operation

While doing complex calculations, you often need the log value of a particular number.

The Math object in Small Basic offers the Log operation to get the logarithm (base 10) value of the specified number.

In this example, you use the Log operation to get the log of 22.3.

Page 9: 3.3   the math object

The GetRandomNumber Operation

Now, let’s discuss the GetRandomNumber operation of the Math object. You can use this operation to get a random number between 1 and the specified maximum number.

Let’s use this operation in a program.

In this program, you draw the ‘*’ shape on the graphics window in different sizes and at different locations. You first set the height, width, and background color of the graphics window. Then you use the GetRandomNumber operation to set the font size. The font size will be between 1 and 30 because you have specified 30 as the parameter for the GetRandomNumber operation. Next, you use this operation to set the x- and y-coordinates of the asterisks at random.

Page 10: 3.3   the math object

The Min Operation

The Math object provides another operation called the Min operation. You can use this operation to compare two numbers and get the smaller number of the two.

Let’s apply this operation in a program.

In this example, you accept two numbers and use the Min operation to compare the two numbers and display the smaller number in the text window. You also ensure that if the entered numbers are equal, the text window displays the statement “These numbers are the same.”

Page 11: 3.3   the math object

The SquareRoot Operation

Let’s check out the SquareRoot operation of the Math object. You can use this operation to get the square root of a specified number.

In this example, you enter a number and use the SquareRoot operation to get its square root.

Page 12: 3.3   the math object

The Remainder Operation

Let’s look at the Remainder operation of the Math object. You can use this operation to get the remainder in a division problem.

In this program, you want to check whether a number is even or odd.You use the If condition to check if the entered number is completely divisible by 2 (the remainder is 0). If so, the number is an even number; otherwise, it is an odd number. To check the remainder, you use the Remainder operation of the Math object.

Page 13: 3.3   the math object

Let’s Summarize…

Congratulations! Now you know how to:

Use different properties of the Math object.

Use different operations of the Math object.

Page 14: 3.3   the math object

It’s Time to Apply Your Learning…

Using the GetRandomNumber operation, write a program to move and rotate a rectangle in a random manner.

Write a program to draw circles of different sizes in the graphics window. Set the size of the circle by using the area of the circle and randomize the x- and y- coordinates of the circle.