Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Math Class Fields with Examples in C#
The Math class in C# provides essential mathematical constants through predefined fields. The two most commonly used fields are Math.E (Euler's number) and Math.PI (pi), which represent fundamental mathematical constants used in various calculations.
These fields are declared as public const double, making them accessible throughout your application without needing to instantiate the Math class.
Math.E Field
The Math.E field represents Euler's number, which is the natural logarithmic base. This constant is approximately equal to 2.718281828 and is frequently used in exponential and logarithmic calculations.
Syntax
public const double E = 2.71828182845905;
Example
Here's how to use Math.E in calculations −
using System;
public class Demo {
public static void Main() {
double eulerNumber = Math.E;
Console.WriteLine("Math.E = " + eulerNumber);
// Using E in exponential calculation
double result = Math.Pow(Math.E, 2);
Console.WriteLine("e^2 = " + result);
// Natural logarithm of E is 1
double naturalLog = Math.Log(Math.E);
Console.WriteLine("ln(e) = " + naturalLog);
}
}
The output of the above code is −
Math.E = 2.71828182845905 e^2 = 7.38905609893065 ln(e) = 1
Math.PI Field
The Math.PI field represents pi (?), which is the ratio of a circle's circumference to its diameter. This constant is approximately equal to 3.141592654 and is essential for geometric calculations involving circles and trigonometry.
Syntax
public const double PI = 3.14159265358979;
Example
Here's how to use Math.PI in geometric calculations −
using System;
public class Demo {
public static void Main() {
double pi = Math.PI;
Console.WriteLine("Math.PI = " + pi);
// Calculate area of a circle with radius 5
double radius = 5.0;
double area = Math.PI * radius * radius;
Console.WriteLine("Area of circle (r=5): " + area);
// Calculate circumference of a circle with radius 5
double circumference = 2 * Math.PI * radius;
Console.WriteLine("Circumference of circle (r=5): " + circumference);
}
}
The output of the above code is −
Math.PI = 3.14159265358979 Area of circle (r=5): 78.5398163397448 Circumference of circle (r=5): 31.4159265358979
Practical Application
Here's an example showing both constants used together in a practical scenario −
using System;
public class MathConstants {
public static void Main() {
Console.WriteLine("Mathematical Constants Demonstration");
Console.WriteLine("===================================");
// Using PI for trigonometric calculations
double angle = Math.PI / 4; // 45 degrees in radians
double sinValue = Math.Sin(angle);
Console.WriteLine("sin(?/4) = " + sinValue);
// Using E for exponential growth calculation
double growthRate = 0.05; // 5% growth rate
double time = 2.0;
double finalAmount = 1000 * Math.Pow(Math.E, growthRate * time);
Console.WriteLine("Continuous compound interest: " + finalAmount);
// Combined usage
double complexResult = Math.PI * Math.E;
Console.WriteLine("? × e = " + complexResult);
}
}
The output of the above code is −
Mathematical Constants Demonstration =================================== sin(?/4) = 0.707106781186548 Continuous compound interest: 1105.17091807565 ? × e = 8.53973422267357
Conclusion
The Math.E and Math.PI fields provide access to fundamental mathematical constants in C#. Math.E is essential for logarithmic and exponential calculations, while Math.PI is crucial for geometric and trigonometric operations involving circles and angles.
