ceilfloor.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. // interesting floating point limits
  6. check(NaN, NaN);
  7. check(+0, +0);
  8. check(-0, -0);
  9. check(+Infinity, +Infinity);
  10. check(-Infinity, -Infinity);
  11. // values abs(x) < 1
  12. check(-0, -4.9406564584124654000e-324);
  13. check(-0, -9.8813129168249309000e-324);
  14. check(-0, -0.5);
  15. check(-0, -9.9999999999999989000e-001);
  16. check(-0, -9.9999999999999978000e-001);
  17. check(-1, -1);
  18. check(1, 4.9406564584124654000e-324);
  19. check(1, 9.8813129168249309000e-324);
  20. check(1, 0.5);
  21. check(1, 9.9999999999999989000e-001);
  22. check(1, 9.9999999999999978000e-001);
  23. check(1, 1);
  24. // other interesting double values
  25. var x = 1;
  26. for(var i = 0; i < 50; ++i)
  27. {
  28. check(x, x - 0.1);
  29. check(-x + 1, -x + 0.1);
  30. x = x * 2;
  31. }
  32. check(54, 53.7);
  33. check(112233581321, 112233581320.001);
  34. // values around the maximums
  35. check(1.7976931348623157000e+308, 1.7976931348623157000e+308);
  36. check(-1.7976931348623157000e+308, -1.7976931348623157000e+308)
  37. // values around INT_MIN and INT_MAX for amd64 (Bug 179932)
  38. function foo(b)
  39. {
  40. //Its okay to check only for ceil as correctness tests for floor are already here and floor and ceil will have the same value for the parameter passed for this test
  41. var ceil = Math.ceil(b);
  42. if(ceil <= 2147483647)
  43. return "fail";
  44. return "pass";
  45. }
  46. WScript.Echo(foo(2147483648));
  47. function bar(b)
  48. {
  49. //Its okay to check only for ceil as correctness tests for floor are already here and floor and ceil will have the same value for the parameter passed for this test
  50. var ceil = Math.ceil(b);
  51. if(ceil >= -2147483648)
  52. return "fail";
  53. return "pass";
  54. }
  55. WScript.Echo(bar(-2147483649));
  56. WScript.Echo("done");
  57. function check(result, n)
  58. {
  59. if(!isNaN(n))
  60. {
  61. if(Math.ceil(n) != result)
  62. {
  63. WScript.Echo("ceil(" + n + ") != " + result);
  64. }
  65. if(-Math.floor(-n) != result)
  66. {
  67. WScript.Echo("floor(" + (-n) + ") != " + (-result));
  68. }
  69. }
  70. else
  71. {
  72. if(!isNaN(Math.ceil(n)) || !isNaN(-Math.floor(-n)))
  73. {
  74. WScript.Echo("error with ceil/floor of NaNs");
  75. }
  76. }
  77. }
  78. Verify("Math.ceil around negative 0", -Infinity, 1/Math.ceil(-0.1));
  79. Verify("Math.floor around negative 0", -Infinity, 1/Math.floor(-0));
  80. function Verify(test, expected, actual) {
  81. if (expected === actual) {
  82. WScript.Echo("PASSED: " + test);
  83. }
  84. else {
  85. WScript.Echo("FAILED: " + test + " Expected:" + expected + "Actual:" + actual);
  86. }
  87. }