Math class in C#


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:

Abs

Overloaded. Returns the absolute value of a specified number.

Acos

Returns the angle whose cosine is the specified number.

Asin

Returns the angle whose sine is the specified number.

Atan

Returns the angle whose tangent is the specified number.

Atan2

Returns the angle whose tangent is the quotient of two specified numbers.

BigMul

Produces the full product of two 32-bit numbers.

Ceiling

Overloaded. Returns the smallest integer greater than or equal to the specified number.

Cos

Returns the cosine of the specified angle.

Cosh

Returns the hyperbolic cosine of the specified angle.

DivRem

Overloaded. Calculates the quotient of two numbers and also returns the remainder in an output parameter.

Equals 

Overloaded. Determines whether two Object instances are equal. (inherited from Object)

Exp

Returns e raised to the specified power.

Floor

Overloaded. Returns the largest integer less than or equal to the specified number.

GetHashCode 

Serves as a hash function for a particular type. (inherited from Object)

GetType 

Gets the Type of the current instance. (inherited from Object)

IEEERemainder

Returns the remainder resulting from the division of a specified number by another specified number.

Log

Overloaded. Returns the logarithm of a specified number.

Log10

Returns the base 10 logarithm of a specified number.

Max

Overloaded. Returns the larger of two specified numbers.

Min

Overloaded. Returns the smaller of two numbers.

Pow

Returns a specified number raised to the specified power.

ReferenceEquals 

Determines whether the specified Object instances are the same instance. (inherited from Object)

Round

Overloaded. Rounds a value to the nearest integer or specified number of decimal places.

Sign

Overloaded. Returns a value indicating the sign of a number.

Sin

Returns the sine of the specified angle.

Sinh

Returns the hyperbolic sine of the specified angle.

Sqrt

Returns the square root of a specified number.

Tan

Returns the tangent of the specified angle.

Tanh

Returns the hyperbolic tangent of the specified angle.

ToString 

Returns a String that represents the current Object. (inherited from Object)

Truncate

Overloaded. Calculates the integral part of a number.

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

Example :

private void Example_MathClassFunctions()

        {

            int i = 0;

            i = Math.Pow(5, 5);

            MessageBox.Show("5 to the power= " + i);

 

            i = Math.Sqrt(5);

            MessageBox.Show("squre root of 5= " + i);

 

            i = Math.Round(5.68743);

            MessageBox.Show("round figure of 5.68743= " + i);

 

            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.
 


Recommended Free Ebook
Similar Articles