Math_object.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Copyright (c) ChakraCore Project Contributors. All rights reserved.
  4. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  5. //-------------------------------------------------------------------------------------------------------
  6. "use strict";
  7. (function (intrinsic) {
  8. var platform = intrinsic.JsBuiltIn;
  9. __chakraLibrary.positiveInfinity = platform.POSITIVE_INFINITY;
  10. __chakraLibrary.negativeInfinity = platform.NEGATIVE_INFINITY;
  11. platform.registerFunction('min', function (value1, value2) {
  12. // #sec-math.min
  13. // If no arguments are given, the result is positive infinity
  14. // If any value is NaN, the result is NaN.
  15. // The comparison of values to determine the smallest value is done using the Abstract Relational Comparison algorithm except that +0 is considered to be larger than -0.
  16. if (arguments.length === 0) {
  17. return __chakraLibrary.positiveInfinity;
  18. }
  19. let returnNaN = false;
  20. value1 = +value1;
  21. if (value1 !== value1) {
  22. returnNaN = true;
  23. }
  24. if (arguments.length === 1) {
  25. return value1;
  26. }
  27. if (arguments.length === 2) {
  28. value2 = +value2;
  29. if (value2 !== value2 || returnNaN) {
  30. return NaN;
  31. }
  32. if ((value1 < value2) || (value1 === value2 && value1 === 0 && 1 / value1 < 1 / value2)) { // checks for -0 and +0
  33. return value1;
  34. }
  35. else {
  36. return value2;
  37. }
  38. }
  39. let min = value1;
  40. let nextVal;
  41. for (let i = 1; i < arguments.length; i++) {
  42. nextVal = +arguments[i]; // Force conversion for all args (ensure call to valueOf)
  43. if (returnNaN) { } // Skip check if possible
  44. else if (nextVal !== nextVal) {
  45. returnNaN = true;
  46. min = NaN;
  47. }
  48. else if ((min > nextVal) || (min === nextVal && min === 0 && 1 / min > 1 / nextVal)) { // checks for -0 and +0
  49. min = nextVal;
  50. }
  51. }
  52. return min;
  53. });
  54. platform.registerFunction('max', function (value1, value2) {
  55. // #sec-math.max
  56. // If no arguments are given, the result is negative infinity
  57. // If any value is NaN, the result is NaN.
  58. // The comparison of values to determine the largest value is done using the Abstract Relational Comparison algorithm except that +0 is considered to be larger than -0.
  59. if (arguments.length === 0) {
  60. return __chakraLibrary.negativeInfinity;
  61. }
  62. let returnNaN = false;
  63. value1 = +value1;
  64. if (value1 !== value1) {
  65. returnNaN = true;
  66. }
  67. if (arguments.length === 1) {
  68. return value1;
  69. }
  70. if (arguments.length === 2) {
  71. value2 = +value2;
  72. if (value2 !== value2 || returnNaN) {
  73. return NaN;
  74. }
  75. if ((value1 > value2) || (value1 === value2 && value1 === 0 && 1 / value1 > 1 / value2)) { // checks for -0 and +0
  76. return value1;
  77. }
  78. else {
  79. return value2;
  80. }
  81. }
  82. let max = value1;
  83. let nextVal;
  84. for (let i = 1; i < arguments.length; i++) {
  85. nextVal = +arguments[i]; // Force conversion for all args (ensure call to valueOf)
  86. if (returnNaN) { } // Skip check if possible
  87. else if (nextVal !== nextVal) {
  88. returnNaN = true;
  89. max = NaN;
  90. }
  91. else if ((max < nextVal) || (max === nextVal && max === 0 && 1 / max < 1 / nextVal)) { // checks for -0 and +0
  92. max = nextVal;
  93. }
  94. }
  95. return max;
  96. });
  97. });