Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
lmathlib.c File Reference
#include "lprefix.h"
#include <stdlib.h>
#include <math.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

Go to the source code of this file.

Macros

#define lmathlib_c
 
#define LUA_LIB
 
#define PI   (l_mathop(3.141592653589793238462643383279502884))
 
#define l_rand()   rand()
 
#define l_srand(x)   srand(x)
 
#define L_RANDMAX   RAND_MAX
 

Functions

static int math_abs (lua_State *L)
 
static int math_sin (lua_State *L)
 
static int math_cos (lua_State *L)
 
static int math_tan (lua_State *L)
 
static int math_asin (lua_State *L)
 
static int math_acos (lua_State *L)
 
static int math_atan (lua_State *L)
 
static int math_toint (lua_State *L)
 
static void pushnumint (lua_State *L, lua_Number d)
 
static int math_floor (lua_State *L)
 
static int math_ceil (lua_State *L)
 
static int math_fmod (lua_State *L)
 
static int math_modf (lua_State *L)
 
static int math_sqrt (lua_State *L)
 
static int math_ult (lua_State *L)
 
static int math_log (lua_State *L)
 
static int math_exp (lua_State *L)
 
static int math_deg (lua_State *L)
 
static int math_rad (lua_State *L)
 
static int math_min (lua_State *L)
 
static int math_max (lua_State *L)
 
static int math_random (lua_State *L)
 
static int math_randomseed (lua_State *L)
 
static int math_type (lua_State *L)
 
LUAMOD_API int luaopen_math (lua_State *L)
 

Variables

static const luaL_Reg mathlib []
 

Macro Definition Documentation

◆ l_rand

#define l_rand ( )    rand()

Definition at line 32 of file lua-5.3.6/src/lmathlib.c.

Referenced by math_random(), and math_randomseed().

◆ L_RANDMAX

#define L_RANDMAX   RAND_MAX

Definition at line 34 of file lua-5.3.6/src/lmathlib.c.

Referenced by math_random().

◆ l_srand

#define l_srand ( x)    srand(x)

Definition at line 33 of file lua-5.3.6/src/lmathlib.c.

Referenced by math_randomseed().

◆ lmathlib_c

#define lmathlib_c

Definition at line 7 of file lua-5.3.6/src/lmathlib.c.

◆ LUA_LIB

#define LUA_LIB

Definition at line 8 of file lua-5.3.6/src/lmathlib.c.

◆ PI

#define PI   (l_mathop(3.141592653589793238462643383279502884))

Definition at line 23 of file lua-5.3.6/src/lmathlib.c.

Referenced by luaopen_math(), math_deg(), and math_rad().

Function Documentation

◆ luaopen_math()

LUAMOD_API int luaopen_math ( lua_State * L)

Definition at line 398 of file lua-5.3.6/src/lmathlib.c.

398 {
400 lua_pushnumber(L, PI);
401 lua_setfield(L, -2, "pi");
402 lua_pushnumber(L, (lua_Number)HUGE_VAL);
403 lua_setfield(L, -2, "huge");
404 lua_pushinteger(L, LUA_MAXINTEGER);
405 lua_setfield(L, -2, "maxinteger");
406 lua_pushinteger(L, LUA_MININTEGER);
407 lua_setfield(L, -2, "mininteger");
408 return 1;
409}
LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
LUA_NUMBER lua_Number
#define luaL_newlib(L, l)
#define PI
static const luaL_Reg mathlib[]

References lua_pushinteger(), lua_pushnumber(), lua_setfield(), luaL_newlib, mathlib, and PI.

◆ math_abs()

static int math_abs ( lua_State * L)
static

Definition at line 39 of file lua-5.3.6/src/lmathlib.c.

39 {
40 if (lua_isinteger(L, 1)) {
41 lua_Integer n = lua_tointeger(L, 1);
42 if (n < 0) n = (lua_Integer)(0u - (lua_Unsigned)n);
43 lua_pushinteger(L, n);
44 }
45 else
47 return 1;
48}
LUALIB_API lua_Number luaL_checknumber(lua_State *L, int narg)
LUA_INTEGER lua_Integer
#define lua_tointeger(L, i)
LUA_UNSIGNED lua_Unsigned
#define l_mathop(x)
LUA_API int lua_isinteger(lua_State *L, int idx)

References l_mathop, lua_isinteger(), lua_pushinteger(), lua_pushnumber(), lua_tointeger, and luaL_checknumber().

◆ math_acos()

static int math_acos ( lua_State * L)
static

Definition at line 70 of file lua-5.3.6/src/lmathlib.c.

70 {
72 return 1;
73}

References l_mathop, lua_pushnumber(), and luaL_checknumber().

◆ math_asin()

static int math_asin ( lua_State * L)
static

Definition at line 65 of file lua-5.3.6/src/lmathlib.c.

65 {
67 return 1;
68}

References l_mathop, lua_pushnumber(), and luaL_checknumber().

◆ math_atan()

static int math_atan ( lua_State * L)
static

Definition at line 75 of file lua-5.3.6/src/lmathlib.c.

75 {
77 lua_Number x = luaL_optnumber(L, 2, 1);
78 lua_pushnumber(L, l_mathop(atan2)(y, x));
79 return 1;
80}
LUALIB_API lua_Number luaL_optnumber(lua_State *L, int narg, lua_Number def)

References l_mathop, lua_pushnumber(), luaL_checknumber(), and luaL_optnumber().

◆ math_ceil()

static int math_ceil ( lua_State * L)
static

Definition at line 116 of file lua-5.3.6/src/lmathlib.c.

116 {
117 if (lua_isinteger(L, 1))
118 lua_settop(L, 1); /* integer is its own ceil */
119 else {
120 lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1));
121 pushnumint(L, d);
122 }
123 return 1;
124}
LUA_API void lua_settop(lua_State *L, int idx)
static void pushnumint(lua_State *L, lua_Number d)

References l_mathop, lua_isinteger(), lua_settop(), luaL_checknumber(), and pushnumint().

◆ math_cos()

static int math_cos ( lua_State * L)
static

Definition at line 55 of file lua-5.3.6/src/lmathlib.c.

55 {
57 return 1;
58}

References l_mathop, lua_pushnumber(), and luaL_checknumber().

◆ math_deg()

static int math_deg ( lua_State * L)
static

Definition at line 204 of file lua-5.3.6/src/lmathlib.c.

204 {
205 lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI));
206 return 1;
207}

References l_mathop, lua_pushnumber(), luaL_checknumber(), and PI.

◆ math_exp()

static int math_exp ( lua_State * L)
static

Definition at line 199 of file lua-5.3.6/src/lmathlib.c.

199 {
201 return 1;
202}

References l_mathop, lua_pushnumber(), and luaL_checknumber().

◆ math_floor()

static int math_floor ( lua_State * L)
static

Definition at line 105 of file lua-5.3.6/src/lmathlib.c.

105 {
106 if (lua_isinteger(L, 1))
107 lua_settop(L, 1); /* integer is its own floor */
108 else {
109 lua_Number d = l_mathop(floor)(luaL_checknumber(L, 1));
110 pushnumint(L, d);
111 }
112 return 1;
113}

References l_mathop, lua_isinteger(), lua_settop(), luaL_checknumber(), and pushnumint().

◆ math_fmod()

static int math_fmod ( lua_State * L)
static

Definition at line 127 of file lua-5.3.6/src/lmathlib.c.

127 {
128 if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) {
129 lua_Integer d = lua_tointeger(L, 2);
130 if ((lua_Unsigned)d + 1u <= 1u) { /* special cases: -1 or 0 */
131 luaL_argcheck(L, d != 0, 2, "zero");
132 lua_pushinteger(L, 0); /* avoid overflow with 0x80000... / -1 */
133 }
134 else
135 lua_pushinteger(L, lua_tointeger(L, 1) % d);
136 }
137 else
139 luaL_checknumber(L, 2)));
140 return 1;
141}
#define luaL_argcheck(L, cond, numarg, extramsg)

References l_mathop, lua_isinteger(), lua_pushinteger(), lua_pushnumber(), lua_tointeger, luaL_argcheck, and luaL_checknumber().

◆ math_log()

static int math_log ( lua_State * L)
static

Definition at line 179 of file lua-5.3.6/src/lmathlib.c.

179 {
181 lua_Number res;
182 if (lua_isnoneornil(L, 2))
183 res = l_mathop(log)(x);
184 else {
185 lua_Number base = luaL_checknumber(L, 2);
186#if !defined(LUA_USE_C89)
187 if (base == l_mathop(2.0))
188 res = l_mathop(log2)(x); else
189#endif
190 if (base == l_mathop(10.0))
191 res = l_mathop(log10)(x);
192 else
193 res = l_mathop(log)(x)/l_mathop(log)(base);
194 }
195 lua_pushnumber(L, res);
196 return 1;
197}
#define lua_isnoneornil(L, n)

References l_mathop, lua_isnoneornil, lua_pushnumber(), and luaL_checknumber().

◆ math_max()

static int math_max ( lua_State * L)
static

Definition at line 229 of file lua-5.3.6/src/lmathlib.c.

229 {
230 int n = lua_gettop(L); /* number of arguments */
231 int imax = 1; /* index of current maximum value */
232 int i;
233 luaL_argcheck(L, n >= 1, 1, "value expected");
234 for (i = 2; i <= n; i++) {
235 if (lua_compare(L, imax, i, LUA_OPLT))
236 imax = i;
237 }
238 lua_pushvalue(L, imax);
239 return 1;
240}
LUA_API void lua_pushvalue(lua_State *L, int idx)
LUA_API int lua_gettop(lua_State *L)
LUA_API int lua_compare(lua_State *L, int index1, int index2, int op)
#define LUA_OPLT

References lua_compare(), lua_gettop(), LUA_OPLT, lua_pushvalue(), and luaL_argcheck.

◆ math_min()

static int math_min ( lua_State * L)
static

Definition at line 215 of file lua-5.3.6/src/lmathlib.c.

215 {
216 int n = lua_gettop(L); /* number of arguments */
217 int imin = 1; /* index of current minimum value */
218 int i;
219 luaL_argcheck(L, n >= 1, 1, "value expected");
220 for (i = 2; i <= n; i++) {
221 if (lua_compare(L, i, imin, LUA_OPLT))
222 imin = i;
223 }
224 lua_pushvalue(L, imin);
225 return 1;
226}

References lua_compare(), lua_gettop(), LUA_OPLT, lua_pushvalue(), and luaL_argcheck.

◆ math_modf()

static int math_modf ( lua_State * L)
static

Definition at line 149 of file lua-5.3.6/src/lmathlib.c.

149 {
150 if (lua_isinteger(L ,1)) {
151 lua_settop(L, 1); /* number is its own integer part */
152 lua_pushnumber(L, 0); /* no fractional part */
153 }
154 else {
156 /* integer part (rounds toward zero) */
157 lua_Number ip = (n < 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n);
158 pushnumint(L, ip);
159 /* fractional part (test needed for inf/-inf) */
160 lua_pushnumber(L, (n == ip) ? l_mathop(0.0) : (n - ip));
161 }
162 return 2;
163}

References l_mathop, lua_isinteger(), lua_pushnumber(), lua_settop(), luaL_checknumber(), and pushnumint().

◆ math_rad()

static int math_rad ( lua_State * L)
static

Definition at line 209 of file lua-5.3.6/src/lmathlib.c.

209 {
210 lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0)));
211 return 1;
212}

References l_mathop, lua_pushnumber(), luaL_checknumber(), and PI.

◆ math_random()

static int math_random ( lua_State * L)
static

Definition at line 247 of file lua-5.3.6/src/lmathlib.c.

247 {
248 lua_Integer low, up;
249 double r = (double)l_rand() * (1.0 / ((double)L_RANDMAX + 1.0));
250 switch (lua_gettop(L)) { /* check number of arguments */
251 case 0: { /* no arguments */
252 lua_pushnumber(L, (lua_Number)r); /* Number between 0 and 1 */
253 return 1;
254 }
255 case 1: { /* only upper limit */
256 low = 1;
257 up = luaL_checkinteger(L, 1);
258 break;
259 }
260 case 2: { /* lower and upper limits */
261 low = luaL_checkinteger(L, 1);
262 up = luaL_checkinteger(L, 2);
263 break;
264 }
265 default: return luaL_error(L, "wrong number of arguments");
266 }
267 /* random integer in the interval [low, up] */
268 luaL_argcheck(L, low <= up, 1, "interval is empty");
269 luaL_argcheck(L, low >= 0 || up <= LUA_MAXINTEGER + low, 1,
270 "interval too large");
271 r *= (double)(up - low) + 1.0;
272 lua_pushinteger(L, (lua_Integer)r + low);
273 return 1;
274}
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int narg)
#define l_rand()
#define L_RANDMAX

References l_rand, L_RANDMAX, lua_gettop(), lua_pushinteger(), lua_pushnumber(), luaL_argcheck, luaL_checkinteger(), and luaL_error().

◆ math_randomseed()

static int math_randomseed ( lua_State * L)
static

Definition at line 277 of file lua-5.3.6/src/lmathlib.c.

277 {
278 l_srand((unsigned int)(lua_Integer)luaL_checknumber(L, 1));
279 (void)l_rand(); /* discard first value to avoid undesirable correlations */
280 return 0;
281}
#define l_srand(x)

References l_rand, l_srand, and luaL_checknumber().

◆ math_sin()

static int math_sin ( lua_State * L)
static

Definition at line 50 of file lua-5.3.6/src/lmathlib.c.

50 {
52 return 1;
53}

References l_mathop, lua_pushnumber(), and luaL_checknumber().

◆ math_sqrt()

static int math_sqrt ( lua_State * L)
static

Definition at line 166 of file lua-5.3.6/src/lmathlib.c.

166 {
168 return 1;
169}

References l_mathop, lua_pushnumber(), and luaL_checknumber().

◆ math_tan()

static int math_tan ( lua_State * L)
static

Definition at line 60 of file lua-5.3.6/src/lmathlib.c.

60 {
62 return 1;
63}

References l_mathop, lua_pushnumber(), and luaL_checknumber().

◆ math_toint()

static int math_toint ( lua_State * L)
static

Definition at line 83 of file lua-5.3.6/src/lmathlib.c.

83 {
84 int valid;
85 lua_Integer n = lua_tointegerx(L, 1, &valid);
86 if (valid)
87 lua_pushinteger(L, n);
88 else {
89 luaL_checkany(L, 1);
90 lua_pushnil(L); /* value is not convertible to integer */
91 }
92 return 1;
93}
LUA_API void lua_pushnil(lua_State *L)
LUALIB_API void luaL_checkany(lua_State *L, int narg)
LUA_API lua_Integer lua_tointegerx(lua_State *L, int idx, int *isnum)

References lua_pushinteger(), lua_pushnil(), lua_tointegerx(), and luaL_checkany().

◆ math_type()

static int math_type ( lua_State * L)
static

Definition at line 284 of file lua-5.3.6/src/lmathlib.c.

284 {
285 if (lua_type(L, 1) == LUA_TNUMBER) {
286 if (lua_isinteger(L, 1))
287 lua_pushliteral(L, "integer");
288 else
289 lua_pushliteral(L, "float");
290 }
291 else {
292 luaL_checkany(L, 1);
293 lua_pushnil(L);
294 }
295 return 1;
296}
LUA_API int lua_type(lua_State *L, int idx)
#define lua_pushliteral(L, s)
#define LUA_TNUMBER

References lua_isinteger(), lua_pushliteral, lua_pushnil(), LUA_TNUMBER, lua_type(), and luaL_checkany().

◆ math_ult()

static int math_ult ( lua_State * L)
static

Definition at line 172 of file lua-5.3.6/src/lmathlib.c.

172 {
176 return 1;
177}
LUA_API void lua_pushboolean(lua_State *L, int b)

References lua_pushboolean(), and luaL_checkinteger().

◆ pushnumint()

static void pushnumint ( lua_State * L,
lua_Number d )
static

Definition at line 96 of file lua-5.3.6/src/lmathlib.c.

96 {
98 if (lua_numbertointeger(d, &n)) /* does 'd' fit in an integer? */
99 lua_pushinteger(L, n); /* result is integer */
100 else
101 lua_pushnumber(L, d); /* result is float */
102}
#define lua_numbertointeger(n, p)

References lua_numbertointeger, lua_pushinteger(), and lua_pushnumber().

Referenced by math_ceil(), math_floor(), and math_modf().

Variable Documentation

◆ mathlib

const luaL_Reg mathlib[]
static

Definition at line 352 of file lua-5.3.6/src/lmathlib.c.

352 {
353 {"abs", math_abs},
354 {"acos", math_acos},
355 {"asin", math_asin},
356 {"atan", math_atan},
357 {"ceil", math_ceil},
358 {"cos", math_cos},
359 {"deg", math_deg},
360 {"exp", math_exp},
361 {"tointeger", math_toint},
362 {"floor", math_floor},
363 {"fmod", math_fmod},
364 {"ult", math_ult},
365 {"log", math_log},
366 {"max", math_max},
367 {"min", math_min},
368 {"modf", math_modf},
369 {"rad", math_rad},
370 {"random", math_random},
371 {"randomseed", math_randomseed},
372 {"sin", math_sin},
373 {"sqrt", math_sqrt},
374 {"tan", math_tan},
375 {"type", math_type},
376#if defined(LUA_COMPAT_MATHLIB)
377 {"atan2", math_atan},
378 {"cosh", math_cosh},
379 {"sinh", math_sinh},
380 {"tanh", math_tanh},
381 {"pow", math_pow},
382 {"frexp", math_frexp},
383 {"ldexp", math_ldexp},
384 {"log10", math_log10},
385#endif
386 /* placeholders */
387 {"pi", NULL},
388 {"huge", NULL},
389 {"maxinteger", NULL},
390 {"mininteger", NULL},
391 {NULL, NULL}
392};
#define NULL
Definition gmacros.h:924
static int math_frexp(lua_State *L)
static int math_pow(lua_State *L)
static int math_tanh(lua_State *L)
static int math_log10(lua_State *L)
static int math_sinh(lua_State *L)
static int math_cosh(lua_State *L)
static int math_ldexp(lua_State *L)
static int math_max(lua_State *L)
static int math_tan(lua_State *L)
static int math_fmod(lua_State *L)
static int math_randomseed(lua_State *L)
static int math_ult(lua_State *L)
static int math_rad(lua_State *L)
static int math_cos(lua_State *L)
static int math_atan(lua_State *L)
static int math_toint(lua_State *L)
static int math_exp(lua_State *L)
static int math_log(lua_State *L)
static int math_sqrt(lua_State *L)
static int math_abs(lua_State *L)
static int math_acos(lua_State *L)
static int math_floor(lua_State *L)
static int math_random(lua_State *L)
static int math_asin(lua_State *L)
static int math_min(lua_State *L)
static int math_deg(lua_State *L)
static int math_sin(lua_State *L)
static int math_modf(lua_State *L)
static int math_ceil(lua_State *L)
static int math_type(lua_State *L)

Referenced by luaopen_math().