MathLib.pow()

MathLib.pow calculates \[ \text{MathLib.pow}(x, n) = x^n \]

Examples

Let's say you want to write the first few square numbers in the console. This can be done by the following code: [1,2,3,4,5,6,7,8,9,10].forEach(function (x) { console.log(MathLib.pow(x, 2)); });

Specification

MathLib.pow is handling the special cases as defined by the IEEE 754-2008 standard:
  1. MathLib.pow (x, ±0) = 1 (for any x, even a zero, NaN, or ±∞)
  2. MathLib.pow (±0, y) = ±∞ (for y an odd integer < 0)
  3. MathLib.pow(±0, -∞) = +∞.
  4. MathLib.pow(±0, +∞) = +0.
  5. MathLib.pow (±0, y) = +∞ (for finite y < 0 and not an odd integer)
  6. MathLib.pow (±0, y) = ±0 (for finite y > 0 an odd integer)
  7. MathLib.pow (±0, y) = +0 (for finite y > 0 and not an odd integer)
  8. MathLib.pow(-1, ±∞) = 1[1]
  9. MathLib.pow(+1, y) = 1 (for any y, even ±∞ and NaN)[2]
  10. MathLib.pow (x, y) = NaN (for finite x < 0 and finite non-integer y.)
  11. MathLib.pow(x, +∞) = +∞ (for |x| > 1)[3]
  12. MathLib.pow(x, -∞) = +0 (for |x| > 1)[3]
  13. MathLib.pow(x, +∞) = +0 (for |x| < 1)[3]
  14. MathLib.pow(x, -∞) = +∞ (for |x| < 1)[3]
  15. MathLib.pow(+∞, y) = +∞ (for y > 0)[3]
  16. MathLib.pow(+∞, y) = +0 (for y < 0)[3]
  17. MathLib.pow(-∞, y) = MathLib.pow(-0, -y)[3]
  18. MathLib.pow(NaN, y) = NaN (for all y except ±0)[3]
  19. MathLib.pow(x, NaN) = NaN (for all x except +1)[3]
  20. otherwise MathLib.pow(x, n) = $x^n$

[1]This is NaN in JavaScript.

[2]This is NaN in JavaScript for y being ±∞ or NaN.

[3]This specification is not in the IEEE standard (the standard does not handle these cases). MathLib uses the same results as Math.pow.

Implementation

As JavaScript isn't using the IEEE 754-2008 standard, there are some special cases where MathLib and JavaScript differ. MathLib.pow is checking for these differences. In all other cases the JavaScript function Math.pow is used.

Tests