aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-02-19 15:33:35 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-02-19 15:33:35 -0200
commitb5cd7d426fdfea4984234edc4ad1316063bd0917 (patch)
treed72068219da4d8923adf9de254c8cbcf473cbf95
parentbf6d2ccf92768be938aaac0a0bfc87833b793e8f (diff)
downloadlua-b5cd7d426fdfea4984234edc4ad1316063bd0917.tar.gz
lua-b5cd7d426fdfea4984234edc4ad1316063bd0917.tar.bz2
lua-b5cd7d426fdfea4984234edc4ad1316063bd0917.zip
details
-rw-r--r--lmathlib.c88
1 files changed, 35 insertions, 53 deletions
diff --git a/lmathlib.c b/lmathlib.c
index 33af1d14..60582677 100644
--- a/lmathlib.c
+++ b/lmathlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmathlib.c,v 1.14 1998/12/30 21:23:26 roberto Exp $ 2** $Id: lmathlib.c,v 1.15 1999/01/04 12:41:12 roberto Exp roberto $
3** Lua standard mathematical library 3** Lua standard mathematical library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -14,108 +14,93 @@
14 14
15 15
16#define PI (3.14159265358979323846) 16#define PI (3.14159265358979323846)
17#define RADIANS_PER_DEGREE (PI/180.0)
18
17 19
18 20
19/* 21/*
20** If you want Lua to operate in radians (instead of degrees), 22** If you want Lua to operate in radians (instead of degrees),
21** changes these two macros to identities: 23** define RADIANS
22** #define FROMRAD(a) (a)
23** #define TORAD(a) (a)
24*/ 24*/
25#define FROMRAD(a) ((a)*(180.0/PI)) 25#ifdef RADIANS
26#define TORAD(a) ((a)*(PI/180.0)) 26#define FROMRAD(a) (a)
27#define TORAD(a) (a)
28#else
29#define FROMRAD(a) ((a)/RADIANS_PER_DEGREE)
30#define TORAD(a) ((a)*RADIANS_PER_DEGREE)
31#endif
27 32
28 33
29static void math_abs (void) 34static void math_abs (void) {
30{ 35 lua_pushnumber(fabs(luaL_check_number(1)));
31 double d = luaL_check_number(1);
32 if (d < 0) d = -d;
33 lua_pushnumber(d);
34} 36}
35 37
36static void math_sin (void) 38static void math_sin (void) {
37{
38 lua_pushnumber(sin(TORAD(luaL_check_number(1)))); 39 lua_pushnumber(sin(TORAD(luaL_check_number(1))));
39} 40}
40 41
41static void math_cos (void) 42static void math_cos (void) {
42{
43 lua_pushnumber(cos(TORAD(luaL_check_number(1)))); 43 lua_pushnumber(cos(TORAD(luaL_check_number(1))));
44} 44}
45 45
46static void math_tan (void) 46static void math_tan (void) {
47{
48 lua_pushnumber(tan(TORAD(luaL_check_number(1)))); 47 lua_pushnumber(tan(TORAD(luaL_check_number(1))));
49} 48}
50 49
51static void math_asin (void) 50static void math_asin (void) {
52{
53 lua_pushnumber(FROMRAD(asin(luaL_check_number(1)))); 51 lua_pushnumber(FROMRAD(asin(luaL_check_number(1))));
54} 52}
55 53
56static void math_acos (void) 54static void math_acos (void) {
57{
58 lua_pushnumber(FROMRAD(acos(luaL_check_number(1)))); 55 lua_pushnumber(FROMRAD(acos(luaL_check_number(1))));
59} 56}
60 57
61static void math_atan (void) 58static void math_atan (void) {
62{
63 lua_pushnumber(FROMRAD(atan(luaL_check_number(1)))); 59 lua_pushnumber(FROMRAD(atan(luaL_check_number(1))));
64} 60}
65 61
66static void math_atan2 (void) 62static void math_atan2 (void) {
67{
68 lua_pushnumber(FROMRAD(atan2(luaL_check_number(1), luaL_check_number(2)))); 63 lua_pushnumber(FROMRAD(atan2(luaL_check_number(1), luaL_check_number(2))));
69} 64}
70 65
71static void math_ceil (void) 66static void math_ceil (void) {
72{
73 lua_pushnumber(ceil(luaL_check_number(1))); 67 lua_pushnumber(ceil(luaL_check_number(1)));
74} 68}
75 69
76static void math_floor (void) 70static void math_floor (void) {
77{
78 lua_pushnumber(floor(luaL_check_number(1))); 71 lua_pushnumber(floor(luaL_check_number(1)));
79} 72}
80 73
81static void math_mod (void) 74static void math_mod (void) {
82{
83 lua_pushnumber(fmod(luaL_check_number(1), luaL_check_number(2))); 75 lua_pushnumber(fmod(luaL_check_number(1), luaL_check_number(2)));
84} 76}
85 77
86static void math_sqrt (void) 78static void math_sqrt (void) {
87{
88 lua_pushnumber(sqrt(luaL_check_number(1))); 79 lua_pushnumber(sqrt(luaL_check_number(1)));
89} 80}
90 81
91static void math_pow (void) 82static void math_pow (void) {
92{
93 lua_pushnumber(pow(luaL_check_number(1), luaL_check_number(2))); 83 lua_pushnumber(pow(luaL_check_number(1), luaL_check_number(2)));
94} 84}
95 85
96static void math_log (void) 86static void math_log (void) {
97{
98 lua_pushnumber(log(luaL_check_number(1))); 87 lua_pushnumber(log(luaL_check_number(1)));
99} 88}
100 89
101static void math_log10 (void) 90static void math_log10 (void) {
102{
103 lua_pushnumber(log10(luaL_check_number(1))); 91 lua_pushnumber(log10(luaL_check_number(1)));
104} 92}
105 93
106static void math_exp (void) 94static void math_exp (void) {
107{
108 lua_pushnumber(exp(luaL_check_number(1))); 95 lua_pushnumber(exp(luaL_check_number(1)));
109} 96}
110 97
111static void math_deg (void) 98static void math_deg (void) {
112{ 99 lua_pushnumber(luaL_check_number(1)/RADIANS_PER_DEGREE);
113 lua_pushnumber(luaL_check_number(1)*(180.0/PI));
114} 100}
115 101
116static void math_rad (void) 102static void math_rad (void) {
117{ 103 lua_pushnumber(luaL_check_number(1)*RADIANS_PER_DEGREE);
118 lua_pushnumber(luaL_check_number(1)*(PI/180.0));
119} 104}
120 105
121static void math_frexp (void) { 106static void math_frexp (void) {
@@ -130,8 +115,7 @@ static void math_ldexp (void) {
130 115
131 116
132 117
133static void math_min (void) 118static void math_min (void) {
134{
135 int i = 1; 119 int i = 1;
136 double dmin = luaL_check_number(i); 120 double dmin = luaL_check_number(i);
137 while (lua_getparam(++i) != LUA_NOOBJECT) { 121 while (lua_getparam(++i) != LUA_NOOBJECT) {
@@ -143,8 +127,7 @@ static void math_min (void)
143} 127}
144 128
145 129
146static void math_max (void) 130static void math_max (void) {
147{
148 int i = 1; 131 int i = 1;
149 double dmax = luaL_check_number(i); 132 double dmax = luaL_check_number(i);
150 while (lua_getparam(++i) != LUA_NOOBJECT) { 133 while (lua_getparam(++i) != LUA_NOOBJECT) {
@@ -209,8 +192,7 @@ static struct luaL_reg mathlib[] = {
209/* 192/*
210** Open math library 193** Open math library
211*/ 194*/
212void lua_mathlibopen (void) 195void lua_mathlibopen (void) {
213{
214 luaL_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0]))); 196 luaL_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0])));
215 lua_pushcfunction(math_pow); 197 lua_pushcfunction(math_pow);
216 lua_pushnumber(0); /* to get its tag */ 198 lua_pushnumber(0); /* to get its tag */