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   (3.14159265358979323846)
 
#define RADIANS_PER_DEGREE   (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_log10 (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)
 
LUALIB_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.1.5/src/lmathlib.c.

◆ LUA_LIB

#define LUA_LIB

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

◆ PI

#define PI   (3.14159265358979323846)

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

Referenced by luaopen_math().

◆ RADIANS_PER_DEGREE

#define RADIANS_PER_DEGREE   (PI/180.0)

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

Referenced by math_deg(), and math_rad().

Function Documentation

◆ luaopen_math()

LUALIB_API int luaopen_math ( lua_State * L)

Definition at line 251 of file lua-5.1.5/src/lmathlib.c.

251 {
253 lua_pushnumber(L, PI);
254 lua_setfield(L, -2, "pi");
255 lua_pushnumber(L, HUGE_VAL);
256 lua_setfield(L, -2, "huge");
257#if defined(LUA_COMPAT_MOD)
258 lua_getfield(L, -1, "fmod");
259 lua_setfield(L, -2, "mod");
260#endif
261 return 1;
262}
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_getfield(lua_State *L, int idx, const char *k)
LUALIB_API void luaL_register(lua_State *L, const char *libname, const luaL_Reg *l)
#define PI
static const luaL_Reg mathlib[]
#define LUA_MATHLIBNAME

References lua_getfield(), LUA_MATHLIBNAME, lua_pushnumber(), lua_setfield(), luaL_register(), mathlib, and PI.

◆ math_abs()

static int math_abs ( lua_State * L)
static

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

26 {
27 lua_pushnumber(L, fabs(luaL_checknumber(L, 1)));
28 return 1;
29}
LUALIB_API lua_Number luaL_checknumber(lua_State *L, int narg)

References lua_pushnumber(), and luaL_checknumber().

◆ math_acos()

static int math_acos ( lua_State * L)
static

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

66 {
67 lua_pushnumber(L, acos(luaL_checknumber(L, 1)));
68 return 1;
69}

References lua_pushnumber(), and luaL_checknumber().

◆ math_asin()

static int math_asin ( lua_State * L)
static

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

61 {
62 lua_pushnumber(L, asin(luaL_checknumber(L, 1)));
63 return 1;
64}

References lua_pushnumber(), and luaL_checknumber().

◆ math_atan()

static int math_atan ( lua_State * L)
static

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

71 {
72 lua_pushnumber(L, atan(luaL_checknumber(L, 1)));
73 return 1;
74}

References lua_pushnumber(), and luaL_checknumber().

◆ math_atan2()

static int math_atan2 ( lua_State * L)
static

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

76 {
77 lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
78 return 1;
79}

References lua_pushnumber(), and luaL_checknumber().

◆ math_ceil()

static int math_ceil ( lua_State * L)
static

Definition at line 81 of file lua-5.1.5/src/lmathlib.c.

81 {
82 lua_pushnumber(L, ceil(luaL_checknumber(L, 1)));
83 return 1;
84}

References lua_pushnumber(), and luaL_checknumber().

◆ math_cos()

static int math_cos ( lua_State * L)
static

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

41 {
42 lua_pushnumber(L, cos(luaL_checknumber(L, 1)));
43 return 1;
44}

References lua_pushnumber(), and luaL_checknumber().

◆ math_cosh()

static int math_cosh ( lua_State * L)
static

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

46 {
47 lua_pushnumber(L, cosh(luaL_checknumber(L, 1)));
48 return 1;
49}

References lua_pushnumber(), and luaL_checknumber().

◆ math_deg()

static int math_deg ( lua_State * L)
static

Definition at line 129 of file lua-5.1.5/src/lmathlib.c.

129 {
131 return 1;
132}
#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 124 of file lua-5.1.5/src/lmathlib.c.

124 {
125 lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
126 return 1;
127}

References lua_pushnumber(), and luaL_checknumber().

◆ math_floor()

static int math_floor ( lua_State * L)
static

Definition at line 86 of file lua-5.1.5/src/lmathlib.c.

86 {
87 lua_pushnumber(L, floor(luaL_checknumber(L, 1)));
88 return 1;
89}

References lua_pushnumber(), and luaL_checknumber().

◆ math_fmod()

static int math_fmod ( lua_State * L)
static

Definition at line 91 of file lua-5.1.5/src/lmathlib.c.

91 {
93 return 1;
94}

References lua_pushnumber(), and luaL_checknumber().

◆ math_frexp()

static int math_frexp ( lua_State * L)
static

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

139 {
140 int e;
141 lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));
142 lua_pushinteger(L, e);
143 return 2;
144}
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)

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

◆ math_ldexp()

static int math_ldexp ( lua_State * L)
static

Definition at line 146 of file lua-5.1.5/src/lmathlib.c.

146 {
147 lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));
148 return 1;
149}
#define luaL_checkint(L, n)

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

◆ math_log()

static int math_log ( lua_State * L)
static

Definition at line 114 of file lua-5.1.5/src/lmathlib.c.

114 {
115 lua_pushnumber(L, log(luaL_checknumber(L, 1)));
116 return 1;
117}

References lua_pushnumber(), and luaL_checknumber().

◆ math_log10()

static int math_log10 ( lua_State * L)
static

Definition at line 119 of file lua-5.1.5/src/lmathlib.c.

119 {
120 lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
121 return 1;
122}

References lua_pushnumber(), and luaL_checknumber().

◆ math_max()

static int math_max ( lua_State * L)
static

Definition at line 167 of file lua-5.1.5/src/lmathlib.c.

167 {
168 int n = lua_gettop(L); /* number of arguments */
169 lua_Number dmax = luaL_checknumber(L, 1);
170 int i;
171 for (i=2; i<=n; i++) {
173 if (d > dmax)
174 dmax = d;
175 }
176 lua_pushnumber(L, dmax);
177 return 1;
178}
LUA_API int lua_gettop(lua_State *L)
LUA_NUMBER lua_Number

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

◆ math_min()

static int math_min ( lua_State * L)
static

Definition at line 153 of file lua-5.1.5/src/lmathlib.c.

153 {
154 int n = lua_gettop(L); /* number of arguments */
155 lua_Number dmin = luaL_checknumber(L, 1);
156 int i;
157 for (i=2; i<=n; i++) {
159 if (d < dmin)
160 dmin = d;
161 }
162 lua_pushnumber(L, dmin);
163 return 1;
164}

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

◆ math_modf()

static int math_modf ( lua_State * L)
static

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

96 {
97 double ip;
98 double fp = modf(luaL_checknumber(L, 1), &ip);
99 lua_pushnumber(L, ip);
100 lua_pushnumber(L, fp);
101 return 2;
102}

References lua_pushnumber(), and luaL_checknumber().

◆ math_pow()

static int math_pow ( lua_State * L)
static

Definition at line 109 of file lua-5.1.5/src/lmathlib.c.

109 {
111 return 1;
112}

References lua_pushnumber(), and luaL_checknumber().

◆ math_rad()

static int math_rad ( lua_State * L)
static

Definition at line 134 of file lua-5.1.5/src/lmathlib.c.

134 {
136 return 1;
137}

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

◆ math_random()

static int math_random ( lua_State * L)
static

Definition at line 181 of file lua-5.1.5/src/lmathlib.c.

181 {
182 /* the `%' avoids the (rare) case of r==1, and is needed also because on
183 some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
184 lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
185 switch (lua_gettop(L)) { /* check number of arguments */
186 case 0: { /* no arguments */
187 lua_pushnumber(L, r); /* Number between 0 and 1 */
188 break;
189 }
190 case 1: { /* only upper limit */
191 int u = luaL_checkint(L, 1);
192 luaL_argcheck(L, 1<=u, 1, "interval is empty");
193 lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */
194 break;
195 }
196 case 2: { /* lower and upper limits */
197 int l = luaL_checkint(L, 1);
198 int u = luaL_checkint(L, 2);
199 luaL_argcheck(L, l<=u, 2, "interval is empty");
200 lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */
201 break;
202 }
203 default: return luaL_error(L, "wrong number of arguments");
204 }
205 return 1;
206}
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
#define luaL_argcheck(L, cond, numarg, extramsg)

References lua_gettop(), lua_pushnumber(), luaL_argcheck, luaL_checkint, and luaL_error().

◆ math_randomseed()

static int math_randomseed ( lua_State * L)
static

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

209 {
210 srand(luaL_checkint(L, 1));
211 return 0;
212}

References luaL_checkint.

◆ math_sin()

static int math_sin ( lua_State * L)
static

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

31 {
32 lua_pushnumber(L, sin(luaL_checknumber(L, 1)));
33 return 1;
34}

References lua_pushnumber(), and luaL_checknumber().

◆ math_sinh()

static int math_sinh ( lua_State * L)
static

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

36 {
37 lua_pushnumber(L, sinh(luaL_checknumber(L, 1)));
38 return 1;
39}

References lua_pushnumber(), and luaL_checknumber().

◆ math_sqrt()

static int math_sqrt ( lua_State * L)
static

Definition at line 104 of file lua-5.1.5/src/lmathlib.c.

104 {
105 lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
106 return 1;
107}

References lua_pushnumber(), and luaL_checknumber().

◆ math_tan()

static int math_tan ( lua_State * L)
static

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

51 {
52 lua_pushnumber(L, tan(luaL_checknumber(L, 1)));
53 return 1;
54}

References lua_pushnumber(), and luaL_checknumber().

◆ math_tanh()

static int math_tanh ( lua_State * L)
static

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

56 {
57 lua_pushnumber(L, tanh(luaL_checknumber(L, 1)));
58 return 1;
59}

References lua_pushnumber(), and luaL_checknumber().

Variable Documentation

◆ mathlib

const luaL_Reg mathlib[]
static

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

215 {
216 {"abs", math_abs},
217 {"acos", math_acos},
218 {"asin", math_asin},
219 {"atan2", math_atan2},
220 {"atan", math_atan},
221 {"ceil", math_ceil},
222 {"cosh", math_cosh},
223 {"cos", math_cos},
224 {"deg", math_deg},
225 {"exp", math_exp},
226 {"floor", math_floor},
227 {"fmod", math_fmod},
228 {"frexp", math_frexp},
229 {"ldexp", math_ldexp},
230 {"log10", math_log10},
231 {"log", math_log},
232 {"max", math_max},
233 {"min", math_min},
234 {"modf", math_modf},
235 {"pow", math_pow},
236 {"rad", math_rad},
237 {"random", math_random},
238 {"randomseed", math_randomseed},
239 {"sinh", math_sinh},
240 {"sin", math_sin},
241 {"sqrt", math_sqrt},
242 {"tanh", math_tanh},
243 {"tan", math_tan},
244 {NULL, NULL}
245};
#define NULL
Definition gmacros.h:924
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_log10(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().