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.

Comparison

The comparison methods allow you to compare two values and obtain the larger or smaller.

Max

Max returns the larger of the two numbers that are provided to the method's parameters. The values can be of the byte, decimal, double, short, int, long, sbyte, single, ushort, uint or ulong data types. Both arguments are the same type as the return value.

The following sample code repeatedly uses the Max method to obtain the highest number from an array of integers.

static void Main()
{
    int largest;
    largest = GetLargest(1, 2, 3, 4);       // largest = 4
    largest = GetLargest(0, -10, 5, 8);     // largest = 8
}

static int GetLargest(params int[] values)
{
    if (values.Length == 0) throw new ArgumentException(
        "At least one value must be present");

    int max = int.MinValue;

    foreach (int i in values)
    {
        max = Math.Max(max, i);
    }

    return max;
}

Min

Min returns the smaller of two numbers. The usable data types are the same as for Math.Max. The following sample code repeatedly uses the Min method to obtain the lowest number in an integer array.

static void Main()
{
    int smallest;
    smallest = GetSmallest(1, 2, 3, 4);
    smallest = GetSmallest(0, -10, 5, 8);
}

static int GetSmallest(params int[] values)
{
    if (values.Length == 0) throw new ArgumentException(
        "At least one value must be present");

    int min = int.MaxValue;

    foreach (int i in values)
    {
        min = Math.Min(min, i);
    }

    return min;
}

Math Constants

The final members of the Math class provide a pair of standard mathematical constants.

E

We've seen the use of Euler's number earlier in this article when discussing the exponential and logarithmic functions. Usually referred to using a lower case letter 'e', the Math library uses an upper case E. It returns a double containing e to sixteen decimal places.

double e = Math.E; // e = 2.7182818284590451

PI

PI returns an approximate value of pi (π), which is the ratio of the circumference of a circle and its diameter. Pi is essential when calculating the circumference or area of a circle, the surface area or volume of a sphere and for many other geometrical calculations. It can also be used to convert between radians and degrees. There are 360 degrees in a circle or 2π radians. So to convert from radians to degrees we multiply by 180/π, as demonstrated below:

static void Main()
{
    double pi = Math.PI;                        // pi = 3.1415926535897931
    double radians = DegreesToRadians(36.86);   // radians = 0.64332836228510981
    double degrees = RadiansToDegrees(0.6433);  // degrees = 36.858374960765858
}

static double DegreesToRadians(double degrees)
{
    return degrees / (180 / Math.PI);
}

static double RadiansToDegrees(double radians)
{
    return radians * (180 / Math.PI);
}
7 October 2011