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 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.

## Exponential and Logarithmic Functions

The exponential and logarithmic functions are used to raise numbers to specified powers and to calculate logarithms.

### Pow

Pow raises one value to the power of another. The first argument is the value to be multiplied and the second is the power to raise to. Both arguments and the return value are doubles.

The sample code below uses the Pow method to calculate compound interest for a loan. The AddInterest method calculates a multiplier based upon the annual percentage rate for interest. The totalMultiplier is the value that the original amount must be multiplied by to generate the value of the load for the entire period, assuming no interest is paid, and is calculated by raising the single-year multiplier to the power determined by the duration of the loan.

```static void Main()
{
double total;
total = AddInterest(5000, 10, 1);       // total = 1100.0
total = AddInterest(5000, 10, 2);       // total = 1210.0
total = AddInterest(10000, 8, 5);       // total = 14693.28
total = AddInterest(100000, 3.5, 25);   // total = 236324.5
}

static double AddInterest(double value, double annualPercentage, int years)
{
double multiplier = 1 + (annualPercentage / 100);
double totalMultiplier = Math.Pow(multiplier, years);
double finalValue = value * totalMultiplier;
return Math.Round(finalValue, 2, MidpointRounding.AwayFromZero);
}```

### Exp

Exp raises Euler's number (e) to a given power and returns the result. This is known as the exponential function. The function is similar to Pow but differs because the number being multiplied cannot be changed from e. The value of e is a fundamental constant. When plotted on a graph, e is the value where the rate of change at any point on the graph is equal to the current value. You can see this in the image below. The darker line shows the plotting of the exponential function. The lighter line is highlighting the rate of change when x is zero. At this point, the value on the y-axis is one and the rate of change is also one.

The method works with doubles. Other types must be cast to and from doubles to use the function.

The sample code below estimates the size of a bacteria colony, assuming that the bacteria are constantly increasing in number at a rate equal to the number of bacteria actually present. The size calculation method requires that an initial size and a duration are provided as arguments.

```static void Main()
{
double bacteria;
bacteria = BacterialGrowth(1000000, 0);    // bacteria = 1000000
bacteria = BacterialGrowth(1000000, 1);    // bacteria = 2718281
bacteria = BacterialGrowth(1000000, 2);    // bacteria = 7389056
bacteria = BacterialGrowth(1000000, 3);    // bacteria = 20085536
bacteria = BacterialGrowth(7389056, 1);    // bacteria = 20085536
}

static double BacterialGrowth(double start, int duration)
{
double colonySize = Math.Exp(duration) * start;
return Math.Floor(colonySize);
}```

### Log

Log calculates logarithm of a number. This is the inverse function of raising a value to a power, allowing you to determine the power that a specified value must be raised by in order to achieve a given result. For example, 2 raised to the power of 3 gives the result 8. The base 2 logarithm of 8 is, therefore, 3. The Log function has two variations that each work with doubles. When used with a single parameter the natural logarithm is calculated. This is the base e logarithm of the provided value.

In the following example we are calculating the natural logarithm of a value. The starting value is approximately e squared so the result is almost 2.

```double log;
log = Math.Log(7.389056);                   // log = 1.9999999866111924
log = Math.Round(Math.Log(7.389056), 2);    // log = 2.0```

If you want to calculate the logarithm for an arbitrary base number, provide the base as the second argument, as in the following examples. Note that the first calculation should produce a result of three. However, the limitations of the double-precision data type cause a rounding error.

```double log;
log = Math.Log(1000, 10);   // log = 2.9999999999999996
log = Math.Log(1024, 2);    // log = 10.0
log = Math.Log(81, 3);      // log = 4.0
log = Math.Log(10000, 7.5); // log = 4.5711085238719988```
7 October 2011