Math.Max() / Math.Min() / Math.Pow() / Math.Sqrt()
| Since: | C# 1.0(2002) |
|---|
How to use Math.Max() to return the larger of two values, Math.Min() to return the smaller, Math.Pow() to calculate a power, and Math.Sqrt() to return a square root.
Syntax
using System; Math.Max(T val1, T val2) Math.Min(T val1, T val2) // Returns x raised to the power y. The return type is double. Math.Pow(double x, double y) Math.Sqrt(double d) Math.Log(double x) Math.Log(double x, double newBase)
Method List
| Method | Description |
|---|---|
| Math.Max(val1, val2) | Returns the larger of val1 and val2. Overloads are available for int, double, decimal, and other numeric types. |
| Math.Min(val1, val2) | Returns the smaller of val1 and val2. |
| Math.Pow(x, y) | Returns x raised to the power y (x^y). The return value is always of type double. |
| Math.Sqrt(d) | Returns the square root of d. Returns NaN if d is negative. |
| Math.Log(x) | Returns the natural logarithm (base e) of x. |
| Math.Log(x, newBase) | Returns the logarithm of x with the specified base newBase. |
| Math.Log10(x) | Returns the common logarithm (base 10) of x. |
Sample Code
Program.cs
using System; // Math.Max() — Returns the larger of two values. Console.WriteLine(Math.Max(10, 20)); // 20 Console.WriteLine(Math.Max(-5, -3)); // -3 Console.WriteLine(Math.Max(3.7, 3.2)); // 3.7 // Math.Min() — Returns the smaller of two values. Console.WriteLine(Math.Min(10, 20)); // 10 Console.WriteLine(Math.Min(-5, -3)); // -5 // Math.Pow() — Calculates a power. Console.WriteLine(Math.Pow(2, 10)); // 1024 (2 to the power of 10) Console.WriteLine(Math.Pow(3, 3)); // 27 (3 to the power of 3) Console.WriteLine(Math.Pow(9, 0.5)); // 3 (9 to the power of 0.5 = square root) // Math.Sqrt() — Calculates a square root. Console.WriteLine(Math.Sqrt(16)); // 4 Console.WriteLine(Math.Sqrt(2)); // 1.4142135623730951 Console.WriteLine(Math.Sqrt(-1)); // NaN (square root of a negative number is not real) // Math.Log() — Calculates a logarithm. Console.WriteLine(Math.Log(Math.E)); // 1 (natural log: log_e(e) = 1) Console.WriteLine(Math.Log10(1000)); // 3 (common log: log_10(1000) = 3) Console.WriteLine(Math.Log(8, 2)); // 3 (log_2(8) = 3)
This produces the following output:
dotnet run 20 -3 3.7 10 -5 1024 27 3 4 1.4142135623730951 NaN 1 3 3
Sample Code: Practical Use (Compound Interest and Clamping)
The following example shows practical uses of Pow for compound interest and Max/Min for clamping a value within a range.
Compound.cs
using System;
// Compound interest: principal × (1 + rate)^years
double principal = 100000;
double rate = 0.03;
int years = 10;
double futureValue = principal * Math.Pow(1 + rate, years);
Console.WriteLine($"{years} years later: {futureValue:F0}");
// Clamp a score to the range 0–100
int score = 115;
int clamped = Math.Max(0, Math.Min(100, score));
Console.WriteLine($"Clamped: {clamped}");
This produces the following output:
dotnet run 10 years later: 134392 Clamped: 100
Common Mistakes
Common Mistake: Math.Sqrt() Returns NaN for Negative Numbers, Not an Exception
Passing a negative value to Math.Sqrt() does not throw an exception — it returns NaN (Not a Number). Any subsequent calculation involving NaN also yields NaN, making the source of the bug difficult to trace. Always check the value before passing it in.
using System; // NG: negative input returns NaN silently double result = Math.Sqrt(-4); Console.WriteLine(result); // NaN Console.WriteLine(result + 10); // NaN (any calculation with NaN is NaN) Console.WriteLine(result == result); // False (NaN is not equal to itself)
The corrected version looks like this:
using System;
// OK: check the value before calling Sqrt
double value = -4;
if (value >= 0)
{
Console.WriteLine(Math.Sqrt(value));
}
else
{
Console.WriteLine("Cannot take the square root of a negative number.");
}
This produces the following output:
dotnet run NaN NaN False Cannot take the square root of a negative number.
Notes
The return value of Math.Pow() and Math.Sqrt() is always of type double. An explicit cast is required to assign the result to an integer variable. Note that passing a negative value to Math.Sqrt() returns NaN (Not a Number) rather than throwing an exception.
Math.Max() and Math.Min() can only compare two values at a time. To find the maximum or minimum value of an entire array or list, use LINQ's Max() / Min(). For absolute values and rounding, see Math.Abs() / Round() / Ceiling() / Floor().
If you find any errors or copyright issues, please contact us.