sqstdmath.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /* see copyright notice in squirrel.h */
  2. #include <squirrel.h>
  3. #include <math.h>
  4. //#include <float.h>
  5. #include <limits.h>
  6. #include <stdlib.h>
  7. #include <sqstdmath.h>
  8. /* Some useful constants. */
  9. #ifndef M_E
  10. # define M_E 2.7182818284590452354 /* e */
  11. #endif
  12. #ifndef M_LOG2E
  13. # define M_LOG2E 1.4426950408889634074 /* log_2 e */
  14. #endif
  15. #ifndef M_LOG10E
  16. # define M_LOG10E 0.43429448190325182765 /* log_10 e */
  17. #endif
  18. #ifndef M_LN2
  19. # define M_LN2 0.69314718055994530942 /* log_e 2 */
  20. #endif
  21. #ifndef M_LN10
  22. # define M_LN10 (2.30258509299404568402) /* log_e 10 */
  23. #endif
  24. #ifndef M_PI
  25. # define M_PI (3.14159265358979323846) /* pi */
  26. #endif
  27. #ifndef M_PI_2
  28. # define M_PI_2 (1.57079632679489661923) /* pi/2 */
  29. #endif
  30. #ifndef M_PI_4
  31. # define M_PI_4 (0.78539816339744830962) /* pi/4 */
  32. #endif
  33. #ifndef M_1_PI
  34. # define M_1_PI (0.31830988618379067154) /* 1/pi */
  35. #endif
  36. #ifndef M_2_PI
  37. # define M_2_PI (0.63661977236758134308) /* 2/pi */
  38. #endif
  39. #ifndef M_2_SQRTPI
  40. # define M_2_SQRTPI (1.12837916709551257390) /* 2/sqrt(pi) */
  41. #endif
  42. #ifndef M_SQRT2
  43. # define M_SQRT2 (1.41421356237309504880) /* sqrt(2) */
  44. #endif
  45. #ifndef M_SQRT1_2
  46. # define M_SQRT1_2 (0.70710678118654752440) /* 1/sqrt(2) */
  47. #endif
  48. #define SINGLE_ARG_FUNC(_funcname) static SQRESULT math_##_funcname(HSQUIRRELVM v){ \
  49. SQFloat f; \
  50. sq_getfloat(v,2,&f); \
  51. sq_pushfloat(v,(SQFloat)_funcname(f)); \
  52. return 1; \
  53. }
  54. #define TWO_ARGS_FUNC(_funcname) static SQRESULT math_##_funcname(HSQUIRRELVM v){ \
  55. SQFloat p1,p2; \
  56. sq_getfloat(v,2,&p1); \
  57. sq_getfloat(v,3,&p2); \
  58. sq_pushfloat(v,(SQFloat)_funcname(p1,p2)); \
  59. return 1; \
  60. }
  61. #define BOOL_SINGLE_ARG_FUNC(_funcname) static SQRESULT math_##_funcname(HSQUIRRELVM v){ \
  62. SQFloat f; \
  63. sq_getfloat(v,2,&f); \
  64. sq_pushbool(v,(SQBool)_funcname(f)); \
  65. return 1; \
  66. }
  67. static SQRESULT math_srand(HSQUIRRELVM v)
  68. {
  69. SQInteger i;
  70. if(SQ_FAILED(sq_getinteger(v,2,&i)))
  71. return sq_throwerror(v,_SC("invalid param"));
  72. srand((unsigned int)i);
  73. return 0;
  74. }
  75. static SQRESULT math_rand(HSQUIRRELVM v)
  76. {
  77. sq_pushinteger(v,rand());
  78. return 1;
  79. }
  80. static SQRESULT math_random(HSQUIRRELVM v) {
  81. SQ_FUNC_VARS(v);
  82. SQInteger low, up;
  83. SQFloat r = (SQFloat)rand() * (1.0 / ((SQFloat)RAND_MAX + 1.0));
  84. switch (_top_) { /* check number of arguments */
  85. case 1: { /* no arguments */
  86. sq_pushfloat(v, r); /* Number between 0 and 1 */
  87. return 1;
  88. }
  89. case 2: { /* only upper limit */
  90. low = 1;
  91. SQ_GET_INTEGER_NVD(v, 2, up);
  92. break;
  93. }
  94. case 3: { /* lower and upper limits */
  95. SQ_GET_INTEGER_NVD(v, 2, low);
  96. SQ_GET_INTEGER_NVD(v, 3, up);
  97. break;
  98. }
  99. default: return sq_throwerror(v, _SC("wrong number of arguments"));
  100. }
  101. /* random integer in the interval [low, up] */
  102. if(low >= up) return sq_throwerror(v, _SC("interval is empty"));
  103. if(low <= 0 && up >= SQ_INT_MAX + low)
  104. return sq_throwerror(v, _SC("interval too large"));
  105. r *= (SQFloat)(up - low) + 1.0;
  106. sq_pushinteger(v, (SQInteger)r + low);
  107. return 1;
  108. }
  109. static SQRESULT math_abs(HSQUIRRELVM v)
  110. {
  111. SQInteger n;
  112. sq_getinteger(v,2,&n);
  113. sq_pushinteger(v,(SQInteger)::abs(n));
  114. return 1;
  115. }
  116. static SQRESULT math_log(HSQUIRRELVM v) {
  117. SQ_FUNC_VARS(v);
  118. SQ_GET_FLOAT(v, 2, num);
  119. switch (_top_) { /* check number of arguments */
  120. case 2: { /* only num */
  121. sq_pushfloat(v,log(num));
  122. break;
  123. }
  124. case 3: { /* num and base */
  125. SQ_GET_FLOAT(v, 3, base);
  126. sq_pushfloat(v,log(num)/log(base));
  127. break;
  128. }
  129. }
  130. return 1;
  131. }
  132. SINGLE_ARG_FUNC(sqrt)
  133. SINGLE_ARG_FUNC(fabs)
  134. SINGLE_ARG_FUNC(sin)
  135. SINGLE_ARG_FUNC(asin)
  136. SINGLE_ARG_FUNC(cos)
  137. SINGLE_ARG_FUNC(acos)
  138. //SINGLE_ARG_FUNC(log)
  139. SINGLE_ARG_FUNC(log10)
  140. SINGLE_ARG_FUNC(tan)
  141. SINGLE_ARG_FUNC(atan)
  142. TWO_ARGS_FUNC(atan2)
  143. TWO_ARGS_FUNC(pow)
  144. SINGLE_ARG_FUNC(floor)
  145. SINGLE_ARG_FUNC(ceil)
  146. SINGLE_ARG_FUNC(exp)
  147. SINGLE_ARG_FUNC(acosh)
  148. SINGLE_ARG_FUNC(asinh)
  149. SINGLE_ARG_FUNC(tanh)
  150. SINGLE_ARG_FUNC(atanh)
  151. BOOL_SINGLE_ARG_FUNC(isnan)
  152. BOOL_SINGLE_ARG_FUNC(isfinite)
  153. //DAD start
  154. #include <string.h>
  155. #include <stdio.h>
  156. #include <ctype.h>
  157. SQ_OPT_STRING_STRLEN()
  158. /* mathB_roundf: rounds real value x to the d-th digit; patched Dec 22, 2007 for negative values */
  159. static SQRESULT math_roundf (HSQUIRRELVM v) {
  160. SQ_FUNC_VARS(v);
  161. SQ_GET_FLOAT(v, 2, x);
  162. SQ_OPT_INTEGER(v, 3, dec_places, 0);
  163. SQFloat dec_factor;
  164. if( dec_places>15 || dec_places<0 ) return sq_throwerror(v, _SC("decimal places out of range 0-15"));
  165. switch(dec_places){
  166. case 0: dec_factor = 1.0; break;
  167. case 1: dec_factor = 10.0; break;
  168. case 2: dec_factor = 100.0; break;
  169. case 3: dec_factor = 1000.0; break;
  170. case 4: dec_factor = 10000.0; break;
  171. case 5: dec_factor = 100000.0; break;
  172. case 6: dec_factor = 1000000.0; break;
  173. default: dec_factor = pow((double)10.0, dec_places);
  174. }
  175. if (x < 0)
  176. sq_pushfloat(v, ceil(dec_factor*x-0.5) / dec_factor);
  177. else
  178. sq_pushfloat(v, floor(dec_factor*x+0.5) / dec_factor);
  179. return 1;
  180. }
  181. static SQRESULT math_broundf (HSQUIRRELVM v) {
  182. SQ_FUNC_VARS(v);
  183. SQ_GET_FLOAT(v, 2, num);
  184. SQ_OPT_INTEGER(v, 3, dec_places, 0);
  185. SQFloat dec_factor, tmp, itmp;
  186. int neg;
  187. if( dec_places>15 || dec_places<0 ) return sq_throwerror(v, _SC("decimal places out of range 0-15"));
  188. neg = num < 0;
  189. switch(dec_places){
  190. case 0: dec_factor = 1.0; break;
  191. case 1: dec_factor = 10.0; break;
  192. case 2: dec_factor = 100.0; break;
  193. case 3: dec_factor = 1000.0; break;
  194. case 4: dec_factor = 10000.0; break;
  195. case 5: dec_factor = 100000.0; break;
  196. case 6: dec_factor = 1000000.0; break;
  197. default: dec_factor = pow((double)10.0, dec_places);
  198. }
  199. tmp = num * dec_factor;
  200. itmp = floor(tmp + (neg ? -0.5 : 0.5));
  201. // Handle rounding of .5 in a special manner
  202. if(tmp - floor(tmp) == 0.5){
  203. tmp = itmp / 2;
  204. if(tmp != floor(tmp) ) { // Is itmp odd
  205. // Reduce Magnitude by 1 to make even
  206. if(neg) itmp++;
  207. else itmp--;
  208. }
  209. }
  210. sq_pushfloat(v, itmp / dec_factor);
  211. return 1;
  212. }
  213. static SQChar math_number_format_dec_point[2] = _SC(".");
  214. static SQChar math_number_format_thousand_sep[2] = _SC(",");
  215. static SQRESULT math_number_format_get_dec_point(HSQUIRRELVM v) {
  216. sq_pushstring(v, math_number_format_dec_point, -1);
  217. return 1;
  218. }
  219. static SQRESULT math_number_format_set_dec_point(HSQUIRRELVM v) {
  220. SQ_FUNC_VARS_NO_TOP(v);
  221. SQ_GET_STRING(v, 2, dec_point);
  222. math_number_format_dec_point[0] = *dec_point;
  223. math_number_format_dec_point[1] = _SC('\0');
  224. return 0;
  225. }
  226. static SQRESULT math_number_format_get_thousand_sep(HSQUIRRELVM v) {
  227. sq_pushstring(v, math_number_format_thousand_sep, -1);
  228. return 1;
  229. }
  230. static SQRESULT math_number_format_set_thousand_sep(HSQUIRRELVM v) {
  231. SQ_FUNC_VARS_NO_TOP(v);
  232. SQ_GET_STRING(v, 2, thousand_sep);
  233. math_number_format_thousand_sep[0] = *thousand_sep;
  234. math_number_format_thousand_sep[1] = '\0';
  235. return 0;
  236. }
  237. static SQRESULT math_number_format(HSQUIRRELVM v) {
  238. SQ_FUNC_VARS(v);
  239. SQ_GET_FLOAT(v, 2, d);
  240. SQ_OPT_INTEGER(v, 3, dec, 2);
  241. SQ_OPT_STRING(v, 4, dec_point, math_number_format_dec_point);
  242. SQ_OPT_STRING(v, 5, thousand_sep, math_number_format_thousand_sep);
  243. SQChar tmpbuf[64], resbuf[64];
  244. SQChar *s, *t; /* source, target */
  245. SQChar *dp;
  246. int integral;
  247. size_t tmplen, reslen=0;
  248. int count=0;
  249. int is_negative=0;
  250. int trim_right_zeros=0;
  251. if (d < 0) {
  252. is_negative = 1;
  253. d = -d;
  254. }
  255. if (dec < 0) {
  256. trim_right_zeros = 1;
  257. dec = -dec;
  258. }
  259. tmplen = scsprintf(tmpbuf, sizeof(tmpbuf), _SC("%.*f"), (int)dec, d);
  260. resbuf[0] = _SC('\0');
  261. if (isdigit((int)tmpbuf[0])) {
  262. /* find decimal point, if expected */
  263. if (dec) {
  264. dp = scstrpbrk(tmpbuf, _SC(".,"));
  265. } else {
  266. dp = NULL;
  267. }
  268. /* calculate the length of the return buffer */
  269. if (dp) {
  270. integral = dp - tmpbuf;
  271. } else {
  272. /* no decimal point was found */
  273. integral = tmplen;
  274. }
  275. /* allow for thousand separators */
  276. if (*thousand_sep) {
  277. integral += (integral-1) / 3;
  278. }
  279. reslen = integral;
  280. if (dec) {
  281. reslen += dec;
  282. if (*dec_point) {
  283. reslen++;
  284. }
  285. }
  286. /* add a byte for minus sign */
  287. if (is_negative) {
  288. reslen++;
  289. }
  290. if(sizeof(resbuf) >= reslen+1) {
  291. s = tmpbuf+tmplen-1;
  292. t = resbuf+reslen;
  293. *t-- = '\0';
  294. /* copy the decimal places.
  295. * Take care, as the sprintf implementation may return less places than
  296. * we requested due to internal buffer limitations */
  297. if (dec) {
  298. int declen = dp ? s - dp : 0;
  299. int topad = dec > declen ? dec - declen : 0;
  300. /* pad with '0's */
  301. while (topad--) {
  302. *t-- = '0';
  303. }
  304. if (dp) {
  305. s -= declen + 1; /* +1 to skip the point */
  306. t -= declen;
  307. /* now copy the chars after the point */
  308. memcpy(t + 1, dp + 1, declen);
  309. }
  310. /* add decimal point */
  311. if (*dec_point) {
  312. *t-- = *dec_point;
  313. }
  314. }
  315. /* copy the numbers before the decimal point, adding thousand
  316. * separator every three digits */
  317. while(s >= tmpbuf) {
  318. *t-- = *s--;
  319. if (*thousand_sep && (++count%3)==0 && s>=tmpbuf) {
  320. *t-- = *thousand_sep;
  321. }
  322. }
  323. /* and a minus sign, if needed */
  324. if (is_negative) {
  325. *t-- = '-';
  326. }
  327. /* trim right zeros */
  328. if (*dec_point && trim_right_zeros) {
  329. for(t=resbuf+reslen-1; *t != *dec_point; t--){
  330. if (*t == '0') *t = '\0';
  331. else break;
  332. }
  333. if (*t == *dec_point) *t = '\0';
  334. }
  335. }
  336. }
  337. sq_pushstring(v, resbuf, -1);
  338. return 1;
  339. }
  340. static SQRESULT math_ult(HSQUIRRELVM v)
  341. {
  342. SQ_FUNC_VARS_NO_TOP(v);
  343. SQ_GET_INTEGER(v, 2, n1);
  344. SQ_GET_INTEGER(v, 3, n2);
  345. sq_pushbool(v,((SQUnsignedInteger)n1) < ((SQUnsignedInteger)n2));
  346. return 1;
  347. }
  348. static SQRESULT math_min (HSQUIRRELVM v) {
  349. SQInteger n = sq_gettop(v); /* number of arguments */
  350. SQInteger imin = 2; /* index of current minimum value */
  351. for (SQInteger i = 3; i <= n; i++) {
  352. if (sq_compare(v, i, imin) < 0)
  353. imin = i;
  354. }
  355. sq_push(v, imin);
  356. return 1;
  357. }
  358. static SQRESULT math_max (HSQUIRRELVM v) {
  359. SQInteger n = sq_gettop(v); /* number of arguments */
  360. SQInteger imax = 2; /* index of current minimum value */
  361. for (SQInteger i = 3; i <= n; i++) {
  362. if (sq_compare(v, i, imax) > 0)
  363. imax = i;
  364. }
  365. sq_push(v, imax);
  366. return 1;
  367. }
  368. static SQRESULT math_deg (HSQUIRRELVM v) {
  369. SQ_FUNC_VARS_NO_TOP(v);
  370. SQ_GET_FLOAT(v, 2, n);
  371. sq_pushfloat(v, n * (180.0 / M_PI));
  372. return 1;
  373. }
  374. static SQRESULT math_rad (HSQUIRRELVM v) {
  375. SQ_FUNC_VARS_NO_TOP(v);
  376. SQ_GET_FLOAT(v, 2, n);
  377. sq_pushfloat(v, n * (M_PI / 180.0));
  378. return 1;
  379. }
  380. //DAD end
  381. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name),math_##name,nparams,tycheck,false}
  382. static const SQRegFunction mathlib_funcs[] = {
  383. _DECL_FUNC(sqrt,2,_SC(".n")),
  384. _DECL_FUNC(sin,2,_SC(".n")),
  385. _DECL_FUNC(cos,2,_SC(".n")),
  386. _DECL_FUNC(asin,2,_SC(".n")),
  387. _DECL_FUNC(acos,2,_SC(".n")),
  388. _DECL_FUNC(log,-2,_SC(".nn")),
  389. _DECL_FUNC(log10,2,_SC(".n")),
  390. _DECL_FUNC(tan,2,_SC(".n")),
  391. _DECL_FUNC(atan,2,_SC(".n")),
  392. _DECL_FUNC(atan2,3,_SC(".nn")),
  393. _DECL_FUNC(pow,3,_SC(".nn")),
  394. _DECL_FUNC(floor,2,_SC(".n")),
  395. _DECL_FUNC(ceil,2,_SC(".n")),
  396. _DECL_FUNC(exp,2,_SC(".n")),
  397. _DECL_FUNC(srand,2,_SC(".n")),
  398. _DECL_FUNC(rand,1,NULL),
  399. _DECL_FUNC(random,-1,_SC(".ii")),
  400. _DECL_FUNC(fabs,2,_SC(".n")),
  401. _DECL_FUNC(abs,2,_SC(".n")),
  402. _DECL_FUNC(isnan,2,_SC(".n")),
  403. _DECL_FUNC(isfinite,2,_SC(".n")),
  404. _DECL_FUNC(roundf,-2,_SC(".ni")),
  405. _DECL_FUNC(broundf,-2,_SC(".ni")),
  406. _DECL_FUNC(number_format,-2,_SC(".niss")),
  407. _DECL_FUNC(number_format_get_dec_point,1,_SC(".")),
  408. _DECL_FUNC(number_format_set_dec_point,2,_SC(".s")),
  409. _DECL_FUNC(number_format_get_thousand_sep,1,_SC(".")),
  410. _DECL_FUNC(number_format_set_thousand_sep,2,_SC(".s")),
  411. _DECL_FUNC(ult,3,_SC(".nn")),
  412. _DECL_FUNC(min,-3,_SC(".nn")),
  413. _DECL_FUNC(max,-3,_SC(".nn")),
  414. _DECL_FUNC(rad,2,_SC(".n")),
  415. _DECL_FUNC(deg,2,_SC(".n")),
  416. _DECL_FUNC(asinh,2,_SC(".n")),
  417. _DECL_FUNC(acosh,2,_SC(".n")),
  418. _DECL_FUNC(tanh,2,_SC(".n")),
  419. _DECL_FUNC(atanh,2,_SC(".n")),
  420. {NULL,(SQFUNCTION)0,0,NULL,false}
  421. };
  422. #undef _DECL_FUNC
  423. static void installFloatConst(HSQUIRRELVM v, const SQChar *skey, SQFloat fv)
  424. {
  425. sq_pushstring(v,skey,-1);
  426. sq_pushfloat(v,fv);
  427. sq_newslot(v,-3,SQFalse);
  428. }
  429. SQRESULT sqstd_register_mathlib(HSQUIRRELVM v)
  430. {
  431. sq_pushstring(v,_SC("math"),-1);
  432. sq_newtable(v);
  433. SQInteger i=0;
  434. while(mathlib_funcs[i].name!=0) {
  435. sq_pushstring(v,mathlib_funcs[i].name,-1);
  436. sq_newclosure(v,mathlib_funcs[i].f,0);
  437. sq_setparamscheck(v,mathlib_funcs[i].nparamscheck,mathlib_funcs[i].typemask);
  438. sq_setnativeclosurename(v,-1,mathlib_funcs[i].name);
  439. sq_newslot(v,-3,SQFalse);
  440. i++;
  441. }
  442. sq_pushstring(v,_SC("RAND_MAX"),-1);
  443. sq_pushinteger(v,RAND_MAX);
  444. sq_newslot(v,-3,SQFalse);
  445. installFloatConst(v, _SC("E"), (SQFloat)M_E);
  446. installFloatConst(v, _SC("LOG2E"), (SQFloat)M_LOG2E);
  447. installFloatConst(v, _SC("LOG10E"), (SQFloat)M_LOG10E);
  448. installFloatConst(v, _SC("LN2"), (SQFloat)M_LN2);
  449. installFloatConst(v, _SC("LN10"), (SQFloat)M_LN10);
  450. installFloatConst(v, _SC("PI"), (SQFloat)M_PI);
  451. installFloatConst(v, _SC("PI_2"), (SQFloat)M_PI_2);
  452. installFloatConst(v, _SC("PI_4"), (SQFloat)M_PI_4);
  453. installFloatConst(v, _SC("1_PI"), (SQFloat)M_1_PI);
  454. installFloatConst(v, _SC("2_PI"), (SQFloat)M_2_PI);
  455. installFloatConst(v, _SC("2_SQRTPI"), (SQFloat)M_2_SQRTPI);
  456. installFloatConst(v, _SC("SQRT2"), (SQFloat)M_SQRT2);
  457. installFloatConst(v, _SC("SQRT1_2"), (SQFloat)M_SQRT1_2);
  458. installFloatConst(v, _SC("HUGE"), (SQFloat)HUGE_VAL);
  459. /*
  460. installFloatConst(v, SC("DBL_MAX"), (SQFloat)DBL_MAX);
  461. installFloatConst(v, SC("DBL_MIN"), (SQFloat)DBL_MIN);
  462. */
  463. sq_pushstring(v,_SC("INT_MAX"),-1);
  464. sq_pushinteger(v,SQ_INT_MAX);
  465. sq_newslot(v,-3,SQFalse);
  466. sq_pushstring(v,_SC("INT_MIN"),-1);
  467. sq_pushinteger(v,SQ_INT_MIN);
  468. sq_newslot(v,-3,SQFalse);
  469. sq_newslot(v,-3,SQTrue); //insert math
  470. return SQ_OK;
  471. }