exponentiate.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. // var isNegativeZero = require('is-negative-zero');
  4. var $pow = GetIntrinsic('%Math.pow%');
  5. var $TypeError = require('es-errors/type');
  6. /*
  7. var abs = require('../../helpers/abs');
  8. var isFinite = require('../../helpers/isFinite');
  9. var isNaN = require('../../helpers/isNaN');
  10. var IsInteger = require('../IsInteger');
  11. */
  12. // https://262.ecma-international.org/11.0/#sec-numeric-types-number-exponentiate
  13. /* eslint max-lines-per-function: 0, max-statements: 0 */
  14. module.exports = function NumberExponentiate(base, exponent) {
  15. if (typeof base !== 'number' || typeof exponent !== 'number') {
  16. throw new $TypeError('Assertion failed: `base` and `exponent` arguments must be Numbers');
  17. }
  18. return $pow(base, exponent);
  19. /*
  20. if (isNaN(exponent)) {
  21. return NaN;
  22. }
  23. if (exponent === 0) {
  24. return 1;
  25. }
  26. if (isNaN(base)) {
  27. return NaN;
  28. }
  29. var aB = abs(base);
  30. if (aB > 1 && exponent === Infinity) {
  31. return Infinity;
  32. }
  33. if (aB > 1 && exponent === -Infinity) {
  34. return 0;
  35. }
  36. if (aB === 1 && (exponent === Infinity || exponent === -Infinity)) {
  37. return NaN;
  38. }
  39. if (aB < 1 && exponent === Infinity) {
  40. return +0;
  41. }
  42. if (aB < 1 && exponent === -Infinity) {
  43. return Infinity;
  44. }
  45. if (base === Infinity) {
  46. return exponent > 0 ? Infinity : 0;
  47. }
  48. if (base === -Infinity) {
  49. var isOdd = true;
  50. if (exponent > 0) {
  51. return isOdd ? -Infinity : Infinity;
  52. }
  53. return isOdd ? -0 : 0;
  54. }
  55. if (exponent > 0) {
  56. return isNegativeZero(base) ? Infinity : 0;
  57. }
  58. if (isNegativeZero(base)) {
  59. if (exponent > 0) {
  60. return isOdd ? -0 : 0;
  61. }
  62. return isOdd ? -Infinity : Infinity;
  63. }
  64. if (base < 0 && isFinite(base) && isFinite(exponent) && !IsInteger(exponent)) {
  65. return NaN;
  66. }
  67. */
  68. };