Advertisement
historic_bruno

Untitled

Jun 26th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Tests for consistent and correct math results
  3.  */
  4.  
  5.  // +0 is different than -0, but standard equality won't test that
  6. function isNegativeZero(z) { return z === 0 && 1/z === -Infinity; }
  7. function isPositiveZero(z) { return z === 0 && 1/z === Infinity; }
  8.  
  9.  
  10. // rounding
  11. TS_ASSERT_EQUALS(0.1+0.2, 0.30000000000000004);
  12. TS_ASSERT_EQUALS(0.1+0.7+0.3, 1.0999999999999999);
  13.  
  14. // cos
  15. TS_ASSERT_EQUALS(Math.cos(Math.PI/2), 0);
  16. TS_ASSERT_UNEVAL_EQUALS(Math.cos(NaN), NaN);
  17. TS_ASSERT_EQUALS(Math.cos(0), 1);
  18. TS_ASSERT_EQUALS(Math.cos(-0), 1);
  19. TS_ASSERT_UNEVAL_EQUALS(Math.cos(Infinity), NaN);
  20. TS_ASSERT_UNEVAL_EQUALS(Math.cos(-Infinity), NaN);
  21.  
  22. // sin
  23. TS_ASSERT_EQUALS(Math.sin(Math.PI), 0);
  24. TS_ASSERT_UNEVAL_EQUALS(Math.sin(NaN), NaN);
  25. TS_ASSERT(isPositiveZero(Math.sin(0)));
  26. // TS_ASSERT(isNegativeZero(Math.sin(-0))); TODO: doesn't match spec
  27. TS_ASSERT_UNEVAL_EQUALS(Math.sin(Infinity), NaN);
  28. TS_ASSERT_UNEVAL_EQUALS(Math.sin(-Infinity), NaN);
  29.  
  30. // atan
  31. TS_ASSERT_UNEVAL_EQUALS(Math.atan(NaN), NaN);
  32. TS_ASSERT(isPositiveZero(Math.atan(0)));
  33. //TS_ASSERT(isNegativeZero(Math.atan(-0))); TODO: doesn't match spec
  34. TS_ASSERT_EQUALS(Math.atan(Infinity), Math.PI/2);
  35. TS_ASSERT_EQUALS(Math.atan(-Infinity), -Math.PI/2);
  36. TS_ASSERT_EQUALS(Math.atan(1e-310), 1.00000000003903e-310);
  37.  
  38. // atan2
  39. TS_ASSERT_EQUALS(Math.atan2(1e-310, 2), 5.0000000001954e-311);
  40. TS_ASSERT_UNEVAL_EQUALS(Math.atan2(NaN, 1), NaN);
  41. TS_ASSERT_UNEVAL_EQUALS(Math.atan2(1, NaN), NaN);
  42. TS_ASSERT_EQUALS(Math.atan2(1, 0), Math.PI/2);
  43. //TS_ASSERT_EQUALS(Math.atan2(1, -0), -Math.PI/2); TODO: doesn't match spec
  44. TS_ASSERT(isPositiveZero(Math.atan2(0, 1)));
  45. TS_ASSERT(isPositiveZero(Math.atan2(0, 0)));
  46. TS_ASSERT_EQUALS(Math.atan2(0, -0), Math.PI);
  47. TS_ASSERT_EQUALS(Math.atan2(0, -1), Math.PI);
  48. TS_ASSERT(isNegativeZero(Math.atan2(-0, 1)));
  49. TS_ASSERT(isNegativeZero(Math.atan2(-0, 0)));
  50. TS_ASSERT_EQUALS(Math.atan2(-0, -0), -Math.PI);
  51. TS_ASSERT_EQUALS(Math.atan2(-0, -1), -Math.PI);
  52. TS_ASSERT_EQUALS(Math.atan2(-1, 0), -Math.PI/2);
  53. TS_ASSERT_EQUALS(Math.atan2(-1, -0), -Math.PI/2);
  54. TS_ASSERT(isPositiveZero(Math.atan2(1.7e308, Infinity)));
  55. TS_ASSERT_EQUALS(Math.atan2(1.7e308, -Infinity), Math.PI);
  56. TS_ASSERT(isNegativeZero(Math.atan2(-1.7e308, Infinity)));
  57. TS_ASSERT_EQUALS(Math.atan2(-1.7e308, -Infinity), -Math.PI);
  58. TS_ASSERT_EQUALS(Math.atan2(Infinity, -1.7e308), Math.PI/2);
  59. TS_ASSERT_EQUALS(Math.atan2(-Infinity, 1.7e308), -Math.PI/2);
  60. TS_ASSERT_EQUALS(Math.atan2(Infinity, Infinity), Math.PI/4);
  61. TS_ASSERT_EQUALS(Math.atan2(Infinity, -Infinity), 3*Math.PI/4);
  62. TS_ASSERT_EQUALS(Math.atan2(-Infinity, Infinity), -Math.PI/4);
  63. TS_ASSERT_EQUALS(Math.atan2(-Infinity, -Infinity), -3*Math.PI/4);
  64.  
  65. // pow
  66. TS_ASSERT_EQUALS(Math.pow(Math.PI, -100), 1.9275814160560185e-50);
  67. TS_ASSERT_EQUALS(Math.pow(NaN, 0), 1);
  68. TS_ASSERT_EQUALS(Math.pow(NaN, -0), 1);
  69. TS_ASSERT_UNEVAL_EQUALS(Math.pow(NaN, 100), NaN);
  70. TS_ASSERT_EQUALS(Math.pow(1.7e308, Infinity), Infinity);
  71. TS_ASSERT_EQUALS(Math.pow(-1.7e308, Infinity), Infinity);
  72. TS_ASSERT(isPositiveZero(Math.pow(1.7e308, -Infinity)));
  73. TS_ASSERT(isPositiveZero(Math.pow(-1.7e308, -Infinity)));
  74. TS_ASSERT_UNEVAL_EQUALS(Math.pow(1, Infinity), NaN);
  75. TS_ASSERT_UNEVAL_EQUALS(Math.pow(-1, Infinity), NaN);
  76. TS_ASSERT_UNEVAL_EQUALS(Math.pow(1, -Infinity), NaN);
  77. TS_ASSERT_UNEVAL_EQUALS(Math.pow(-1, -Infinity), NaN);
  78. TS_ASSERT(isPositiveZero(Math.pow(1e-310, Infinity)));
  79. TS_ASSERT(isPositiveZero(Math.pow(-1e-310, Infinity)));
  80. TS_ASSERT_EQUALS(Math.pow(1e-310, -Infinity), Infinity);
  81. TS_ASSERT_EQUALS(Math.pow(-1e-310, -Infinity), Infinity);
  82. TS_ASSERT_EQUALS(Math.pow(Infinity, 1e-310), Infinity);
  83. TS_ASSERT(isPositiveZero(Math.pow(Infinity, -1e-310)));
  84. TS_ASSERT_EQUALS(Math.pow(-Infinity, 101), -Infinity);
  85. TS_ASSERT_EQUALS(Math.pow(-Infinity, 1.7e308), Infinity);
  86. TS_ASSERT(isNegativeZero(Math.pow(-Infinity, -101)));
  87. TS_ASSERT(isPositiveZero(Math.pow(-Infinity, -1.7e308)));
  88. TS_ASSERT(isPositiveZero(Math.pow(0, 1e-310)));
  89. TS_ASSERT_EQUALS(Math.pow(0, -1e-310), Infinity);
  90. TS_ASSERT(isNegativeZero(Math.pow(-0, 101)));
  91. TS_ASSERT(isPositiveZero(Math.pow(-0, 1e-310)));
  92. TS_ASSERT_EQUALS(Math.pow(-0, -101), -Infinity);
  93. TS_ASSERT_EQUALS(Math.pow(-0, -1e-310), Infinity);
  94. TS_ASSERT_UNEVAL_EQUALS(Math.pow(-1.7e308, 1e-310), NaN);
  95.  
  96.  
  97. // exp
  98. TS_ASSERT_EQUALS(Math.exp(10), 22026.465794806707);
  99. TS_ASSERT_UNEVAL_EQUALS(Math.exp(NaN), NaN);
  100. TS_ASSERT_EQUALS(Math.exp(0), 1);
  101. TS_ASSERT_EQUALS(Math.exp(-0), 1);
  102. TS_ASSERT_EQUALS(Math.exp(Infinity), Infinity);
  103. TS_ASSERT(isPositiveZero(Math.exp(-Infinity)));
  104.  
  105. // log
  106. // TODO: more tests
  107. TS_ASSERT_UNEVAL_EQUALS(Math.log("NaN"), NaN);
  108. TS_ASSERT_UNEVAL_EQUALS(Math.log(-1), NaN);
  109. TS_ASSERT_EQUALS(Math.log(0), -Infinity);
  110. TS_ASSERT_EQUALS(Math.log(-0), -Infinity);
  111. TS_ASSERT(isPositiveZero(Math.log(1)));
  112. TS_ASSERT_EQUALS(Math.log(Infinity), Infinity);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement