Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

C# Dictionary

  1. Home
  2. C# Dictionary
  3. Math.Max() / Math.Min() / Math.Pow() / Math.Sqrt()

Math.Max() / Math.Min() / Math.Pow() / Math.Sqrt()

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;

// Returns the larger of val1 and val2.
Math.Max(T val1, T val2)

// Returns the smaller of val1 and 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)

// Returns the square root of d. The return type is double.
Math.Sqrt(double d)

// Returns the natural logarithm of x.
Math.Log(double x)

// Returns the logarithm of x in the specified base newBase.
Math.Log(double x, double newBase)

Method List

MethodDescription
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

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 a real number)

// 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)

// Practical example: 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} yen");

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 .