Windows Phone Developers

Thursday, October 30, 2008

Mathematical Functions in C# / Logarithmic Function in C# / Trigonometric Functions in C#

Mathematical Functions in .NET / Logarithmic Function in .NET / Trigonometric Functions in .NET

The common logarithmic, trigonometric functions are available in the .NET framework as part of System.Math class. Here is a list of functions that are available

Name

Description

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.

Exp

Returns e raised to the specified power.

Floor

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

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.

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.

Truncate

Overloaded. Calculates the integral part of a number.

How to use Mathematical Function PI in C# (.NET)

Mathematical PI, which represents the ratio of the circumference of a circle to its diameter, is specified by the constant, π and can be retrieved by

Console.WriteLine("Value of PI = " + Math.PI);

Similarly other trigonometric functions like Sin, Cos, Tan etc can be used in C# as follows:

public static void MathFunctions()

{

Console.WriteLine("Value of PI = " + Math.PI);

Console.WriteLine("Value of Tan 45 = " + Math.Tan(45*Math.PI/180));

Console.WriteLine("Value of Cos 45 = " + Math.Cos(45 * Math.PI / 180));

Console.WriteLine("Value of Sin 45 = " + Math.Sin(45 * Math.PI / 180));

}

Functions Sin, Cos, Tan etc accept angle in Radians (NOT in degrees). If you want to use degrees as input, use appropriate conversion functions

Radians to Degree Conversion using C#

public static double Radians2Degrees(double degrees)

{

try

{

return Math.PI * degrees / 180;

}

catch (Exception ex)

{

Console.WriteLine(ex.ToString());

return -1;

}

}

Console.WriteLine("Value of Tan 45 = " +

Math.Tan(Radians2Degrees(45)));

The output will be as shown below


Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

4 comments:

  1. tnx mate, I wasted 10 mins bcoz I didn't know this functions takes its arguments in radians

    ReplyDelete
  2. Thanks for the post, helped in my assignments! :)

    ReplyDelete
  3. thanx for this it helped alot

    ReplyDelete
  4. http://www.codeproject.com/script/Articles/ArticleVersion.aspx?aid=339638&av=501750


    :) derivative, integrate... all

    ReplyDelete