pow

Compute the power of two integral numbers.

  1. Unqual!F pow(F x, G n)
  2. typeof(Unqual!(F).init * Unqual!(G).init) pow(F x, G n)
    @nogc @trusted pure nothrow
    typeof(Unqual!(F).init * Unqual!(G).init)
    pow
    (
    F
    G
    )
    (
    F x
    ,
    G n
    )
    if ()
  3. real pow(I x, F y)
  4. Unqual!(Largest!(F, G)) pow(F x, G y)

Parameters

x F

base

n G

exponent

Return Value

Type: typeof(Unqual!(F).init * Unqual!(G).init)

x raised to the power of n. If n is negative the result is 1 / pow(x, -n), which is calculated as integer division with remainder. This may result in a division by zero error.

If both x and n are 0, the result is 1.

Throws

If x is 0 and n is negative, the result is the same as the result of a division by zero.

Examples

1 assert(pow(2, 3) == 8);
2 assert(pow(3, 2) == 9);
3 
4 assert(pow(2, 10) == 1_024);
5 assert(pow(2, 20) == 1_048_576);
6 assert(pow(2, 30) == 1_073_741_824);
7 
8 assert(pow(0, 0) == 1);
9 
10 assert(pow(1, -5) == 1);
11 assert(pow(1, -6) == 1);
12 assert(pow(-1, -5) == -1);
13 assert(pow(-1, -6) == 1);
14 
15 assert(pow(-2, 5) == -32);
16 assert(pow(-2, -5) == 0);
17 assert(pow(cast(double) -2, -5) == -0.03125);

Meta

Suggestion Box / Bug Report