System.Math Class
Provides fundamental mathematical operations and constants. The `Math` class is a static class, meaning you do not create an instance of it. Its members are accessed directly through the class name.
Introduction
The System.Math class in the .NET Framework provides a comprehensive set of static methods and constants for performing common mathematical operations. This includes trigonometric functions, logarithmic and exponential functions, absolute value, rounding, and more. Since it's a static class, you can call its methods directly without instantiating the class, for example, Math.Sin(angle).
Fields
-
PI
public static readonly double PIRepresents the mathematical constant π. -
E
public static readonly double ERepresents Euler's number, the base of the natural logarithm. -
MAX_VALUE
public const double MAX_VALUERepresents the largest possible value of adouble. -
MIN_VALUE
public const double MIN_VALUERepresents the smallest positive normalized value of adouble. -
NEGATIVE_INFINITY
public const double NEGATIVE_INFINITYRepresents negative infinity. -
POSITIVE_INFINITY
public const double POSITIVE_INFINITYRepresents positive infinity. -
NaN
public const double NaNRepresents "Not-a-Number".
Methods
-
Abs
public static double Abs(double d)Returns the absolute value of a double-precision floating-point number.d: The number to get the absolute value of.Returns: The absolute value ofd. -
Sin
public static double Sin(double d)Returns the sine of the specified angle, measured in radians.d: An angle, in radians.Returns: The sine of the angle. -
Cos
public static double Cos(double d)Returns the cosine of the specified angle, measured in radians.d: An angle, in radians.Returns: The cosine of the angle. -
Tan
public static double Tan(double d)Returns the tangent of the specified angle, measured in radians.d: An angle, in radians.Returns: The tangent of the angle. -
Sqrt
public static double Sqrt(double d)Returns the square root of a specified number.d: The number to get the square root of.Returns: The square root ofd. -
Pow
public static double Pow(double x, double y)Returns a specified number raised to the specified power.x: The base.
y: The exponent.Returns: The numberxraised to the power ofy. -
Round
public static double Round(double d)Rounds a double-precision floating-point number to the nearest integer.d: The number to round.Returns: The nearest integer tod. -
Floor
public static double Floor(double d)Returns the largest integer less than or equal to the specified double-precision floating-point number.d: A number to round down to the nearest integer.Returns: The largest integer less than or equal tod. -
Ceiling
public static double Ceiling(double d)Returns the smallest integer greater than or equal to the specified double-precision floating-point number.d: A number to round up to the nearest integer.Returns: The smallest integer greater than or equal tod. -
Log
public static double Log(double d)Returns the natural (base e) logarithm of a specified number.d: The number whose logarithm is to be found.Returns: The natural logarithm ofd. -
Log10
public static double Log10(double d)Returns the base 10 logarithm of a specified number.d: The number whose logarithm is to be found.Returns: The base 10 logarithm ofd. -
Exp
public static double Exp(double d)Returns e raised to the power of a specified number.d: A number, representing the exponent of e.Returns: e raised to the power ofd.
Example Usage
Here's a simple C# example demonstrating the use of the Math class:
using System;
public class MathExample
{
public static void Main(string[] args)
{
double radius = 5.0;
double circumference = 2 * Math.PI * radius;
double area = Math.PI * Math.Pow(radius, 2);
double absoluteValue = Math.Abs(-10.5);
double roundedValue = Math.Round(15.789);
double squareRoot = Math.Sqrt(25.0);
Console.WriteLine($"Radius: {radius}");
Console.WriteLine($"Circumference: {circumference:F2}"); // Formatted to 2 decimal places
Console.WriteLine($"Area: {area:F2}"); // Formatted to 2 decimal places
Console.WriteLine($"Absolute value of -10.5: {absoluteValue}");
Console.WriteLine($"Rounded value of 15.789: {roundedValue}");
Console.WriteLine($"Square root of 25.0: {squareRoot}");
double angleInDegrees = 45.0;
double angleInRadians = angleInDegrees * Math.PI / 180.0;
double sineValue = Math.Sin(angleInRadians);
Console.WriteLine($"Sine of {angleInDegrees} degrees: {sineValue:F4}");
}
}