The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
lmathlib.cpp
Go to the documentation of this file.
1 /*
2 ** Standard mathematical library
3 ** See Copyright Notice in lua.h
4 */
5 
6 
7 #include <stdlib.h>
8 #include <math.h>
9 #include <boost/math/constants/constants.hpp>
10 
11 #define lmathlib_c
12 #define LUA_LIB
13 
14 #include "lua.h"
15 
16 #include "lauxlib.h"
17 #include "lualib.h"
18 
19 
20 #define RADIANS_PER_DEGREE ( boost::math::constants::pi<lua_Number>()/180.0 )
21 
22 
23 
24 static int math_abs (lua_State *L) {
25  lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1)));
26  return 1;
27 }
28 
29 static int math_sin (lua_State *L) {
31  return 1;
32 }
33 
34 static int math_sinh (lua_State *L) {
35  lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1)));
36  return 1;
37 }
38 
39 static int math_cos (lua_State *L) {
41  return 1;
42 }
43 
44 static int math_cosh (lua_State *L) {
45  lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1)));
46  return 1;
47 }
48 
49 static int math_tan (lua_State *L) {
51  return 1;
52 }
53 
54 static int math_tanh (lua_State *L) {
55  lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1)));
56  return 1;
57 }
58 
59 static int math_asin (lua_State *L) {
60  lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1)));
61  return 1;
62 }
63 
64 static int math_acos (lua_State *L) {
65  lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1)));
66  return 1;
67 }
68 
69 static int math_atan (lua_State *L) {
70  lua_pushnumber(L, l_mathop(atan)(luaL_checknumber(L, 1)));
71  return 1;
72 }
73 
74 static int math_atan2 (lua_State *L) {
76  luaL_checknumber(L, 2)));
77  return 1;
78 }
79 
80 static int math_ceil (lua_State *L) {
81  lua_pushnumber(L, l_mathop(ceil)(luaL_checknumber(L, 1)));
82  return 1;
83 }
84 
85 static int math_floor (lua_State *L) {
87  return 1;
88 }
89 
90 static int math_fmod (lua_State *L) {
92  luaL_checknumber(L, 2)));
93  return 1;
94 }
95 
96 static int math_modf (lua_State *L) {
97  lua_Number ip;
98  lua_Number fp = l_mathop(modf)(luaL_checknumber(L, 1), &ip);
99  lua_pushnumber(L, ip);
100  lua_pushnumber(L, fp);
101  return 2;
102 }
103 
104 static int math_sqrt (lua_State *L) {
105  lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1)));
106  return 1;
107 }
108 
109 static int math_pow (lua_State *L) {
110  lua_Number x = luaL_checknumber(L, 1);
111  lua_Number y = luaL_checknumber(L, 2);
112  lua_pushnumber(L, l_mathop(pow)(x, y));
113  return 1;
114 }
115 
116 static int math_log (lua_State *L) {
117  lua_Number x = luaL_checknumber(L, 1);
118  lua_Number res;
119  if (lua_isnoneornil(L, 2))
120  res = l_mathop(log)(x);
121  else {
122  lua_Number base = luaL_checknumber(L, 2);
123  if (base == (lua_Number)10.0) res = l_mathop(log10)(x);
124  else res = l_mathop(log)(x)/l_mathop(log)(base);
125  }
126  lua_pushnumber(L, res);
127  return 1;
128 }
129 
130 #if defined(LUA_COMPAT_LOG10)
131 static int math_log10 (lua_State *L) {
132  lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));
133  return 1;
134 }
135 #endif
136 
137 static int math_exp (lua_State *L) {
138  lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));
139  return 1;
140 }
141 
142 static int math_deg (lua_State *L) {
144  return 1;
145 }
146 
147 static int math_rad (lua_State *L) {
149  return 1;
150 }
151 
152 static int math_frexp (lua_State *L) {
153  int e;
154  lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e));
155  lua_pushinteger(L, e);
156  return 2;
157 }
158 
159 static int math_ldexp (lua_State *L) {
160  lua_Number x = luaL_checknumber(L, 1);
161  int ep = luaL_checkint(L, 2);
162  lua_pushnumber(L, l_mathop(ldexp)(x, ep));
163  return 1;
164 }
165 
166 
167 
168 static int math_min (lua_State *L) {
169  int n = lua_gettop(L); /* number of arguments */
170  lua_Number dmin = luaL_checknumber(L, 1);
171  int i;
172  for (i=2; i<=n; i++) {
173  lua_Number d = luaL_checknumber(L, i);
174  if (d < dmin)
175  dmin = d;
176  }
177  lua_pushnumber(L, dmin);
178  return 1;
179 }
180 
181 
182 static int math_max (lua_State *L) {
183  int n = lua_gettop(L); /* number of arguments */
184  lua_Number dmax = luaL_checknumber(L, 1);
185  int i;
186  for (i=2; i<=n; i++) {
187  lua_Number d = luaL_checknumber(L, i);
188  if (d > dmax)
189  dmax = d;
190  }
191  lua_pushnumber(L, dmax);
192  return 1;
193 }
194 
195 
196 static int math_random (lua_State *L) {
197  /* the `%' avoids the (rare) case of r==1, and is needed also because on
198  some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
199  lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
200  switch (lua_gettop(L)) { /* check number of arguments */
201  case 0: { /* no arguments */
202  lua_pushnumber(L, r); /* Number between 0 and 1 */
203  break;
204  }
205  case 1: { /* only upper limit */
206  lua_Number u = luaL_checknumber(L, 1);
207  luaL_argcheck(L, (lua_Number)1.0 <= u, 1, "interval is empty");
208  lua_pushnumber(L, l_mathop(floor)(r*u) + (lua_Number)(1.0)); /* [1, u] */
209  break;
210  }
211  case 2: { /* lower and upper limits */
212  lua_Number l = luaL_checknumber(L, 1);
213  lua_Number u = luaL_checknumber(L, 2);
214  luaL_argcheck(L, l <= u, 2, "interval is empty");
215  lua_pushnumber(L, l_mathop(floor)(r*(u-l+1)) + l); /* [l, u] */
216  break;
217  }
218  default: return luaL_error(L, "wrong number of arguments");
219  }
220  return 1;
221 }
222 
223 
224 static int math_randomseed (lua_State *L) {
225  srand(luaL_checkunsigned(L, 1));
226  (void)rand(); /* discard first value to avoid undesirable correlations */
227  return 0;
228 }
229 
230 
231 static const luaL_Reg mathlib[] = {
232  {"abs", math_abs},
233  {"acos", math_acos},
234  {"asin", math_asin},
235  {"atan2", math_atan2},
236  {"atan", math_atan},
237  {"ceil", math_ceil},
238  {"cosh", math_cosh},
239  {"cos", math_cos},
240  {"deg", math_deg},
241  {"exp", math_exp},
242  {"floor", math_floor},
243  {"fmod", math_fmod},
244  {"frexp", math_frexp},
245  {"ldexp", math_ldexp},
246 #if defined(LUA_COMPAT_LOG10)
247  {"log10", math_log10},
248 #endif
249  {"log", math_log},
250  {"max", math_max},
251  {"min", math_min},
252  {"modf", math_modf},
253  {"pow", math_pow},
254  {"rad", math_rad},
255  {"random", math_random},
256  {"randomseed", math_randomseed},
257  {"sinh", math_sinh},
258  {"sin", math_sin},
259  {"sqrt", math_sqrt},
260  {"tanh", math_tanh},
261  {"tan", math_tan},
262  {NULL, NULL}
263 };
264 
265 
266 /*
267 ** Open math library
268 */
270  luaL_newlib(L, mathlib);
271  lua_pushnumber(L, boost::math::constants::pi<lua_Number>());
272  lua_setfield(L, -2, "pi");
273  lua_pushnumber(L, HUGE_VAL);
274  lua_setfield(L, -2, "huge");
275  return 1;
276 }
277 
static int math_abs(lua_State *L)
Definition: lmathlib.cpp:24
#define lua_isnoneornil(L, n)
Definition: lua.h:337
static int math_asin(lua_State *L)
Definition: lmathlib.cpp:59
static int math_frexp(lua_State *L)
Definition: lmathlib.cpp:152
static int math_randomseed(lua_State *L)
Definition: lmathlib.cpp:224
GLvoid **typedef void(GLAPIENTRY *PFNGLGETVERTEXATTRIBDVPROC)(GLuint
Definition: glew.h:1806
static int math_tan(lua_State *L)
Definition: lmathlib.cpp:49
static int math_max(lua_State *L)
Definition: lmathlib.cpp:182
LUA_API int lua_gettop(lua_State *L)
Definition: lapi.cpp:154
LUAMOD_API int luaopen_math(lua_State *L)
Definition: lmathlib.cpp:269
static int math_exp(lua_State *L)
Definition: lmathlib.cpp:137
static int math_cosh(lua_State *L)
Definition: lmathlib.cpp:44
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
#define d
#define luaL_argcheck(L, cond, numarg, extramsg)
Definition: lauxlib.h:113
GLdouble l
Definition: glew.h:6966
static int math_random(lua_State *L)
Definition: lmathlib.cpp:196
static int math_sinh(lua_State *L)
Definition: lmathlib.cpp:34
static int math_modf(lua_State *L)
Definition: lmathlib.cpp:96
static int math_atan2(lua_State *L)
Definition: lmathlib.cpp:74
static const luaL_Reg mathlib[]
Definition: lmathlib.cpp:231
static int math_pow(lua_State *L)
Definition: lmathlib.cpp:109
static int math_min(lua_State *L)
Definition: lmathlib.cpp:168
LUALIB_API lua_Number luaL_checknumber(lua_State *L, int narg)
Definition: lauxlib.cpp:377
static int math_cos(lua_State *L)
Definition: lmathlib.cpp:39
static int math_floor(lua_State *L)
Definition: lmathlib.cpp:85
static int math_log(lua_State *L)
Definition: lmathlib.cpp:116
LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
Definition: lapi.cpp:467
#define l_mathop(x)
Definition: luaconf.h:421
GLuint res
Definition: glew.h:9258
#define RADIANS_PER_DEGREE
Definition: lmathlib.cpp:20
static int math_deg(lua_State *L)
Definition: lmathlib.cpp:142
LUALIB_API lua_Unsigned luaL_checkunsigned(lua_State *L, int narg)
Definition: lauxlib.cpp:400
#define LUAMOD_API
Definition: luaconf.h:163
static int math_log10(lua_State *L)
Definition: lmathlib.cpp:131
size_t i
Definition: function.cpp:1057
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
GLdouble GLdouble GLdouble r
Definition: glew.h:1374
#define luaL_newlib(L, l)
Definition: lauxlib.h:111
static int math_ldexp(lua_State *L)
Definition: lmathlib.cpp:159
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
Definition: lauxlib.cpp:198
static int math_ceil(lua_State *L)
Definition: lmathlib.cpp:80
GLclampd n
Definition: glew.h:5903
#define luaL_checkint(L, n)
Definition: lauxlib.h:117
static int math_fmod(lua_State *L)
Definition: lmathlib.cpp:90
static int math_acos(lua_State *L)
Definition: lmathlib.cpp:64
static int math_rad(lua_State *L)
Definition: lmathlib.cpp:147
#define e
static int math_sqrt(lua_State *L)
Definition: lmathlib.cpp:104
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
Definition: lapi.cpp:477
static int math_atan(lua_State *L)
Definition: lmathlib.cpp:69
LUA_NUMBER lua_Number
Definition: lua.h:102
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
Definition: lapi.cpp:752
static int math_tanh(lua_State *L)
Definition: lmathlib.cpp:54
static int math_sin(lua_State *L)
Definition: lmathlib.cpp:29