View source code
Display the source code in std/math.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

std.math.pow - multiple declarations

Function pow

Compute the value of x n, where n is an integer

Unqual!F pow(F, G) (
  F x,
  G n
) pure nothrow @nogc @trusted
if (isFloatingPoint!F && isIntegral!G);

Example

writeln(pow(2.0, 5)); // 32.0
assert(pow(1.5, 9).feqrel(38.4433) > 16);
assert(pow(real.nan, 2) is real.nan);
writeln(pow(real.infinity, 2)); // real.infinity

Function pow

Compute the value of an integer x, raised to the power of a positive integer n.

typeof(Unqual!F.init*Unqual!G.init) pow(F, G) (
  F x,
  G n
) pure nothrow @nogc @trusted
if (isIntegral!F && isIntegral!G);

If both x and n are 0, the result is 1. If n is negative, an integer divide error will occur at runtime, regardless of the value of x.

Example

immutable int one = 1;
immutable byte two = 2;
immutable ubyte three = 3;
immutable short four = 4;
immutable long ten = 10;

writeln(pow(two, three)); // 8
writeln(pow(two, ten)); // 1024
writeln(pow(one, ten)); // 1
writeln(pow(ten, four)); // 10_000
writeln(pow(four, 10)); // 1_048_576
writeln(pow(three, four)); // 81

Function pow

Computes integer to floating point powers.

real pow(I, F) (
  I x,
  F y
) pure nothrow @nogc @trusted
if (isIntegral!I && isFloatingPoint!F);

Example

writeln(pow(2, 5.0)); // 32.0
writeln(pow(7, 3.0)); // 343.0
assert(pow(2, real.nan) is real.nan);
writeln(pow(2, real.infinity)); // real.infinity

Function pow

Calculates xy.

Unqual!(Largest!(F,G)) pow(F, G) (
  F x,
  G y
) pure nothrow @nogc @trusted
if (isFloatingPoint!F && isFloatingPoint!G);

Special Values
x y pow(x, y) div 0 invalid?
anything ±0.0 1.0 no no
|x| > 1 +∞ +∞ no no
|x| < 1 +∞ +0.0 no no
|x| > 1 -∞ +0.0 no no
|x| < 1 -∞ +∞ no no
+∞ > 0.0 +∞ no no
+∞ < 0.0 +0.0 no no
-∞ odd integer > 0.0 -∞ no no
-∞ > 0.0, not odd integer +∞ no no
-∞ odd integer < 0.0 -0.0 no no
-∞ < 0.0, not odd integer +0.0 no no
±1.0 ±∞ -NAN no yes
< 0.0 finite, nonintegral NAN no yes
±0.0 odd integer < 0.0 ±∞ yes no
±0.0 < 0.0, not odd integer +∞ yes no
±0.0 odd integer > 0.0 ±0.0 no no
±0.0 > 0.0, not odd integer +0.0 no no

Example

writeln(pow(1.0, 2.0)); // 1.0
writeln(pow(0.0, 0.0)); // 1.0
assert(pow(1.5, 10.0).feqrel(57.665) > 16);

// special values
writeln(pow(1.5, real.infinity)); // real.infinity
writeln(pow(0.5, real.infinity)); // 0.0
writeln(pow(1.5, -real.infinity)); // 0.0
writeln(pow(0.5, -real.infinity)); // real.infinity
writeln(pow(real.infinity, 1.0)); // real.infinity
writeln(pow(real.infinity, -1.0)); // 0.0
writeln(pow(-real.infinity, 1.0)); // -real.infinity
writeln(pow(-real.infinity, 2.0)); // real.infinity
writeln(pow(-real.infinity, -1.0)); // -0.0
writeln(pow(-real.infinity, -2.0)); // 0.0
assert(pow(1.0, real.infinity) is -real.nan);
writeln(pow(0.0, -1.0)); // real.infinity
writeln(pow(real.nan, 0.0)); // 1.0

Authors

Walter Bright, Don Clugston, Conversion of CEPHES math library to D by Iain Buclaw and David Nadlinger

License

Boost License 1.0.