Small Basic > Curriculum > Online > Lesson 3.3: The Math Object
In this lesson, you will learn how to:
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.
This object includes the following operations and properties:
Let’s learn about some operations of the Math object by writing a simple program.
TextWindow.WriteLine("Enter the angle in degrees and get the sine of the angle:") number = TextWindow.Read() TextWindow.WriteLine("The sine of the angle is " + Math.Sin(number)) TextWindow.WriteLine("The arcsine of the angle is " + Math.ArcSin(Math.Sin(number))) TextWindow.WriteLine("The angle in degrees is " + Math.GetDegrees(Math.ArcSin(Math.Sin(number)))) TextWindow.WriteLine("Enter the angle in degrees and get the cosine of the angle:") number = TextWindow.Read() TextWindow.WriteLine("The cosine of the angle is " + Math.Cos(number))
In this example, you get the sine and the cosine of an angle that you specify by using the Sin and Cos operations of the Math object. You can also get the angle in radians from the sine value by using the ArcSin operation. Next, you can convert the angle from radians to degrees with the GetDegrees operation.
You can use the Sin operation to get the sine of the specified angle in radians.
You can use the ArcSin operation to get the angle in radians, given the sine value.
You can use the GetDegrees operation to convert the value of an angle from radians to degrees.
You can use the Cos operation to get the cosine of the specified angle in radians.
The value of pi is an important aspect of some mathematical calculations. You can retrieve the value of pi in your calculations by using the Pi property of the Math object.
Let’s use this property to calculate the area of the circle.
TextWindow.Write("Enter the radius of the circle:") Radius = TextWindow.Read() Area = Math.Pi * Math.Power(Radius, 2) TextWindow.WriteLine("Area of the Circle is " + Area)
In this example, you retrieve the value of pi by using the Pi property of the Math object. Then you use that value in the formula to get the area of the circle.
The Pi property of the Math object returns the value of pi, which is 3.14.
Abs is another useful operation that the Math object provides. Let’s check it out.
By using the Abs operation, you can get the absolute value of the given number. For example, if you subtract a number from a smaller number, the result will be a negative numeral.
TextWindow.WriteLine("Enter two numbers for subtraction: ") Number1 = TextWindow.Read() Number2 = TextWindow.Read() Substraction = Number1 - Number2 Textwindow.WriteLine("The answer is " + Math.Abs(substraction))
In this example, you subtract two numbers. Even if the first number is smaller than the second, the Abs operation returns a positive number.
You can use the Abs operation of the Math object to get the absolute value of a number. For example, if the given number is -50, the Abs operation will return the value as 50.
While you create your Small Basic program, how can you get the integer value of a decimal number? The Floor operation was created to give an integer value that is smaller than or equal to a decimal number that you specify.
Let’s see how you can use this operation in a program to calculate a student’s average grade.
TextWindow.Write("Enter the name of the student: ") Name = TextWindow.Read() TextWindow.WriteLine("Enter the student's marks in six subjects:") For I = 0 To 5 Subject[i] = TextWindow.Read() Total = Total + Subject[i] EndFor Percentage = Total / 6 TextWindow.WriteLine("Total Marks:" + Total) TextWindow.WriteLine("Percentage:" + Math.Floor(percentage))
In this example, you enter the grades that a student earned in six subjects. Then, you use the Floor operation to get the student’s average as an integer value.
When you perform complex calculations, you often need the logarithmic value (base 10) of a particular number.
The Math object in Small Basic offers the Log operation to get the log value of the specified number.
TextWindow.WriteLine("Enter number to get its log value: ") Number = TextWindow.Read() TextWindow.WriteLine("Log value of " + Number + " is: " + Math.Log(Number))
In this example, you use the Log operation to get the log value of 22.3.
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 maximum number that you specify.
Let’s use this operation in a program.
GraphicsWindow.BackgroundColor = "Black" GraphicsWindow.Width = 600 GraphicsWindow.Height = 500 For i = 0 To 800 GraphicsWindow.FontSize = Math.GetRandomNumber(30) x = Math.GetRandomNumber(GraphicsWindow.Width) y = Math.GetRandomNumber(GraphicsWindow.Height) GraphicsWindow.DrawText(x, y, "*") Program.Delay(10) EndFor
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 set the font size by using the GetRandomNumber operation. The font size will be between 1 and 30 because you have specified 30 as the parameter for the GetRandomNumber operation.
You also use this operation to randomly set the asterisks’ x-coordinates and y-coordinates.
This is the output you will see:
The Math object also provides the Min operation, which you can use to compare two numbers and identify the smaller number of the two.
Let’s apply this operation in a program.
TextWindow.WriteLine("Enter the first number:") Number1 = TextWindow.Read() TextWindow.WriteLine("Enter the second number:") Number2 = TextWindow.Read() min = Math.Min(Number1, Number2) If (Number1 = Number2) Then TextWindow.WriteLine("These numbers are the same") Else TextWindow.WriteLine("The smaller number is:" + min) EndIf
In this example, you request two numbers from the user, use the Min operation to compare them, and display the smaller number in the text window. You also ensure that, if the user specifies the same number twice, the statement “Both numbers are the same” appears.
By using the SquareRoot operation of the Math object, you can get the square root of a number that you specify.
TextWindow.Write("Enter a number to get its square root: ") Number = TextWindow.Read() TextWindow.WriteLine("Square root of the entered number is " + Math.SquareRoot(Number))
In this example, you specify a number and use the SquareRoot operation to get its square root.
You can get the remainder in a division problem by using the Remainder operation of the Math object.
start: TextWindow.Write("Enter a number to check if it is even or odd: ") number = TextWindow.Read() If Math.Remainder(number, 2) = 0 Then TextWindow.WriteLine(number + " is an even number.") Else TextWindow.WriteLine(number + " is an odd number.") EndIf Goto start
In this program, you want to verify whether a specified number is even or odd. You use the If condition to verify whether the number is even (that is, whether the remainder is 0 when you divide the number by 2). If the remainder is 1, the number is odd. To check the remainder, you use the Remainder operation of the Math object.
Congratulations! Now you know how to:
By 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 its area, and randomize the x-coordinates and y-coordinates of the circle.
To see the answers to these questions, go to the Answer Key page.
Next Lesson