In C#, the Math class provides the constants and the static methods for logarithmic, trigonometric, and other mathematical functions. Math class has two fields as follows:
- Math.E Field
- Math.PI Field
Math.E Field
This field represents the natural logarithmic base, specified by the constant, e.
Syntax:
public const double E
Program 1: To illustrate the Math.E field in Math class
// C# program to demonstrate the // Constant values of Math.E function using System;
class GFG {
// Main method
static void Main()
{
// To find E constant values
double e = Math.E;
// Print result
Console.WriteLine( "Math.E = " + e);
}
} |
Math.E = 2.71828182845905
Program 2: Let’s see an example to compare Math.E with the value calculated from a power series.
// C# program to demonstrate the to // find the values of Math.E field using System;
class GFG {
// Main Method
public static void Main()
{
// Initialize the data
double fact = 1.0;
double PS = 0.0;
// for loop run until the absolute
// values condition satisfied
for ( int n = 0; n < 10 &&
Math.Abs(Math.E - PS) > 1.0E-15; n++)
{
// Calculate the factorial
if (n > 0)
// update factorial
fact *= ( double )n;
// Calculate the power series.
PS += 1.0 / fact;
// Display the power series result
Console.WriteLine(PS);
Console.WriteLine(Math.E - PS);
}
}
} |
1 1.71828182845905 2 0.718281828459045 2.5 0.218281828459045 2.66666666666667 0.0516151617923786 2.70833333333333 0.00994849512571205 2.71666666666667 0.00161516179237875 2.71805555555556 0.000226272903489644 2.71825396825397 2.7860205076724E-05 2.71827876984127 3.05861777505356E-06 2.71828152557319 3.02885852843104E-07
Math.PI Field
It represents the ratio of the circumference of a circle to its diameter, specified by the constant, PI(π).
Syntax:
public const double PI
Program 1: To illustrate the Math.PI field in Math class
// C# program to demonstrate the // Constant values of Math.PI function using System;
class GFG
{ // Main method
static void Main()
{
// To find PI constant values
double pi_value = Math.PI;
// Print result
Console.WriteLine( "Math.PI = " + pi_value);
}
} |
Math.PI = 3.14159265358979
Program 2: To demonstrate the multiplication of different data types with Math.PI constant value.
// C# program to demonstrate the // Constant values of Math.PI function using System;
class GFG
{ // Main method
static void Main()
{
// Multiply int ,float,negative number with
// Math.PI and display result
Console.WriteLine(1 * Math.PI );
Console.WriteLine(2 * Math.PI );
Console.WriteLine(1.5 * Math.PI );
Console.WriteLine(-3.10 * Math.PI );
}
} |
3.14159265358979 6.28318530717959 4.71238898038469 -9.73893722612836
Program 3: Let’s Demonstrate the access of PI and find the Volume of the cylinder.
// C# program to demonstrate the // Constant values of Math.PI function using System;
class GFG {
// Main method
static void Main()
{
// input radius value
double radius = 6;
// input length value
double length = 9;
// calculate the area using PI
double area = radius * radius * Math.PI;
// Calculate volume
double volume = area * length;
// print area and volume of cylinder
Console.WriteLine( "Area of cylinder is : " + area);
Console.WriteLine( "Volume of cylinder is : " + volume);
}
} |
Area of cylinder is : 113.097335529233 Volume of cylinder is : 1017.87601976309
References:
- https://msdn.microsoft.com/en-us/library/system.math.e(v=vs.110).aspx
- https://msdn.microsoft.com/en-us/library/system.math.pi(v=vs.110).aspx