Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
lmathlib.c File Reference
#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   ((lua_Number)(3.1415926535897932384626433832795))
 
#define RADIANS_PER_DEGREE   ((lua_Number)(PI/180.0))
 

Functions

static int math_abs (lua_State *L)
 
static int math_sin (lua_State *L)
 
static int math_sinh (lua_State *L)
 
static int math_cos (lua_State *L)
 
static int math_cosh (lua_State *L)
 
static int math_tan (lua_State *L)
 
static int math_tanh (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_atan2 (lua_State *L)
 
static int math_ceil (lua_State *L)
 
static int math_floor (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_pow (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_frexp (lua_State *L)
 
static int math_ldexp (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)
 
LUAMOD_API int luaopen_math (lua_State *L)
 

Variables

static const luaL_Reg mathlib []
 

Macro Definition Documentation

◆ lmathlib_c

#define lmathlib_c

Definition at line 11 of file lua-5.2.4/src/lmathlib.c.

◆ LUA_LIB

#define LUA_LIB

Definition at line 12 of file lua-5.2.4/src/lmathlib.c.

◆ PI

#define PI   ((lua_Number)(3.1415926535897932384626433832795))

Definition at line 21 of file lua-5.2.4/src/lmathlib.c.

Referenced by luaopen_math().

◆ RADIANS_PER_DEGREE

#define RADIANS_PER_DEGREE   ((lua_Number)(PI/180.0))

Definition at line 22 of file lua-5.2.4/src/lmathlib.c.

Referenced by math_deg(), and math_rad().

Function Documentation

◆ luaopen_math()

LUAMOD_API int luaopen_math ( lua_State * L)

Definition at line 271 of file lua-5.2.4/src/lmathlib.c.

271 {
273 lua_pushnumber(L, PI);
274 lua_setfield(L, -2, "pi");
275 lua_pushnumber(L, HUGE_VAL);
276 lua_setfield(L, -2, "huge");
277 return 1;
278}
LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
#define luaL_newlib(L, l)
#define PI
static const luaL_Reg mathlib[]

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

◆ math_abs()

static int math_abs ( lua_State * L)
static

Definition at line 26 of file lua-5.2.4/src/lmathlib.c.

26 {
28 return 1;
29}
LUALIB_API lua_Number luaL_checknumber(lua_State *L, int narg)
#define l_mathop(x)

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

◆ math_acos()

static int math_acos ( lua_State * L)
static

Definition at line 66 of file lua-5.2.4/src/lmathlib.c.

66 {
68 return 1;
69}

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

◆ math_asin()

static int math_asin ( lua_State * L)
static

Definition at line 61 of file lua-5.2.4/src/lmathlib.c.

61 {
63 return 1;
64}

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

◆ math_atan()

static int math_atan ( lua_State * L)
static

Definition at line 71 of file lua-5.2.4/src/lmathlib.c.

71 {
73 return 1;
74}

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

◆ math_atan2()

static int math_atan2 ( lua_State * L)
static

Definition at line 76 of file lua-5.2.4/src/lmathlib.c.

76 {
78 luaL_checknumber(L, 2)));
79 return 1;
80}

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

◆ math_ceil()

static int math_ceil ( lua_State * L)
static

Definition at line 82 of file lua-5.2.4/src/lmathlib.c.

82 {
84 return 1;
85}

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

◆ math_cos()

static int math_cos ( lua_State * L)
static

Definition at line 41 of file lua-5.2.4/src/lmathlib.c.

41 {
43 return 1;
44}

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

◆ math_cosh()

static int math_cosh ( lua_State * L)
static

Definition at line 46 of file lua-5.2.4/src/lmathlib.c.

46 {
48 return 1;
49}

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

◆ math_deg()

static int math_deg ( lua_State * L)
static

Definition at line 144 of file lua-5.2.4/src/lmathlib.c.

144 {
146 return 1;
147}
#define RADIANS_PER_DEGREE

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

◆ math_exp()

static int math_exp ( lua_State * L)
static

Definition at line 139 of file lua-5.2.4/src/lmathlib.c.

139 {
141 return 1;
142}

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

◆ math_floor()

static int math_floor ( lua_State * L)
static

Definition at line 87 of file lua-5.2.4/src/lmathlib.c.

87 {
89 return 1;
90}

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

◆ math_fmod()

static int math_fmod ( lua_State * L)
static

Definition at line 92 of file lua-5.2.4/src/lmathlib.c.

92 {
94 luaL_checknumber(L, 2)));
95 return 1;
96}

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

◆ math_frexp()

static int math_frexp ( lua_State * L)
static

Definition at line 154 of file lua-5.2.4/src/lmathlib.c.

154 {
155 int e;
156 lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e));
157 lua_pushinteger(L, e);
158 return 2;
159}
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)

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

◆ math_ldexp()

static int math_ldexp ( lua_State * L)
static

Definition at line 161 of file lua-5.2.4/src/lmathlib.c.

161 {
163 int ep = luaL_checkint(L, 2);
164 lua_pushnumber(L, l_mathop(ldexp)(x, ep));
165 return 1;
166}
#define luaL_checkint(L, n)
LUA_NUMBER lua_Number

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

◆ math_log()

static int math_log ( lua_State * L)
static

Definition at line 118 of file lua-5.2.4/src/lmathlib.c.

118 {
120 lua_Number res;
121 if (lua_isnoneornil(L, 2))
122 res = l_mathop(log)(x);
123 else {
124 lua_Number base = luaL_checknumber(L, 2);
125 if (base == (lua_Number)10.0) res = l_mathop(log10)(x);
126 else res = l_mathop(log)(x)/l_mathop(log)(base);
127 }
128 lua_pushnumber(L, res);
129 return 1;
130}
#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 184 of file lua-5.2.4/src/lmathlib.c.

184 {
185 int n = lua_gettop(L); /* number of arguments */
186 lua_Number dmax = luaL_checknumber(L, 1);
187 int i;
188 for (i=2; i<=n; i++) {
190 if (d > dmax)
191 dmax = d;
192 }
193 lua_pushnumber(L, dmax);
194 return 1;
195}
LUA_API int lua_gettop(lua_State *L)

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

◆ math_min()

static int math_min ( lua_State * L)
static

Definition at line 170 of file lua-5.2.4/src/lmathlib.c.

170 {
171 int n = lua_gettop(L); /* number of arguments */
172 lua_Number dmin = luaL_checknumber(L, 1);
173 int i;
174 for (i=2; i<=n; i++) {
176 if (d < dmin)
177 dmin = d;
178 }
179 lua_pushnumber(L, dmin);
180 return 1;
181}

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

◆ math_modf()

static int math_modf ( lua_State * L)
static

Definition at line 98 of file lua-5.2.4/src/lmathlib.c.

98 {
99 lua_Number ip;
100 lua_Number fp = l_mathop(modf)(luaL_checknumber(L, 1), &ip);
101 lua_pushnumber(L, ip);
102 lua_pushnumber(L, fp);
103 return 2;
104}

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

◆ math_pow()

static int math_pow ( lua_State * L)
static

Definition at line 111 of file lua-5.2.4/src/lmathlib.c.

111 {
114 lua_pushnumber(L, l_mathop(pow)(x, y));
115 return 1;
116}

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

◆ math_rad()

static int math_rad ( lua_State * L)
static

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

149 {
151 return 1;
152}

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

◆ math_random()

static int math_random ( lua_State * L)
static

Definition at line 198 of file lua-5.2.4/src/lmathlib.c.

198 {
199 /* the `%' avoids the (rare) case of r==1, and is needed also because on
200 some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
201 lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
202 switch (lua_gettop(L)) { /* check number of arguments */
203 case 0: { /* no arguments */
204 lua_pushnumber(L, r); /* Number between 0 and 1 */
205 break;
206 }
207 case 1: { /* only upper limit */
209 luaL_argcheck(L, (lua_Number)1.0 <= u, 1, "interval is empty");
210 lua_pushnumber(L, l_mathop(floor)(r*u) + (lua_Number)(1.0)); /* [1, u] */
211 break;
212 }
213 case 2: { /* lower and upper limits */
216 luaL_argcheck(L, l <= u, 2, "interval is empty");
217 lua_pushnumber(L, l_mathop(floor)(r*(u-l+1)) + l); /* [l, u] */
218 break;
219 }
220 default: return luaL_error(L, "wrong number of arguments");
221 }
222 return 1;
223}
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
#define luaL_argcheck(L, cond, numarg, extramsg)

References l_mathop, lua_gettop(), lua_pushnumber(), luaL_argcheck, luaL_checknumber(), and luaL_error().

◆ math_randomseed()

static int math_randomseed ( lua_State * L)
static

Definition at line 226 of file lua-5.2.4/src/lmathlib.c.

226 {
227 srand(luaL_checkunsigned(L, 1));
228 (void)rand(); /* discard first value to avoid undesirable correlations */
229 return 0;
230}
LUALIB_API lua_Unsigned luaL_checkunsigned(lua_State *L, int narg)

References luaL_checkunsigned().

◆ math_sin()

static int math_sin ( lua_State * L)
static

Definition at line 31 of file lua-5.2.4/src/lmathlib.c.

31 {
33 return 1;
34}

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

◆ math_sinh()

static int math_sinh ( lua_State * L)
static

Definition at line 36 of file lua-5.2.4/src/lmathlib.c.

36 {
38 return 1;
39}

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

◆ math_sqrt()

static int math_sqrt ( lua_State * L)
static

Definition at line 106 of file lua-5.2.4/src/lmathlib.c.

106 {
108 return 1;
109}

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

◆ math_tan()

static int math_tan ( lua_State * L)
static

Definition at line 51 of file lua-5.2.4/src/lmathlib.c.

51 {
53 return 1;
54}

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

◆ math_tanh()

static int math_tanh ( lua_State * L)
static

Definition at line 56 of file lua-5.2.4/src/lmathlib.c.

56 {
58 return 1;
59}

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

Variable Documentation

◆ mathlib

const luaL_Reg mathlib[]
static

Definition at line 233 of file lua-5.2.4/src/lmathlib.c.

233 {
234 {"abs", math_abs},
235 {"acos", math_acos},
236 {"asin", math_asin},
237 {"atan2", math_atan2},
238 {"atan", math_atan},
239 {"ceil", math_ceil},
240 {"cosh", math_cosh},
241 {"cos", math_cos},
242 {"deg", math_deg},
243 {"exp", math_exp},
244 {"floor", math_floor},
245 {"fmod", math_fmod},
246 {"frexp", math_frexp},
247 {"ldexp", math_ldexp},
248#if defined(LUA_COMPAT_LOG10)
249 {"log10", math_log10},
250#endif
251 {"log", math_log},
252 {"max", math_max},
253 {"min", math_min},
254 {"modf", math_modf},
255 {"pow", math_pow},
256 {"rad", math_rad},
257 {"random", math_random},
258 {"randomseed", math_randomseed},
259 {"sinh", math_sinh},
260 {"sin", math_sin},
261 {"sqrt", math_sqrt},
262 {"tanh", math_tanh},
263 {"tan", math_tan},
264 {NULL, NULL}
265};
#define NULL
Definition gmacros.h:924
static int math_log10(lua_State *L)
static int math_max(lua_State *L)
static int math_tan(lua_State *L)
static int math_frexp(lua_State *L)
static int math_pow(lua_State *L)
static int math_fmod(lua_State *L)
static int math_tanh(lua_State *L)
static int math_sinh(lua_State *L)
static int math_randomseed(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_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_cosh(lua_State *L)
static int math_min(lua_State *L)
static int math_atan2(lua_State *L)
static int math_deg(lua_State *L)
static int math_sin(lua_State *L)
static int math_ldexp(lua_State *L)
static int math_modf(lua_State *L)
static int math_ceil(lua_State *L)

Referenced by luaopen_math().