Math class in C#

There are many situation when we need some mathematical operation in our program, such as some trigonometric and logarithm tics.

.Net framework provides us Math class of System namespaces that allows us very rich set of static methods. It also contains e and PI.

These are the list of some mathematical function:

AbsOverloaded. Returns the absolute value of a specified number.
AcosReturns the angle whose cosine is the specified number.
AsinReturns the angle whose sine is the specified number.
AtanReturns the angle whose tangent is the specified number.
Atan2Returns the angle whose tangent is the quotient of two specified numbers.
BigMulProduces the full product of two 32-bit numbers.
CeilingOverloaded. Returns the smallest integer greater than or equal to the specified number.
CosReturns the cosine of the specified angle.
CoshReturns the hyperbolic cosine of the specified angle.
DivRemOverloaded. Calculates the quotient of two numbers and also returns the remainder in an output parameter.
EqualsOverloaded. Determines whether two Object instances are equal. (inherited from Object)
ExpReturns e raised to the specified power.
FloorOverloaded. Returns the largest integer less than or equal to the specified number.
GetHashCodeServes as a hash function for a particular type. (inherited from Object)
GetTypeGets the Type of the current instance. (inherited from Object)
IEEERemainderReturns the remainder resulting from the division of a specified number by another specified number.
LogOverloaded. Returns the logarithm of a specified number.
Log10Returns the base 10 logarithm of a specified number.
MaxOverloaded. Returns the larger of two specified numbers.
MinOverloaded. Returns the smaller of two numbers.
PowReturns a specified number raised to the specified power.
ReferenceEqualsDetermines whether the specified Object instances are the same instance. (inherited from Object)
RoundOverloaded. Rounds a value to the nearest integer or specified number of decimal places.
SignOverloaded. Returns a value indicating the sign of a number.
SinReturns the sine of the specified angle.
SinhReturns the hyperbolic sine of the specified angle.
SqrtReturns the square root of a specified number.
TanReturns the tangent of the specified angle.
TanhReturns the hyperbolic tangent of the specified angle.
ToStringReturns a String that represents the current Object. (inherited from Object)
TruncateOverloaded. Calculates the integral part of a number.

It is very important to know that those methods take double type parameters returns double type value.


The following example shows the use of the pow, sqrt, round, min, log, sin and DivRem functions.

    private void Example_MathClassFunctions()
        double k = 0;
        k = Math.Pow(5, 5);
        MessageBox.Show("5 to the power= " + k);
        k = Math.Sqrt(5);
        MessageBox.Show("squre root of 5= " + k);
        k = Math.Round(5.68743);
        MessageBox.Show("round figure of 5.68743= " + k);
        int i = 0;
        i = Math.Min(4, 7);
        MessageBox.Show("minimum value in 4 and 7= " + i);
        double j = 0;
        j = Math.Log(15);
        MessageBox.Show("Log value of 15 = " + j);
        j = Math.Sin(90);
        MessageBox.Show("sin 90 = " + j);
        int x = 511;
        int y = 50;
        int Value = 0;
        int Remainder = 0;
        Value = Math.DivRem(x, y, out Remainder);
        MessageBox.Show("value is  = " + Value + " and reminder is = " + Remainder);

We already say that members and methods of math class are static ,means they are accessed using the class name itself instead of its object.