Math

The Math class in C# is a static class in the System namespace that provides methods and constants for trigonometric, logarithmic, and other common mathematical functions. Some of the key methods include:

Trigonometric Functions

Sin(), Cos(), and Tan() return the sine, cosine, and tangent of a specified angle, respectively. Their inverses are given by Asin(), Acos(), and Atan(). Hyperbolic versions of these functions are also available.

Example

double a = 100.50;
Console.WriteLine(Math.Sin(a));
Console.WriteLine(Math.Cos(a));
Console.WriteLine(Math.Tan(a));

Console.WriteLine(Math.Asin(a));
Console.WriteLine(Math.Acos(a));
Console.WriteLine(Math.Atan(a));

Logarithmic Functions

Log() returns the natural logarithm of a specified number, and Log10() returns the base 10 logarithm.

Exponential Functions

The function Exp() returns the mathematical constant e, which is the base of natural logarithms, raised to the power specified as its argument.

Power Functions

Pow() returns a specified number raised to the specified power.

Rounding Functions

Ceiling() returns the smallest integer value greater than or equal to the specified number, and Floor() returns the largest integer value less than or equal to the specified number.

Example

double a = 5.34, b = 9.89;
Console.WriteLine(Math.Ceiling(a));  //output is 6
Console.WriteLine(Math.Floor(b));  //output is 9

Absolute Value

The Abs() function returns the non-negative value of a specified number. It is used to convert a negative number into its positive equivalent.

Square Root

The function Sqrt() calculates and returns the square root of a given number.

Example

int a = 9;
Console.WriteLine(Math.Sqrt(a));  //output is 3

Max and Min

The Max() function returns the larger of two numbers, while the Min() function returns the smaller of the two numbers.

Example

int a = 5, b = 9;
Console.WriteLine(Math.Min(a,b));  //output is 5
Console.WriteLine(Math.Max(a,b));  //output is 9

Constants

The Math class provides two important mathematical constants: E (the base of natural logarithms) and PI (the ratio of the circumference of a circle to its diameter).
These methods can be used to perform a variety of mathematical tasks, such as calculating the inner angles of a trapezoid1. The Math class is a powerful tool for any C# programmer needing to perform mathematical operations.
Post a comment

Leave a Comment

Scroll to Top