Skip to content

Class Math

Kristian Virtanen edited this page Oct 14, 2024 · 6 revisions

Description:

Math class provides a collection of math functions and sych that allow users to perform a mathematical operations.

Example codes:

Methods:

  • Math.Pi
  • Returns the value of Pi.

  • Math.Abs(number)
  • Returns the absolute value of a number

  • Math.Ceiling(number)
  • Returns the smallest integer greater than or equal to the specified number.

  • Math.Floor(number)
  • Returns the largest integer less than or equal to the specified number.

  • Math.NaturalLog(number)
  • Returns the natural logarithm (base e) of a number.

  • Math.Log(number)
  • Returns the base-10 logarithm of a number.

  • Math.Cos(angle)
  • Returns the cosine of the specified angle (in radians).
  • Returns true if the operation succeeds, otherwise false.

  • Math.Sin(angle)
  • Returns the sine of the specified angle (in radians).

  • File.CreateDirectory(directoryPath)
  • Creates a new directory at the specified path.
  • Returns true if the operation succeeds, otherwise false.

  • Math.Tan(angle)
  • Returns the tangent of the specified angle (in radians).

  • Math.ArcSin(sinValue)
  • Returns the arcsine of the specified sine value, in radians.
  • Returns a string of file paths seperated by line change, or an empty string if an error occurs.

  • Math.ArcCos(cosValue)
  • Returns the arccosine of the specified cosine value, in radians.

  • Math.ArcTan(tanValue)
  • Returns the arctangent of the specified tangent value, in radians.

  • Math.GetDegrees(angle)
  • Converts the specified angle from radians to degrees.

  • Math.GetRadians(angle)
  • Converts the specified angle from degrees to radians.

  • Math.SquareRoot(number)
  • Returns the square root of the specified number.

  • Math.Power(baseNumber,exponent)
  • Returns a specified number raised to the specified power.

  • Math.Round(number)
  • Rounds the specified number to the nearest integer. IE: 1.2 = 1, 32.1 = 32.

  • Math.Max(number1,number2)
  • Returns the greater of two numbers.

  • Math.Min(number1,number2)
  • Returns the smaller of two numbers.

  • Math.Remainder(dividend,divisor)
  • Returns the remainder of division of two numbers.

  • Math.GetRandomNumber(maxNumber)
  • Generates a random number between 1 and the specified maximum number.

  • Math.DoubleToDecimal(number)
  • Converts a double-precision floating-point number to a decimal.

Top

Example code in SmallBasicOE:

' Testing Pi
TextWindow.WriteLine("Pi: " + Math.Pi)

' Testing absolute value
TextWindow.WriteLine("Absolute value of -10: " + Math.Abs(-10))

' Testing ceiling and floor
TextWindow.WriteLine("Ceiling of 2.3: " + Math.Ceiling(2.3))
TextWindow.WriteLine("Floor of 2.7: " + Math.Floor(2.7))

' Testing logarithms
TextWindow.WriteLine("Natural log of 10: " + Math.NaturalLog(10))
TextWindow.WriteLine("Log base 10 of 100: " + Math.Log(100))

' Testing trigonometric functions
TextWindow.WriteLine("Cosine of Pi/4: " + Math.Cos(Math.Pi / 4))
TextWindow.WriteLine("Sine of Pi/4: " + Math.Sin(Math.Pi / 4))
TextWindow.WriteLine("Tangent of Pi/4: " + Math.Tan(Math.Pi / 4))

' Testing arc functions
TextWindow.WriteLine("Arcsine of 0.5: " + Math.ArcSin(0.5))
TextWindow.WriteLine("Arccosine of 0.5: " + Math.ArcCos(0.5))
TextWindow.WriteLine("Arctangent of 1: " + Math.ArcTan(1))

' Testing degree and radian conversion
TextWindow.WriteLine("Convert Pi radians to degrees: " + Math.GetDegrees(Math.Pi))
TextWindow.WriteLine("Convert 180 degrees to radians: " + Math.GetRadians(180))

' Testing square root and power
TextWindow.WriteLine("Square root of 16: " + Math.SquareRoot(16))
TextWindow.WriteLine("2 raised to the power of 3: " + Math.Power(2, 3))

' Testing rounding
TextWindow.WriteLine("Round 4.6: " + Math.Round(4.6))

' Testing Max and Min
TextWindow.WriteLine("Max of 5 and 10: " + Math.Max(5, 10))
TextWindow.WriteLine("Min of 5 and 10: " + Math.Min(5, 10))

' Testing remainder
TextWindow.WriteLine("Remainder of 10 divided by 3: " + Math.Remainder(10, 3))

' Testing random number generation
TextWindow.WriteLine("Random number between 1 and 10: " + Math.GetRandomNumber(10))

' Testing double to decimal conversion
TextWindow.WriteLine("Double to decimal (2.567): " + Math.DoubleToDecimal(2.567))

Top

Example code in C#:

// Create an alias for the SmallBasicOpenEditionDll namespace
using SB = SmallBasicOpenEditionDll;

static class SB_Program
{
    static void Main()
    {
        // Testing Pi
        Console.WriteLine($"Pi: {SB.Math.Pi}");

        // Testing absolute value
        Console.WriteLine($"Absolute value of -10: {SB.Math.Abs(-10)}");

        // Testing ceiling and floor
        Console.WriteLine($"Ceiling of 2.3: {SB.Math.Ceiling(2.3)}");
        Console.WriteLine($"Floor of 2.7: {SB.Math.Floor(2.7)}");

        // Testing logarithms
        Console.WriteLine($"Natural log of 10: {SB.Math.NaturalLog(10)}");
        Console.WriteLine($"Log base 10 of 100: {SB.Math.Log(100)}");

        // Testing trigonometric functions
        Console.WriteLine($"Cosine of Pi/4: {SB.Math.Cos(SB.Math.Pi / 4)}");
        Console.WriteLine($"Sine of Pi/4: {SB.Math.Sin(SB.Math.Pi / 4)}");
        Console.WriteLine($"Tangent of Pi/4: {SB.Math.Tan(SB.Math.Pi / 4)}");

        // Testing arc functions
        Console.WriteLine($"Arcsine of 0.5: {SB.Math.ArcSin(0.5)}");
        Console.WriteLine($"Arccosine of 0.5: {SB.Math.ArcCos(0.5)}");
        Console.WriteLine($"Arctangent of 1: {SB.Math.ArcTan(1)}");

        // Testing degree and radian conversion
        Console.WriteLine($"Convert Pi radians to degrees: {SB.Math.GetDegrees(SB.Math.Pi)}");
        Console.WriteLine($"Convert 180 degrees to radians: {SB.Math.GetRadians(180)}");

        // Testing square root and power
        Console.WriteLine($"Square root of 16: {SB.Math.SquareRoot(16)}");
        Console.WriteLine($"2 raised to the power of 3: {SB.Math.Power(2, 3)}");

        // Testing rounding
        Console.WriteLine($"Round 4.6: {SB.Math.Round(4.6)}");

        // Testing Max and Min
        Console.WriteLine($"Max of 5 and 10: {SB.Math.Max(5, 10)}");
        Console.WriteLine($"Min of 5 and 10: {SB.Math.Min(5, 10)}");

        // Testing remainder
        Console.WriteLine($"Remainder of 10 divided by 3: {SB.Math.Remainder(10, 3)}");

        // Testing random number generation
        Console.WriteLine($"Random number between 1 and 10: {SB.Math.GetRandomNumber(10)}");

        // Testing double to decimal conversion
        Console.WriteLine($"Double to decimal (2.567): {SB.Math.DoubleToDecimal(2.567)}");

        Console.WriteLine("All math operations tested successfully!");
    }
}

Expected output:

Pi: 3,141592653589793
Absolute value of -10: 10
Ceiling of 2.3: 3
Floor of 2.7: 2
Natural log of 10: 2,302585092994046
Log base 10 of 100: 2
Cosine of Pi/4: 0,7071067811865476
Sine of Pi/4: 0,7071067811865476
Tangent of Pi/4: 0,9999999999999999
Arcsine of 0.5: 0,5235987755982989
Arccosine of 0.5: 1,0471975511965979
Arctangent of 1: 0,7853981633974483
Convert Pi radians to degrees: 180
Convert 180 degrees to radians: 3,141592653589793
Square root of 16: 4
2 raised to the power of 3: 8
Round 4.6: 5
Max of 5 and 10: 10
Min of 5 and 10: 5
Remainder of 10 divided by 3: 1
Random number between 1 and 10: 1
Double to decimal (2.567): 2,567
``

[Top](#description)

Clone this wiki locally