15. 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.
16. 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.
17. 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.
18. 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.
19. 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.
20. 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.”
21. 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.
22. 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.
23. Let’s Summarize… Congratulations! Now you know how to: Use different properties of the Math object. Use different operations of the Math object.
24. 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.
Notas do Editor
Slide Notes: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 sin 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.Code: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))
SlideNotes: The Pi property of the Math object returns the value of pi, which is 3.14.Code:TextWindow.Write("Enter radius of a circle:")Radius = TextWindow.Read()Area = Math.Pi * Math.Power(Radius, 2)TextWindow.WriteLine("Area of the Circle is " + Area)
Notes: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.Code:TextWindow.WriteLine("Enter two numbers for subtraction: ")Number1 = TextWindow.Read()Number2 = TextWindow.Read()Substraction = Number1 - Number2Textwindow.WriteLine("The answer is " + Math.Abs(substraction))
Code: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]EndForPercentage = Total / 6TextWindow.WriteLine("Total Marks:" + Total)TextWindow.WriteLine("Percentage:" + Math.Floor(percentage))
Code:TextWindow.WriteLine("Enter number to get its log value: ")Number = TextWindow.Read()TextWindow.WriteLine("Log value of " + Number + " is: " + Math.Log(Number))
Code:GraphicsWindow.BackgroundColor = "Black"GraphicsWindow.Width = 600GraphicsWindow.Height = 500For i = 0 To 800GraphicsWindow.FontSize = Math.GetRandomNumber(30) x = Math.GetRandomNumber(GraphicsWindow.Width) y = Math.GetRandomNumber(GraphicsWindow.Height)GraphicsWindow.DrawText(x, y, "*")Program.Delay(10)EndFor
Code: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) ThenTextWindow.WriteLine(“These numbers are the same")Else TextWindow.WriteLine("The smaller number is:" + min) EndIf
Code: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))
Code:start:TextWindow.Write("Enter a number to check if it is even or odd: ")number = TextWindow.Read()If Math.Remainder(number, 2) = 0 ThenTextWindow.WriteLine(number + " is an even number.")ElseTextWindow.WriteLine(number + " is an odd number.")EndIfGoto start
Solution 1:Rectangle = Shapes.AddRectangle(100, 100)Shapes.Move(Rectangle, 250, 150)For i = 0 To Math.GetRandomNumber(30) Program.Delay(500)Shapes.Rotate(Rectangle, i * 25) EndForSolution 2:start:GraphicsWindow.Width = 600GraphicsWindow.Height = 450TextWindow.Write("Enter a radius for the circle: ")Radius = TextWindow.Read()Area = Math.Pi * Radius * RadiusHeight = Area / 2Width = Area / 2x = Math.GetRandomNumber(GraphicsWindow.Width / 2)y = Math.GetRandomNumber(GraphicsWindow.Height / 2)GraphicsWindow.DrawEllipse(x, y, Height, Width)Goto start