exponentiate.js 1.7 KB

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