Partager via


Small Basic #8: Math

Microsoft Small Basic supports several mathematical functions, including not only simple addition, subtraction, multiplication, and division, but also comparisons, random numbers, rounding, exponents, logarithms, Pi, arcs, and trigonometry, as demonstrated in the following code:

 TextWindow.WriteLine("The larger of the numbers 10 and 15 is: " + Math.Max(15, 10))
TextWindow.WriteLine("The smaller of the numbers 10 and 15 is: " + Math.Min(15, 10))
TextWindow.WriteLine("A random number between 1 and 10 is: " + Math.GetRandomNumber(10))
TextWindow.WriteLine("The absolute value of -500 is: " + Math.Abs(-500))
TextWindow.WriteLine("The ceiling of 4.02 is: " + Math.Ceiling(4.02))
TextWindow.WriteLine("The floor of 4.02 is: " + Math.Floor(4.02))
TextWindow.WriteLine("The value of Pi is: " + Math.Pi)
TextWindow.WriteLine("5 raised to the power of 3 is: " + Math.Power(5, 3))
TextWindow.WriteLine("The square root of 25 is: " + Math.SquareRoot(25))
TextWindow.WriteLine("Dividing 10 by 4 leaves a remainder of: " + Math.Remainder(10, 4))
TextWindow.WriteLine("Rounding 4.02 results in: " + Math.Round(4.02))
TextWindow.WriteLine("The number of times 10 must be raised to get to 1000 is: " + Math.Log(1000))
TextWindow.WriteLine("The number of times ~2.718 must be raised to get to 1000 is: " + Math.NaturalLog(1000))
TextWindow.WriteLine("-----")

' An angle of 1 radian results in an arc whose length is equal to the circle's radius.
' A 180-degree angle results in an arc length equal to Pi.
TextWindow.WriteLine("1 radian = " + Math.GetDegrees(1) + " degrees (180/Pi).")
TextWindow.WriteLine("90 degrees = " + Math.GetRadians(90) + " radians (Pi/2).")
TextWindow.WriteLine("180 degrees = " + Math.GetRadians(180) + " radians (Pi).")
TextWindow.WriteLine("Let's check our work...")
TextWindow.WriteLine("180/Pi = " + 180/Math.Pi + " degrees.")
TextWindow.WriteLine("Pi/2 = " + Math.Pi/2 + " radians.")
TextWindow.WriteLine("Pi = " + Math.Pi + " radians.")
TextWindow.WriteLine("-----")

'  Given a right (90-degree) triangle with each of the other angles being 45 degrees, and the legs having lengths of 1, 1, and ~1.414: 
TextWindow.WriteLine("The sine of a 45-degree angle is: " + Math.Sin(Math.GetRadians(45)) + ", or 1/~1.414.")
TextWindow.WriteLine("The cosine of a 45-degree angle is: " + Math.Cos(Math.GetRadians(45)) + ", or 1/~1.414.")
TextWindow.WriteLine("The tangent of a 45-degree angle is: " + Math.Tan(Math.GetRadians(45)) + ", or 1/1.")
TextWindow.WriteLine("Let's check our work...")
TextWindow.WriteLine("1/" + Math.SquareRoot(2) + " = " + 1/Math.SquareRoot(2))
TextWindow.WriteLine("-----")

'  Given a right (90-degree) triangle with the other angles being 63.5 degrees and 26.5 degrees, and the legs having lengths of 1, 2, and ~2.24: 
TextWindow.WriteLine("The sine of a 63.5-degree angle is: " + Math.Sin(Math.GetRadians(63.5)) + ", or 2/~2.24.")
TextWindow.WriteLine("The cosine of a 63.5-degree angle is: " + Math.Cos(Math.GetRadians(63.5)) + ", or 1/~2.24.")
TextWindow.WriteLine("The tangent of a 63.5-degree angle is: " + Math.Tan(Math.GetRadians(63.5)) + ", or 2/1.")
TextWindow.WriteLine("Let's check our work...")
TextWindow.WriteLine("2/" + Math.SquareRoot(5) + " = " + 2/Math.SquareRoot(5))
TextWindow.WriteLine("1/" + Math.SquareRoot(5) + " = " + 1/Math.SquareRoot(5))
TextWindow.WriteLine("-----")