C# Math Functions with Examples

C# provides a set of built-in mathematical functions, which are part of the System.Math namespace. These functions cover a wide range of mathematical operations, from simple arithmetic to advanced trigonometry and logarithmic functions.

In this guide, we'll explore the most commonly used math functions in C#, with examples of how to use them in your code.

Math Constants

The System.Math namespace provides a set of constants that are frequently used in mathematical operations. Here are some of the most common ones:

1Math.PI // The value of Pi (3.141592653589793) 2Math.E // The value of Euler's number (2.718281828459045)

Basic Math Functions

Abs

The Abs function returns the absolute value of a number.

1int x = -5; 2int absX = Math.Abs(x); 3// absX = 5

Sign

The Sign function returns the sign of a number as an integer (-1 for negative, 0 for zero, 1 for positive).

1int x = -5; 2int signX = Math.Sign(x); 3// signX = -1

Floor

The Floor function returns the largest integer that is less than or equal to the specified decimal or double number.

1double x = 3.75; 2double floorX = Math.Floor(x); 3// floorX = 3

Ceiling

The Ceiling function returns the smallest integer that is greater than or equal to the specified decimal or double number.

1double x = 3.75; 2double ceilingX = Math.Ceiling(x); 3// ceilingX = 4

Round

The Round function rounds a decimal or double number to the nearest integer.

1double x = 3.75; 2double roundedX = Math.Round(x); 3// roundedX = 4

Max and Min

The Max and Min functions return the maximum and minimum values from a set of numbers, respectively.

1int a = 5, b = 10, c = 7; 2int maxVal = Math.Max(a, Math.Max(b, c)); 3// maxVal = 10 4 5int minVal = Math.Min(a, Math.Min(b, c)); 6// minVal = 5

Trigonometric Functions

C# provides a set of trigonometric functions, which are commonly used in geometry and physics. Here are some examples:

Sin, Cos, Tan

The Sin, Cos, and Tan functions return the sine, cosine, and tangent of an angle, respectively. The argument of these functions is specified in radians.

1double angle = Math.PI / 4; 2double sinVal = Math.Sin(angle); 3double cosVal = Math.Cos(angle); 4double tanVal = Math.Tan(angle);

Arcsin, Arccos, Arctan

The Asin, Acos, and Atan functions return the inverse sine, inverse cosine, and inverse tangent of a value, respectively. The result of these functions is specified in radians.

1double value = 0.5; 2double asinVal = Math.Asin(value); 3double acosVal = Math.Acos(value); 4double atanVal = Math.Atan(value);

Exponential and Logarithmic Functions

C# provides several functions for calculating exponential and logarithmic values.

  • Math.Exp: This function returns e raised to the specified power. It takes a double argument and returns a double value. The mathematical expression for this function is e^x. Example:
1double expValue = Math.Exp(2.0); // expValue is 7.3890560989306495
  • Math.Log: This function returns the natural (base e) logarithm of a specified number. It takes a double argument and returns a double value. Example:
1double logValue = Math.Log(10.0); // logValue is 2.302585092994046
  • Math.Log10: This function returns the base 10 logarithm of a specified number. It takes a double argument and returns a double value. Example:
1double log10Value = Math.Log10(100.0); // log10Value is 2
  • Math.Pow: This function returns a specified number raised to the specified power. It takes two double arguments and returns a double value. Example:
1double powValue = Math.Pow(2.0, 3.0); // powValue is 8

Trigonometric Functions

C# provides several functions for calculating trigonometric values. These functions take and return values in radians.

  • Math.Sin: This function returns the sine of the specified angle. It takes a double argument and returns a double value. Example:
1double sinValue = Math.Sin(Math.PI / 2); // sinValue is 1
  • Math.Cos: This function returns the cosine of the specified angle. It takes a double argument and returns a double value. Example:
1double cosValue = Math.Cos(Math.PI); // cosValue is -1
  • Math.Tan: This function returns the tangent of the specified angle. It takes a double argument and returns a double value. Example:
1double tanValue = Math.Tan(Math.PI / 4); // tanValue is 0.999999999999999
  • Math.Asin: This function returns the arcsine of the specified angle. It takes a double argument and returns a double value. Example:
1double asinValue = Math.Asin(1); // asinValue is 1.5707963267948966
  • Math.Acos: This function returns the arccosine of the specified angle. It takes a double argument and returns a double value. Example:
1double acosValue = Math.Acos(-1); // acosValue is 3.141592653589793
  • Math.Atan: This function returns the arctangent of the specified angle. It takes a double argument and returns a double value. Example:
1double atanValue = Math.Atan(1); // atanValue is 0.7853981633974483

Random Number Functions

C# provides functions for generating random numbers.

  • Random: This class generates random numbers. You can create an instance of this class and use the Next method to generate a random number. Example:
1Random rand = new Random(); 2int randomValue = rand.Next(1, 100); // generates a random number between 1 and 100