round.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. var FAILED = false;
  6. // check rounding of NaN
  7. checknan(Math.round(NaN), "Math.round(NaN)");
  8. checknan(Math.round(Math.asin(2.0)), "Math.round(Math.asin(2.0))");
  9. // check rounding of Infinity
  10. check(Infinity, Math.round(Infinity), "Math.round(Infinity)");
  11. check(-Infinity, Math.round(-Infinity), "Math.round(-Infinity)");
  12. // check positive and negative 0
  13. //check(0, Math.round(+0), "Math.round(+0)");
  14. check(-0, Math.round(-0), "Math.round(-0)");
  15. // check various values between 0 and 0.5
  16. check(0, Math.round(4.9999999999999994000e-001), "round largest value < 0.5"); // for ES5 the result is 0
  17. check(0, Math.round(4.9999999999999989000e-001), "round 2nd largest value < 0.5");
  18. check(0, Math.round(4.9406564584124654000e-324), "round smallest value > 0");
  19. check(0, Math.round(9.8813129168249309000e-324), "round 2nd smallest value > 0");
  20. for(var i = 0.001; i < 0.5; i += 0.001)
  21. {
  22. check(0, Math.round(i), "round " + i);
  23. }
  24. // check various values between -0.5 and 0
  25. checkisnegativezero(Math.round(-4.9406564584124654000e-324), "round most positive value < 0");
  26. checkisnegativezero(Math.round(-9.8813129168249309000e-324), "round 2nd most positive value < 0");
  27. checkisnegativezero(Math.round(-4.9999999999999994000e-001), "round most negative value > -0.5");
  28. checkisnegativezero(Math.round(-4.9999999999999989000e-001), "round 2nd most negative value > -0.5");
  29. checkisnegativezero(Math.round(-0), "round -0 should be -0");
  30. for(var i = -0.001; i > -0.5; i -= 0.001)
  31. {
  32. checkisnegativezero(Math.round(i), "round " + i);
  33. }
  34. // check various integers
  35. check(1, Math.round(1), "round 1");
  36. check(2, Math.round(2), "round 2");
  37. check(-1, Math.round(-1), "round -1");
  38. check(-2, Math.round(-2), "round -2");
  39. check(4294967295, Math.round(4294967295), "round 4294967295");
  40. check(4294967296, Math.round(4294967296), "round 4294967296");
  41. check(-4294967296, Math.round(-4294967296), "round -4294967296");
  42. for(var i = 1000; i < 398519; i += 179)
  43. {
  44. check(i, Math.round(i), "round " + i);
  45. }
  46. for(var i = 0.001; i <= 0.5; i += 0.001)
  47. {
  48. check(1, Math.round(0.5 + i), "round " + (0.5+i));
  49. }
  50. for(var i = -0.001; i >= -0.5; i -= 0.001)
  51. {
  52. check(-1, Math.round(-0.5 + i), "round " + (-0.5+i));
  53. }
  54. // check I + 0.5
  55. check(1, Math.round(0.5), "round 0.5");
  56. check(2, Math.round(1.5), "round 1.5");
  57. check(3, Math.round(2.5), "round 2.5");
  58. check(4294967296, Math.round(4294967295 + 0.5), "round 4294967295.5");
  59. for(var i = -100000; i <= 100000; i += 100)
  60. {
  61. check(i+1, Math.round(i + 0.5), "round " + (i+0.5));
  62. }
  63. // miscellaneous other real numbers
  64. check(30593859183, Math.round(30593859183.3915898), "round a double with high precision");
  65. check(1, Math.round(5.0000000000000011000e-001), "round smallest value > 0.5");
  66. check(1, Math.round(5.0000000000000022000e-001), "round 2nd smallest value > 0.5");
  67. check(1.7976931348623157000e+308, Math.round(1.7976931348623157000e+308), "round largest number < Infinity");
  68. check(1.7976931348623155000e+308, Math.round(1.7976931348623155000e+308), "round 2nd largest number < Infinity");
  69. check(-1.7976931348623157000e+308, Math.round(-1.7976931348623157000e+308), "round least positive number > -Infinity");
  70. check(-1.7976931348623155000e+308, Math.round(-1.7976931348623155000e+308), "round 2nd least positive number > -Infinity");
  71. +// if x <= -2^52 or x >= 2^52, Math.round(x) == x
  72. +check(4503599627370496, Math.round(4503599627370496), "round 4503599627370496");
  73. +check(4503599627370497, Math.round(4503599627370497), "round 4503599627370497");
  74. +check(4503599627370548, Math.round(4503599627370548), "round 4503599627370548");
  75. +check(9007199254740991, Math.round(9007199254740991), "round 9007199254740991");
  76. +check(-4503599627370496, Math.round(-4503599627370496), "round -4503599627370496");
  77. +check(-4503599627370497, Math.round(-4503599627370497), "round -4503599627370497");
  78. +check(-4503599627370548, Math.round(-4503599627370548), "round -4503599627370548");
  79. +check(-9007199254740991, Math.round(-9007199254740991), "round -9007199254740991");
  80. // values around INT_MIN and INT_MAX for amd64 (Bug 179932)
  81. function foo(b)
  82. {
  83. var round = Math.round(b);
  84. if(round <= 2147483647)
  85. {
  86. FAILED = true;
  87. }
  88. }
  89. foo(2147483648);
  90. function bar(b)
  91. {
  92. var round = Math.round(b);
  93. if(round >= -2147483648)
  94. {
  95. FAILED = true;
  96. }
  97. }
  98. bar(-2147483649);
  99. if (!FAILED)
  100. {
  101. WScript.Echo("Passed");
  102. }
  103. function check(x, y, str)
  104. {
  105. if(x != y)
  106. {
  107. FAILED = true;
  108. WScript.Echo("fail: " + str);
  109. }
  110. }
  111. function checkisnegativezero(x, str)
  112. {
  113. // this is a quick way to check if a number is -0
  114. if(x != 0 || 1/x >= 0)
  115. {
  116. FAILED = true;
  117. WScript.Echo("fail: " + str);
  118. }
  119. }
  120. function checknan(x, str)
  121. {
  122. if(!isNaN(x))
  123. {
  124. FAILED = true;
  125. WScript.Echo("fail: " + str);
  126. }
  127. }