annotate lib/lua/lua-5.2.2/lmathlib.c @ 2553:48708da03b7f

Party Creation UI logic separated from MainMenu
author a.parshin
date Wed, 13 May 2015 02:20:05 +0200
parents 41cc4dd3c122
children
rev   line source
1866
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
2 ** $Id: lmathlib.c,v 1.83 2013/03/07 18:21:32 roberto Exp $
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
3 ** Standard mathematical library
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
4 ** See Copyright Notice in lua.h
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
5 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
6
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
7
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
8 #include <stdlib.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
9 #include <math.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
10
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
11 #define lmathlib_c
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
12 #define LUA_LIB
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
13
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
14 #include "lua.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
15
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
16 #include "lauxlib.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
17 #include "lualib.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
18
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
19
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
20 #undef PI
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
21 #define PI ((lua_Number)(3.1415926535897932384626433832795))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
22 #define RADIANS_PER_DEGREE ((lua_Number)(PI/180.0))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
23
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
24
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
25
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
26 static int math_abs (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
27 lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
28 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
29 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
30
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
31 static int math_sin (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
32 lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
33 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
34 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
35
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
36 static int math_sinh (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
37 lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
38 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
39 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
40
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
41 static int math_cos (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
42 lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
43 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
44 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
45
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
46 static int math_cosh (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
47 lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
48 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
49 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
50
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
51 static int math_tan (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
52 lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
53 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
54 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
55
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
56 static int math_tanh (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
57 lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
58 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
59 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
60
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
61 static int math_asin (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
62 lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
63 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
64 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
65
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
66 static int math_acos (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
67 lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
68 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
69 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
70
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
71 static int math_atan (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
72 lua_pushnumber(L, l_mathop(atan)(luaL_checknumber(L, 1)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
73 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
74 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
75
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
76 static int math_atan2 (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
77 lua_pushnumber(L, l_mathop(atan2)(luaL_checknumber(L, 1),
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
78 luaL_checknumber(L, 2)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
79 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
80 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
81
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
82 static int math_ceil (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
83 lua_pushnumber(L, l_mathop(ceil)(luaL_checknumber(L, 1)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
84 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
85 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
86
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
87 static int math_floor (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
88 lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
89 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
90 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
91
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
92 static int math_fmod (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
93 lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1),
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
94 luaL_checknumber(L, 2)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
95 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
96 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
97
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
98 static int math_modf (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
99 lua_Number ip;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
100 lua_Number fp = l_mathop(modf)(luaL_checknumber(L, 1), &ip);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
101 lua_pushnumber(L, ip);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
102 lua_pushnumber(L, fp);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
103 return 2;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
104 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
105
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
106 static int math_sqrt (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
107 lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
108 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
109 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
110
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
111 static int math_pow (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
112 lua_Number x = luaL_checknumber(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
113 lua_Number y = luaL_checknumber(L, 2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
114 lua_pushnumber(L, l_mathop(pow)(x, y));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
115 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
116 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
117
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
118 static int math_log (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
119 lua_Number x = luaL_checknumber(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
120 lua_Number res;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
121 if (lua_isnoneornil(L, 2))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
122 res = l_mathop(log)(x);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
123 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
124 lua_Number base = luaL_checknumber(L, 2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
125 if (base == (lua_Number)10.0) res = l_mathop(log10)(x);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
126 else res = l_mathop(log)(x)/l_mathop(log)(base);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
127 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
128 lua_pushnumber(L, res);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
129 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
130 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
131
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
132 #if defined(LUA_COMPAT_LOG10)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
133 static int math_log10 (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
134 lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
135 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
136 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
137 #endif
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
138
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
139 static int math_exp (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
140 lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
141 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
142 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
143
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
144 static int math_deg (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
145 lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
146 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
147 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
148
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
149 static int math_rad (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
150 lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
151 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
152 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
153
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
154 static int math_frexp (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
155 int e;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
156 lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
157 lua_pushinteger(L, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
158 return 2;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
159 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
160
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
161 static int math_ldexp (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
162 lua_Number x = luaL_checknumber(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
163 int ep = luaL_checkint(L, 2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
164 lua_pushnumber(L, l_mathop(ldexp)(x, ep));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
165 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
166 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
167
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
168
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
169
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
170 static int math_min (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
171 int n = lua_gettop(L); /* number of arguments */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
172 lua_Number dmin = luaL_checknumber(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
173 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
174 for (i=2; i<=n; i++) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
175 lua_Number d = luaL_checknumber(L, i);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
176 if (d < dmin)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
177 dmin = d;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
178 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
179 lua_pushnumber(L, dmin);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
180 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
181 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
182
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
183
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
184 static int math_max (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
185 int n = lua_gettop(L); /* number of arguments */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
186 lua_Number dmax = luaL_checknumber(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
187 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
188 for (i=2; i<=n; i++) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
189 lua_Number d = luaL_checknumber(L, i);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
190 if (d > dmax)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
191 dmax = d;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
192 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
193 lua_pushnumber(L, dmax);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
194 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
195 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
196
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
197
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
198 static int math_random (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
199 /* the `%' avoids the (rare) case of r==1, and is needed also because on
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
200 some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
201 lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
202 switch (lua_gettop(L)) { /* check number of arguments */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
203 case 0: { /* no arguments */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
204 lua_pushnumber(L, r); /* Number between 0 and 1 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
205 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
206 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
207 case 1: { /* only upper limit */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
208 lua_Number u = luaL_checknumber(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
209 luaL_argcheck(L, (lua_Number)1.0 <= u, 1, "interval is empty");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
210 lua_pushnumber(L, l_mathop(floor)(r*u) + (lua_Number)(1.0)); /* [1, u] */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
211 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
212 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
213 case 2: { /* lower and upper limits */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
214 lua_Number l = luaL_checknumber(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
215 lua_Number u = luaL_checknumber(L, 2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
216 luaL_argcheck(L, l <= u, 2, "interval is empty");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
217 lua_pushnumber(L, l_mathop(floor)(r*(u-l+1)) + l); /* [l, u] */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
218 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
219 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
220 default: return luaL_error(L, "wrong number of arguments");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
221 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
222 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
223 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
224
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
225
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
226 static int math_randomseed (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
227 srand(luaL_checkunsigned(L, 1));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
228 (void)rand(); /* discard first value to avoid undesirable correlations */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
229 return 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
230 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
231
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
232
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
233 static const luaL_Reg mathlib[] = {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
234 {"abs", math_abs},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
235 {"acos", math_acos},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
236 {"asin", math_asin},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
237 {"atan2", math_atan2},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
238 {"atan", math_atan},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
239 {"ceil", math_ceil},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
240 {"cosh", math_cosh},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
241 {"cos", math_cos},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
242 {"deg", math_deg},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
243 {"exp", math_exp},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
244 {"floor", math_floor},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
245 {"fmod", math_fmod},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
246 {"frexp", math_frexp},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
247 {"ldexp", math_ldexp},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
248 #if defined(LUA_COMPAT_LOG10)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
249 {"log10", math_log10},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
250 #endif
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
251 {"log", math_log},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
252 {"max", math_max},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
253 {"min", math_min},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
254 {"modf", math_modf},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
255 {"pow", math_pow},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
256 {"rad", math_rad},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
257 {"random", math_random},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
258 {"randomseed", math_randomseed},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
259 {"sinh", math_sinh},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
260 {"sin", math_sin},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
261 {"sqrt", math_sqrt},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
262 {"tanh", math_tanh},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
263 {"tan", math_tan},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
264 {NULL, NULL}
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
265 };
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
266
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
267
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
268 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
269 ** Open math library
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
270 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
271 LUAMOD_API int luaopen_math (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
272 luaL_newlib(L, mathlib);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
273 lua_pushnumber(L, PI);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
274 lua_setfield(L, -2, "pi");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
275 lua_pushnumber(L, HUGE_VAL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
276 lua_setfield(L, -2, "huge");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
277 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
278 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
279