aboutsummaryrefslogtreecommitdiff
path: root/lmathlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'lmathlib.c')
-rw-r--r--lmathlib.c195
1 files changed, 195 insertions, 0 deletions
diff --git a/lmathlib.c b/lmathlib.c
new file mode 100644
index 00000000..ad50ee96
--- /dev/null
+++ b/lmathlib.c
@@ -0,0 +1,195 @@
1/*
2** $Id: $
3** Lua standard mathematical library
4** See Copyright Notice in lua.h
5*/
6
7
8#include <stdlib.h>
9#include <math.h>
10
11#include "lauxlib.h"
12#include "lua.h"
13#include "lualib.h"
14
15#ifndef PI
16#define PI 3.14159265358979323846
17#endif
18
19#define TODEGREE(a) ((a)*180.0/PI)
20#define TORAD(a) ((a)*PI/180.0)
21
22
23
24static void math_abs (void)
25{
26 double d = luaL_check_number(1);
27 if (d < 0) d = -d;
28 lua_pushnumber(d);
29}
30
31static void math_sin (void)
32{
33 lua_pushnumber(sin(TORAD(luaL_check_number(1))));
34}
35
36static void math_cos (void)
37{
38 lua_pushnumber(cos(TORAD(luaL_check_number(1))));
39}
40
41static void math_tan (void)
42{
43 lua_pushnumber(tan(TORAD(luaL_check_number(1))));
44}
45
46static void math_asin (void)
47{
48 lua_pushnumber(asin(TODEGREE(luaL_check_number(1))));
49}
50
51static void math_acos (void)
52{
53 lua_pushnumber(acos(TODEGREE(luaL_check_number(1))));
54}
55
56static void math_atan (void)
57{
58 lua_pushnumber(atan(TODEGREE(luaL_check_number(1))));
59}
60
61static void math_atan2 (void)
62{
63 lua_pushnumber(TODEGREE(atan2(luaL_check_number(1), luaL_check_number(2))));
64}
65
66static void math_ceil (void)
67{
68 lua_pushnumber(ceil(luaL_check_number(1)));
69}
70
71static void math_floor (void)
72{
73 lua_pushnumber(floor(luaL_check_number(1)));
74}
75
76static void math_mod (void)
77{
78 lua_pushnumber(fmod(luaL_check_number(1), luaL_check_number(2)));
79}
80
81static void math_sqrt (void)
82{
83 lua_pushnumber(sqrt(luaL_check_number(1)));
84}
85
86static void math_pow (void)
87{
88 lua_pushnumber(pow(luaL_check_number(1), luaL_check_number(2)));
89}
90
91static void math_log (void)
92{
93 lua_pushnumber(log(luaL_check_number(1)));
94}
95
96static void math_log10 (void)
97{
98 lua_pushnumber(log10(luaL_check_number(1)));
99}
100
101static void math_exp (void)
102{
103 lua_pushnumber(exp(luaL_check_number(1)));
104}
105
106static void math_deg (void)
107{
108 lua_pushnumber(luaL_check_number(1)*180./PI);
109}
110
111static void math_rad (void)
112{
113 lua_pushnumber(luaL_check_number(1)/180.*PI);
114}
115
116
117
118static void math_min (void)
119{
120 int i = 1;
121 double dmin = luaL_check_number(i);
122 while (lua_getparam(++i) != LUA_NOOBJECT) {
123 double d = luaL_check_number(i);
124 if (d < dmin)
125 dmin = d;
126 }
127 lua_pushnumber(dmin);
128}
129
130
131static void math_max (void)
132{
133 int i = 1;
134 double dmax = luaL_check_number(i);
135 while (lua_getparam(++i) != LUA_NOOBJECT) {
136 double d = luaL_check_number(i);
137 if (d > dmax)
138 dmax = d;
139 }
140 lua_pushnumber(dmax);
141}
142
143
144static void math_random (void)
145{
146 double l = luaL_opt_number(1, 0);
147 double r = (double)rand() / (double)RAND_MAX;
148 if (l == 0)
149 lua_pushnumber(r);
150 else
151 lua_pushnumber((int)(r*l)+1);
152}
153
154
155static void math_randomseed (void)
156{
157 srand(luaL_check_number(1));
158}
159
160
161static struct luaL_reg mathlib[] = {
162{"abs", math_abs},
163{"sin", math_sin},
164{"cos", math_cos},
165{"tan", math_tan},
166{"asin", math_asin},
167{"acos", math_acos},
168{"atan", math_atan},
169{"atan2", math_atan2},
170{"ceil", math_ceil},
171{"floor", math_floor},
172{"mod", math_mod},
173{"sqrt", math_sqrt},
174{"min", math_min},
175{"max", math_max},
176{"log", math_log},
177{"log10", math_log10},
178{"exp", math_exp},
179{"deg", math_deg},
180{"rad", math_rad},
181{"random", math_random},
182{"randomseed", math_randomseed}
183};
184
185/*
186** Open math library
187*/
188void lua_mathlibopen (void)
189{
190 luaL_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0])));
191 lua_pushcfunction(math_pow);
192 lua_pushnumber(0); /* to get its tag */
193 lua_settagmethod(lua_tag(lua_pop()), "pow");
194}
195