- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Math Class in C#
The Match class has static methods and constants for trigonometric, logarithmic and other mathematical functions.
The Math class in C# has Math.E and Math.PI fields. Let us see an example of both the fields −
Math.E
It is the natural logarithmic base specified by the constant e.
Syntax
The syntax is as follows −
public const double E = 2.71828182845905;
Example
Let us now see an example −
using System; public class Demo{ public static void Main(){ double d = Math.E; Console.WriteLine("Math.E = " + d); } }
Output
This will produce the following output −
Math.E = 2.71828182845905
Math.PI
The Math.PI field represents the ratio of the circumference of a circle to its diameter, specified by the constant, π.
Syntax
The syntax is as follows −
public const double PI = 3.14159265358979;
Example
Let us now see an example −
using System; public class Demo{ public static void Main(){ double d = Math.PI; Console.WriteLine("Math.PI = " + d); } }
Output
This will produce the following output −
Math.PI = 3.14159265358979
Now, let us see some examples of Math class methods.
Math.Acos()
The Math.Acos() method in C# returns the angle whose cosine is the specified number. This number is a double value argument.
Syntax
The syntax is as follows −
public static double Acos (double val);
Above, Val is the number representing a cosine, where Val must be greater than or equal to -1, but less than or equal to 1.
Example
Let us now see an example to implement Math.Acos() method −
using System; public class Demo { public static void Main(){ double val1 = -0.0; double val2 = Double.PositiveInfinity; double val3 = Double.NaN; Console.WriteLine("Return value of {0} : {1}", val1, Math.Acos(val1)); Console.WriteLine("Return value of {0} : {1}", val2, Math.Acos(val2)); Console.WriteLine("Return value of {0} : {1}", val2, Math.Acos(val3)); } }
Output
This will produce the following output −
Return value of 0 : 1.5707963267949 Return value of ∞ : NaN Return value of ∞ : NaN
Math.Pow()
The Math.Pow() method in C# is used to compute a number raised to the power of some other number.
Syntax
Following is the syntax −
public static double Pow(double val1, double val2)
Above, val1 is a double-precision floating-point number to be raised to a power., whereas val2 is a double-precision floating-point number that specifies a power.
Example
Let us now see an example to implement Math.Pow() method −
using System; public class Demo { public static void Main(){ double res; res = Math.Pow(5, 0); Console.WriteLine("Math.Pow(5,0) = "+res); res = Math.Pow(0,5); Console.WriteLine("Math.Pow(0,5) = "+res); } }
Output
This will produce the following output −
Math.Pow(5,0) = 1 Math.Pow(0,5) = 0