csharp-clase-math

What is and How to Use the Math Class in C#

  • 10 min

So far, we have seen how to add, subtract, or multiply using basic operators (+, -, *, /). But what if we need to calculate a square root, a sine, or round a number with specific precision?

This is where the System.Math class comes into play.

The Math class is a static class that provides constants and static methods for trigonometric, logarithmic, and other common mathematical functions.

Being a static class, we do not need (and in fact cannot) create an instance of it with new. We use its methods by calling the class directly: Math.Method().

Constants in the Math Class

The Math class includes very useful constants:

ConstantValue in C# (Math)Description
Math.PI3.141592653589793Represents the value of π (pi).
Math.E2.718281828459045Base of the natural logarithm (Euler’s number).
Math.Tau6.283185307179586Represents the value of τ (tau), which is 2π.
Math.Sqrt21.4142135623730951Represents the square root of 2.
Math.Sqrt31.7320508075688772Represents the square root of 3.
Math.Log2E1.4426950408889634Base 2 logarithm of E.
Math.Log10E0.4342944819032518Base 10 logarithm of E.
Math.EpsilonDouble.EpsilonThe smallest positive number greater than zero.
Console.WriteLine($"The value of PI is: {Math.PI}");
Console.WriteLine($"The value of E is: {Math.E}");
Copied!

Basic Methods of the Math Class

MethodDescription
Math.Abs(value)Returns the absolute value of the number.
Math.Max(a, b)Returns the larger of two values.
Math.Min(a, b)Returns the smaller of two values.
Math.Sign(value)Returns an integer indicating the sign of the number: -1 (negative), 0 (zero), or 1 (positive).

Examples:

double value = -7.5;
double absolute = Math.Abs(value);   // Result: 7.5

double a = 5, b = 10;
double maximum = Math.Max(a, b);     // Result: 10
double minimum = Math.Min(a, b);     // Result: 5

int sign1 = Math.Sign(value);        // Result: -1
int sign2 = Math.Sign(0);            // Result: 0
int sign3 = Math.Sign(8.2);          // Result: 1
Copied!

Rounding Numbers

Rounding is crucial in engineering to simplify results and improve the precision of data presentation.

MethodDescription
Math.Round(value)Rounds to the nearest integer.
Math.Ceiling(value)Rounds up (ceiling).
Math.Floor(value)Rounds down (floor).
Math.Truncate(value)Removes the decimal part without rounding.

Examples:

double value1 = 3.56;
double rounded = Math.Round(value1);   // Result: 4
double ceiling = Math.Ceiling(value1); // Result: 4
double floor = Math.Floor(value1);     // Result: 3
double truncated = Math.Truncate(value1); // Result: 3
Copied!

Rounding with Precision

The Math.Round method can round a number to a specific number of decimal places.

double number = 3.14159;
Console.WriteLine($"Rounded to 2 decimal places: {Math.Round(number, 2)}"); // 3.14
Copied!

Trigonometric Functions

The Math class includes methods for performing trigonometric calculations, such as sine, cosine, and tangent. These methods use radians as the unit of measurement.

Sine, Cosine, and Tangent

MethodDescription
Math.SinReturns the sine of an angle specified in radians.
Math.CosReturns the cosine of an angle specified in radians.
Math.TanReturns the tangent of an angle specified in radians.

Examples:

double angle = Math.PI / 4; // 45 degrees in radians
Console.WriteLine($"Sine of {angle} radians: {Math.Sin(angle)}");
Console.WriteLine($"Cosine of {angle} radians: {Math.Cos(angle)}");
Console.WriteLine($"Tangent of {angle} radians: {Math.Tan(angle)}");
Copied!

Inverse Functions

MethodDescription
Math.AsinReturns the arcsine of a number.
Math.AcosReturns the arccosine of a number.
Math.AtanReturns the arctangent of a number.

Examples:

double value = 0.5;
Console.WriteLine($"Arcsine of {value}: {Math.Asin(value)}");
Console.WriteLine($"Arccosine of {value}: {Math.Acos(value)}");
Console.WriteLine($"Arctangent of {value}: {Math.Atan(value)}");
Copied!

The trigonometric functions of Math accept and return angles in Radians, NOT in Degrees.

To convert degrees to radians and vice versa:

ConceptDescription
Math.PIRepresents the value of π (approximately 3.14159).
Convert degrees to radians(degrees * Math.PI) / 180
Convert radians to degrees(radians * 180) / Math.PI

Examples:

double angleDegrees = 30.0;
double angleRadians = (angleDegrees * Math.PI) / 180; // Conversion to radians

double sine = Math.Sin(angleRadians);
double cosine = Math.Cos(angleRadians);
double tangent = Math.Tan(angleRadians);
Copied!

Exponential and Logarithmic Functions

MethodDescription
Math.Log(value)Calculates the natural logarithm (base e) of a value.
Math.Log10(value)Calculates the base 10 logarithm of a value.
Math.Exp(exponent)Calculates the value of e raised to the given exponent.

Examples:

double logValue = 10.0;
double naturalLog = Math.Log(logValue);    // Natural logarithm of 10
double logBase10 = Math.Log10(logValue);   // Base 10 logarithm of 10

double exponent = 2.0;
double expResult = Math.Exp(exponent); // e^2
Copied!

Powers and Roots

Powers and Roots

Power and square root calculations are common in engineering, especially in applications involving exponential growth, signal attenuation, and energy calculations.

MethodDescription
Math.Pow(base, exponent)Calculates the value of the base raised to the exponent.
Math.Sqrt(value)Calculates the square root of a number.

Examples:

double base = 3.0;
double exponent = 4.0;
double powerResult = Math.Pow(base, exponent); // 3^4 = 81

double value = 16.0;
double rootResult = Math.Sqrt(value); // √16 = 4
Copied!

Math.Pow works internally with double types. If you are using it simply to square an integer (e.g., x * x), it is much more efficient to multiply the variable by itself than to call the Pow function.

Practical Examples