Originally published at https://allcoderthings.com/en/article/csharp-math-library
The C# Math Library, provided by the System.Math class, offers a wide range of built-in methods and constants for performing mathematical calculations in C# applications. Since the Math class is static, its methods can be used directly without creating an instance. In this article, we will explore the most commonly used System.Math methods in C# with clear and practical examples.
Absolute Value (Abs)
The Math.Abs method returns the absolute value of a number, removing any negative sign. It is commonly used when the magnitude of a value is important regardless of its sign.
int a = -15;
Console.WriteLine(Math.Abs(a)); // 15
Minimum and Maximum (Min, Max)
The Math.Min and Math.Max methods are used to compare two numeric values and return the smaller or larger one. These methods are useful for validation and boundary checks.
int x = 8, y = 20;
Console.WriteLine(Math.Min(x, y)); // 8
Console.WriteLine(Math.Max(x, y)); // 20
Square Root (Sqrt)
The Math.Sqrt method calculates the square root of a given number. It returns a double value and is often used in geometry and scientific calculations.
double number = 81;
Console.WriteLine(Math.Sqrt(number)); // 9
Power (Pow)
The Math.Pow method raises a number to the power of another number. It is commonly used for exponential calculations and returns a double.
Console.WriteLine(Math.Pow(2, 3)); // 8
Rounding (Round, Ceiling, Floor)
C# provides several rounding methods through the Math class. Math.Round rounds to the nearest value, Math.Ceiling always rounds up, and Math.Floor always rounds down.
double d = 4.7;
Console.WriteLine(Math.Round(d)); // 5 (rounds to the nearest)
Console.WriteLine(Math.Ceiling(d)); // 5 (always rounds up)
Console.WriteLine(Math.Floor(d)); // 4 (always rounds down)
Truncate
The Math.Truncate method removes the fractional part of a number without rounding, effectively cutting off the decimal portion.
double d = 4.9;
Console.WriteLine(Math.Truncate(d)); // 4
Division Remainder (DivRem)
The Math.DivRem method performs integer division and returns both the quotient and the remainder in a single operation. This can be useful for performance-sensitive calculations.
int quotient, remainder;
remainder = Math.DivRem(17, 5, out quotient);
Console.WriteLine("Quotient: " + quotient); // 3
Console.WriteLine("Remainder: " + remainder); // 2
Trigonometric Functions (Sin, Cos, Tan)
The Math.Sin, Math.Cos, and Math.Tan methods calculate trigonometric values using radians. They are commonly used in geometry, graphics, and physics calculations.
double angle = Math.PI / 4; // 45 degrees (in radians)
Console.WriteLine(Math.Sin(angle)); // 0.707...
Console.WriteLine(Math.Cos(angle)); // 0.707...
Console.WriteLine(Math.Tan(angle)); // 1
Angle Conversion (Radians/Degrees, PI Constant)
Trigonometric methods in C# work with radians. By using the Math.PI constant, degrees can be easily converted to radians when necessary.
double degree = 180;
double radian = degree * (Math.PI / 180);
Console.WriteLine(radian); // 3.14159...
Logarithm (Log, Log10)
The Math.Log method calculates the natural logarithm (base e), while Math.Log10 calculates the base-10 logarithm. These functions are frequently used in scientific and financial applications.
Console.WriteLine(Math.Log(100)); // natural log (base e)
Console.WriteLine(Math.Log10(100)); // base-10 log
Maximum and Minimum Double Values
The double.MaxValue and double.MinValue constants represent the largest and smallest values a double can hold. They are useful for comparisons and initializing variables.
Console.WriteLine(double.MaxValue);
Console.WriteLine(double.MinValue);
Square Root and Power Combination
Math methods can be combined to perform complex calculations. In this example, the number is first raised to a power and then its square root is calculated.
double number = 256;
double result = Math.Sqrt(Math.Pow(number, 2));
Console.WriteLine(result); // 256 (first squared, then square root)
Using with Absolute Value
The Math.Abs method is often used together with calculations where negative results are possible, such as differences between values. By applying Math.Abs, you can ensure that the result is always a positive number, which is useful in comparisons and distance calculations.
int diff = -50;
Console.WriteLine(Math.Abs(diff)); // 50
Sign (Sign)
The Math.Sign method returns -1, 0, or 1 depending on whether a number is negative, zero, or positive. It is useful when you only need to know the direction of a value.
Console.WriteLine(Math.Sign(-10)); // -1
Console.WriteLine(Math.Sign(0)); // 0
Console.WriteLine(Math.Sign(25)); // 1
Clamp
The Math.Clamp method restricts a value to a specified range. If the value is less than the minimum, the minimum is returned; if it is greater than the maximum, the maximum is returned.
int value = 120;
int result = Math.Clamp(value, 0, 100);
Console.WriteLine(result); // 100
Exponential (Exp)
The Math.Exp method returns e raised to the specified power. It is commonly used in scientific and statistical calculations.
Console.WriteLine(Math.Exp(1)); // e ≈ 2.71828
Math.Abs: Returns the absolute value of a number.Math.Sign: Indicates whether a number is negative, zero, or positive.Math.Min,Math.Max: Compare two numbers and return the minimum or maximum.Math.Clamp: Restricts a value to a specified range.Math.Sqrt,Math.Pow: Perform square root and exponentiation operations.Math.Round,Math.Ceiling,Math.Floor,Math.Truncate: Rounding and truncation operations.Math.Sin,Math.Cos,Math.Tan: Trigonometric functions (use radians).Math.Log,Math.Log10,Math.Exp: Logarithmic and exponential functions.Math.PI: Constant representing the value of Pi.
Top comments (0)