System.Math.Pow 方法

方法描述

返回指定数字的指定次幂。

语法定义(C# System.Math.Pow 方法 的用法)

public static double Pow(
	double x,
	double y
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
x System-Double 要乘幂的双精度浮点数。
y System-Double 指定幂的双精度浮点数。
返回值 System.Double 数字 x 的 y 次幂。

提示和注释

下表显示了为 x 和 y 参数指定不同的值或值范围时的返回值。 有关更多信息,请参见Double.PositiveInfinity、Double.NegativeInfinity和Double.NaN。

参数

返回值

x 或 y = NaN。

NaN

x = 除 NaN 以外的任何值;y = 0。

1

x = NegativeInfinity;y < 0。

0

x = NegativeInfinity;y 为正奇数。

NegativeInfinity

x = NegativeInfinity;y 为正,但不为奇数。

PositiveInfinity

x < 0 但不为 NegativeInfinity;y 不为整数、NegativeInfinity 或 PositiveInfinity。

NaN

x = -1;y = NegativeInfinity 或 PositiveInfinity。

NaN

-1 < x < 1;y = NegativeInfinity。

PositiveInfinity

-1 < x < 1;y = PositiveInfinity。

0

x < -1 或 x > 1;y = NegativeInfinity。

0

x < -1 或 x > 1;y = PositiveInfinity。

PositiveInfinity

x = 0;y < 0。

PositiveInfinity

x = 0;y > 0。

0

x = 1;y 为 NaN 以外的任何值。

1

x = PositiveInfinity;y < 0。

0

x = PositiveInfinity;y > 0。

PositiveInfinity

System.Math.Pow 方法例子

下面的示例使用 Pow 方法计算 2 的 n 次幂(n 的范围为 0 至 32)。

using System;

public class Example
{
   public static void Main()
   {
      int value = 2;
      for (int power = 0; power <= 32; power++)
         Console.WriteLine("{0}^{1} = {2:N0} (0x{2:X})", 
                           value, power, (long)Math.Pow(value, power));
   }
}
// The example displays the following output:
//       2^0 = 1 (0x1)
//       2^1 = 2 (0x2)
//       2^2 = 4 (0x4)
//       2^3 = 8 (0x8)
//       2^4 = 16 (0x10)
//       2^5 = 32 (0x20)
//       2^6 = 64 (0x40)
//       2^7 = 128 (0x80)
//       2^8 = 256 (0x100)
//       2^9 = 512 (0x200)
//       2^10 = 1,024 (0x400)
//       2^11 = 2,048 (0x800)
//       2^12 = 4,096 (0x1000)
//       2^13 = 8,192 (0x2000)
//       2^14 = 16,384 (0x4000)
//       2^15 = 32,768 (0x8000)
//       2^16 = 65,536 (0x10000)
//       2^17 = 131,072 (0x20000)
//       2^18 = 262,144 (0x40000)
//       2^19 = 524,288 (0x80000)
//       2^20 = 1,048,576 (0x100000)
//       2^21 = 2,097,152 (0x200000)
//       2^22 = 4,194,304 (0x400000)
//       2^23 = 8,388,608 (0x800000)
//       2^24 = 16,777,216 (0x1000000)
//       2^25 = 33,554,432 (0x2000000)
//       2^26 = 67,108,864 (0x4000000)
//       2^27 = 134,217,728 (0x8000000)
//       2^28 = 268,435,456 (0x10000000)
//       2^29 = 536,870,912 (0x20000000)
//       2^30 = 1,073,741,824 (0x40000000)
//       2^31 = 2,147,483,648 (0x80000000)
//       2^32 = 4,294,967,296 (0x100000000)

异常

异常 异常描述

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。