jsmath.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "jsi.h"
  2. #if defined(_MSC_VER) && (_MSC_VER < 1700) /* VS2012 has stdint.h */
  3. typedef unsigned int uint32_t;
  4. typedef unsigned __int64 uint64_t;
  5. #else
  6. #include <stdint.h>
  7. #endif
  8. #include <time.h>
  9. static double jsM_round(double x)
  10. {
  11. if (isnan(x)) return x;
  12. if (isinf(x)) return x;
  13. if (x == 0) return x;
  14. if (x > 0 && x < 0.5) return 0;
  15. if (x < 0 && x >= -0.5) return -0;
  16. return floor(x + 0.5);
  17. }
  18. static void Math_abs(js_State *J)
  19. {
  20. js_pushnumber(J, fabs(js_tonumber(J, 1)));
  21. }
  22. static void Math_acos(js_State *J)
  23. {
  24. js_pushnumber(J, acos(js_tonumber(J, 1)));
  25. }
  26. static void Math_asin(js_State *J)
  27. {
  28. js_pushnumber(J, asin(js_tonumber(J, 1)));
  29. }
  30. static void Math_atan(js_State *J)
  31. {
  32. js_pushnumber(J, atan(js_tonumber(J, 1)));
  33. }
  34. static void Math_atan2(js_State *J)
  35. {
  36. double y = js_tonumber(J, 1);
  37. double x = js_tonumber(J, 2);
  38. js_pushnumber(J, atan2(y, x));
  39. }
  40. static void Math_ceil(js_State *J)
  41. {
  42. js_pushnumber(J, ceil(js_tonumber(J, 1)));
  43. }
  44. static void Math_cos(js_State *J)
  45. {
  46. js_pushnumber(J, cos(js_tonumber(J, 1)));
  47. }
  48. static void Math_exp(js_State *J)
  49. {
  50. js_pushnumber(J, exp(js_tonumber(J, 1)));
  51. }
  52. static void Math_floor(js_State *J)
  53. {
  54. js_pushnumber(J, floor(js_tonumber(J, 1)));
  55. }
  56. static void Math_log(js_State *J)
  57. {
  58. js_pushnumber(J, log(js_tonumber(J, 1)));
  59. }
  60. static void Math_pow(js_State *J)
  61. {
  62. double x = js_tonumber(J, 1);
  63. double y = js_tonumber(J, 2);
  64. if (!isfinite(y) && fabs(x) == 1)
  65. js_pushnumber(J, NAN);
  66. else
  67. js_pushnumber(J, pow(x,y));
  68. }
  69. static void Math_random(js_State *J)
  70. {
  71. /* Lehmer generator with a=48271 and m=2^31-1 */
  72. /* Park & Miller (1988). Random Number Generators: Good ones are hard to find. */
  73. J->seed = (uint64_t) J->seed * 48271 % 0x7fffffff;
  74. js_pushnumber(J, (double) J->seed / 0x7fffffff);
  75. }
  76. static void Math_init_random(js_State *J)
  77. {
  78. /* Pick initial seed by scrambling current time with Xorshift. */
  79. /* Marsaglia (2003). Xorshift RNGs. */
  80. J->seed = time(0) + 123;
  81. J->seed ^= J->seed << 13;
  82. J->seed ^= J->seed >> 17;
  83. J->seed ^= J->seed << 5;
  84. J->seed %= 0x7fffffff;
  85. }
  86. static void Math_round(js_State *J)
  87. {
  88. double x = js_tonumber(J, 1);
  89. js_pushnumber(J, jsM_round(x));
  90. }
  91. static void Math_sin(js_State *J)
  92. {
  93. js_pushnumber(J, sin(js_tonumber(J, 1)));
  94. }
  95. static void Math_sqrt(js_State *J)
  96. {
  97. js_pushnumber(J, sqrt(js_tonumber(J, 1)));
  98. }
  99. static void Math_tan(js_State *J)
  100. {
  101. js_pushnumber(J, tan(js_tonumber(J, 1)));
  102. }
  103. static void Math_max(js_State *J)
  104. {
  105. int i, n = js_gettop(J);
  106. double x = -INFINITY;
  107. for (i = 1; i < n; ++i) {
  108. double y = js_tonumber(J, i);
  109. if (isnan(y)) {
  110. x = y;
  111. break;
  112. }
  113. if (signbit(x) == signbit(y))
  114. x = x > y ? x : y;
  115. else if (signbit(x))
  116. x = y;
  117. }
  118. js_pushnumber(J, x);
  119. }
  120. static void Math_min(js_State *J)
  121. {
  122. int i, n = js_gettop(J);
  123. double x = INFINITY;
  124. for (i = 1; i < n; ++i) {
  125. double y = js_tonumber(J, i);
  126. if (isnan(y)) {
  127. x = y;
  128. break;
  129. }
  130. if (signbit(x) == signbit(y))
  131. x = x < y ? x : y;
  132. else if (signbit(y))
  133. x = y;
  134. }
  135. js_pushnumber(J, x);
  136. }
  137. void jsB_initmath(js_State *J)
  138. {
  139. Math_init_random(J);
  140. js_pushobject(J, jsV_newobject(J, JS_CMATH, J->Object_prototype));
  141. {
  142. jsB_propn(J, "E", 2.7182818284590452354);
  143. jsB_propn(J, "LN10", 2.302585092994046);
  144. jsB_propn(J, "LN2", 0.6931471805599453);
  145. jsB_propn(J, "LOG2E", 1.4426950408889634);
  146. jsB_propn(J, "LOG10E", 0.4342944819032518);
  147. jsB_propn(J, "PI", 3.1415926535897932);
  148. jsB_propn(J, "SQRT1_2", 0.7071067811865476);
  149. jsB_propn(J, "SQRT2", 1.4142135623730951);
  150. jsB_propf(J, "Math.abs", Math_abs, 1);
  151. jsB_propf(J, "Math.acos", Math_acos, 1);
  152. jsB_propf(J, "Math.asin", Math_asin, 1);
  153. jsB_propf(J, "Math.atan", Math_atan, 1);
  154. jsB_propf(J, "Math.atan2", Math_atan2, 2);
  155. jsB_propf(J, "Math.ceil", Math_ceil, 1);
  156. jsB_propf(J, "Math.cos", Math_cos, 1);
  157. jsB_propf(J, "Math.exp", Math_exp, 1);
  158. jsB_propf(J, "Math.floor", Math_floor, 1);
  159. jsB_propf(J, "Math.log", Math_log, 1);
  160. jsB_propf(J, "Math.max", Math_max, 0); /* 2 */
  161. jsB_propf(J, "Math.min", Math_min, 0); /* 2 */
  162. jsB_propf(J, "Math.pow", Math_pow, 2);
  163. jsB_propf(J, "Math.random", Math_random, 0);
  164. jsB_propf(J, "Math.round", Math_round, 1);
  165. jsB_propf(J, "Math.sin", Math_sin, 1);
  166. jsB_propf(J, "Math.sqrt", Math_sqrt, 1);
  167. jsB_propf(J, "Math.tan", Math_tan, 1);
  168. }
  169. js_defglobal(J, "Math", JS_DONTENUM);
  170. }