BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET Framework
.NET 1.1+

.NET Math Library

The .NET framework includes a class named "Math", which provides a number of standard mathematical functions, using static methods, and mathematical values, using simple constants. This article describes all of the Math class members.

Log10

Log10 calculates the base-10 logarithm for a double value. This is the power that ten must be raised to in order to obtain a specified value.

double log;
log = Math.Log10(0.01);     // log = -2.0
log = Math.Log10(1);        // log = 0.0
log = Math.Log10(100);      // log = 2.0
log = Math.Log10(1000000);  // log = 6.0
log = Math.Log10(4000000);  // log = 6.6020599913279625

Sqrt

Sqrt is the final method in this section. It is used to calculate the square root of a value and can be used with the double data type. The sample code below uses the square root operation and the Pythagorean theorem to calculate the length of the hypotenuse of a right-angled triangle, based upon the length of the two other sides.

static void Main()
{
    double hypotenuse;
    hypotenuse = GetHypotenuse(3, 4);   // hypotenuse = 5.0
    hypotenuse = GetHypotenuse(5, 12);  // hypotenuse = 13.0
    hypotenuse = GetHypotenuse(8, 15);  // hypotenuse = 17.0
    hypotenuse = GetHypotenuse(10, 10); // hypotenuse = 14.142135623730951
}

static double GetHypotenuse(double length1, int length2)
{
    return Math.Sqrt(length1 * length1 + length2 * length2);
}

Trigonometry

The trigonometric methods of the Math class are used to perform calculations based upon the relationships between the side lengths and angles of triangles. They are all based upon the use of the sine, cosine and tangent functions.

Sin

Sin calculates the sine of an angle. When the angle is present in a right-angled triangle, the sine is the ratio of the opposing side and the hypotenuse. You can use this ratio to calculate the angles of a right angled-triangle given enough side lengths or to calculate the side lengths given the length of one side and an angle.

Consider the image below. This shows a 3-4-5 triangle, meaning that the ratios of the side lengths are 3:4:5. The angle θ (theta) is approximately 36.86 degrees. We will be using this triangle to demonstrate trigonometric functions. However, the functions do not refer to angles in degrees. Instead they use radians. We therefore need to know that 36.86 degrees is approximately 0.6433 radians.

Triangle

The Sin method works with doubles only. In the sample code below you can see it used to calculate the lengths of the adjacent side of a triangle based upon the length of the hypotenuse and the angle θ. The sine of the angle provides the ratio of opposite side and hypotenuse. This can be multiplied by the hypotenuse length to obtain the length of the opposite side.

static void Main()
{
    double opposite = GetOpposite(0.6433, 5);       // opposite = 3.0
}

static double GetOpposite(double angle, double hypotenuse)
{
    return Math.Round(Math.Sin(angle) * hypotenuse, 2);
}

Cos

Cos calculates the cosine of an angle. Similar to the sine, the cosine of an angle is the ratio of the adjacent side and the hypotenuse of a right-angled triangle. The sample code below calculates the length of the side adjacent to an angle based upon the angle in radians and the length of the hypotenuse.

static void Main()
{
    double adjacent = GetAdjacent(0.6433, 5);       // adjacent = 4.0
}

static double GetAdjacent(double angle, double hypotenuse)
{
    return Math.Round(Math.Cos(angle) * hypotenuse, 2);
}

Tan

Tan completes the trio of basic trigonometric functions. This calculates the tangent of an angle which, in our right-angled triangle, is the ratio between the opposite and adjacent sides of that angle. This calculation does not include the hypotenuse. When used together, Sin, Cos and Tan can be used to calculate all of the angles and side lengths of a right-angled triangle when one of the non-right-angled angles and one side length is known.

We can use the tangent function to calculate the opposite side when given the adjacent side and angle, as shown below:

static void Main()
{
    double opposite = GetOpposite(0.6433, 4);       // adjacent = 4.0
}

static double GetOpposite(double angle, double adjacent)
{
    return Math.Round(Math.Tan(angle) * adjacent, 2);
}
7 October 2011