Microsoft® Small Basic The Math Object Estimated time to complete this lesson: 1 hour
The Math Object In this lesson, you will learn about: Using different operations of the Math object. Using different properties of the Math object.
The Math Object The Math object offers many mathematical functions that you can use in your programs. It includes various operations and properties, such as: Do complex mathematical calculations boggle your mind at times? Don’t worry!
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.
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.
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.
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.
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.
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.
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.”
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.
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.
Let’s Summarize… Congratulations! Now you know how to: Use different properties of the Math object. Use different operations of the Math object.
It’s Time to Apply Your Learning… 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. Using the GetRandomNumber operation, write a program to move and rotate a rectangle in a random manner.