aboutsummaryrefslogtreecommitdiff
path: root/mathlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1995-11-10 15:54:31 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1995-11-10 15:54:31 -0200
commit5f664a45168803a12b27e9d96f74cad62869c562 (patch)
treeda93f92cb6e0b237957481847023bebdfaff29b0 /mathlib.c
parent87fe07c0d4e2fc5ef973e331da82974d2886e80c (diff)
downloadlua-5f664a45168803a12b27e9d96f74cad62869c562.tar.gz
lua-5f664a45168803a12b27e9d96f74cad62869c562.tar.bz2
lua-5f664a45168803a12b27e9d96f74cad62869c562.zip
error functions are shared by all libraries
Diffstat (limited to 'mathlib.c')
-rw-r--r--mathlib.c64
1 files changed, 24 insertions, 40 deletions
diff --git a/mathlib.c b/mathlib.c
index 9dd39609..1092052a 100644
--- a/mathlib.c
+++ b/mathlib.c
@@ -3,7 +3,7 @@
3** Mathematics library to LUA 3** Mathematics library to LUA
4*/ 4*/
5 5
6char *rcs_mathlib="$Id: mathlib.c,v 1.11 1995/10/04 13:52:09 roberto Exp $"; 6char *rcs_mathlib="$Id: mathlib.c,v 1.12 1995/10/09 12:48:38 roberto Exp roberto $";
7 7
8#include <stdio.h> /* NULL */ 8#include <stdio.h> /* NULL */
9#include <math.h> 9#include <math.h>
@@ -17,25 +17,9 @@ char *rcs_mathlib="$Id: mathlib.c,v 1.11 1995/10/04 13:52:09 roberto Exp $";
17#define TODEGREE(a) ((a)*180.0/PI) 17#define TODEGREE(a) ((a)*180.0/PI)
18#define TORAD(a) ((a)*PI/180.0) 18#define TORAD(a) ((a)*PI/180.0)
19 19
20
21static void str_error(char *funcname)
22{
23 char buff[250];
24 sprintf(buff, "incorrect arguments to function `%s'", funcname);
25 lua_error(buff);
26}
27
28static float get_and_check_float (int numArg, char *funcname)
29{
30 lua_Object o = lua_getparam(numArg);
31 if (!lua_isnumber(o))
32 str_error(funcname);
33 return lua_getnumber(o);
34}
35
36static void math_abs (void) 20static void math_abs (void)
37{ 21{
38 double d = get_and_check_float(1, "abs"); 22 double d = lua_check_number(1, "abs");
39 if (d < 0) d = -d; 23 if (d < 0) d = -d;
40 lua_pushnumber (d); 24 lua_pushnumber (d);
41} 25}
@@ -43,7 +27,7 @@ static void math_abs (void)
43 27
44static void math_sin (void) 28static void math_sin (void)
45{ 29{
46 double d = get_and_check_float(1, "sin"); 30 double d = lua_check_number(1, "sin");
47 lua_pushnumber (sin(TORAD(d))); 31 lua_pushnumber (sin(TORAD(d)));
48} 32}
49 33
@@ -51,7 +35,7 @@ static void math_sin (void)
51 35
52static void math_cos (void) 36static void math_cos (void)
53{ 37{
54 double d = get_and_check_float(1, "cos"); 38 double d = lua_check_number(1, "cos");
55 lua_pushnumber (cos(TORAD(d))); 39 lua_pushnumber (cos(TORAD(d)));
56} 40}
57 41
@@ -59,64 +43,64 @@ static void math_cos (void)
59 43
60static void math_tan (void) 44static void math_tan (void)
61{ 45{
62 double d = get_and_check_float(1, "tan"); 46 double d = lua_check_number(1, "tan");
63 lua_pushnumber (tan(TORAD(d))); 47 lua_pushnumber (tan(TORAD(d)));
64} 48}
65 49
66 50
67static void math_asin (void) 51static void math_asin (void)
68{ 52{
69 double d = get_and_check_float(1, "asin"); 53 double d = lua_check_number(1, "asin");
70 lua_pushnumber (TODEGREE(asin(d))); 54 lua_pushnumber (TODEGREE(asin(d)));
71} 55}
72 56
73 57
74static void math_acos (void) 58static void math_acos (void)
75{ 59{
76 double d = get_and_check_float(1, "acos"); 60 double d = lua_check_number(1, "acos");
77 lua_pushnumber (TODEGREE(acos(d))); 61 lua_pushnumber (TODEGREE(acos(d)));
78} 62}
79 63
80 64
81static void math_atan (void) 65static void math_atan (void)
82{ 66{
83 double d = get_and_check_float(1, "atan"); 67 double d = lua_check_number(1, "atan");
84 lua_pushnumber (TODEGREE(atan(d))); 68 lua_pushnumber (TODEGREE(atan(d)));
85} 69}
86 70
87 71
88static void math_atan2 (void) 72static void math_atan2 (void)
89{ 73{
90 double d1 = get_and_check_float(1, "atan2"); 74 double d1 = lua_check_number(1, "atan2");
91 double d2 = get_and_check_float(2, "atan2"); 75 double d2 = lua_check_number(2, "atan2");
92 lua_pushnumber (TODEGREE(atan2(d1, d2))); 76 lua_pushnumber (TODEGREE(atan2(d1, d2)));
93} 77}
94 78
95 79
96static void math_ceil (void) 80static void math_ceil (void)
97{ 81{
98 double d = get_and_check_float(1, "ceil"); 82 double d = lua_check_number(1, "ceil");
99 lua_pushnumber (ceil(d)); 83 lua_pushnumber (ceil(d));
100} 84}
101 85
102 86
103static void math_floor (void) 87static void math_floor (void)
104{ 88{
105 double d = get_and_check_float(1, "floor"); 89 double d = lua_check_number(1, "floor");
106 lua_pushnumber (floor(d)); 90 lua_pushnumber (floor(d));
107} 91}
108 92
109static void math_mod (void) 93static void math_mod (void)
110{ 94{
111 int d1 = (int)get_and_check_float(1, "mod"); 95 int d1 = (int)lua_check_number(1, "mod");
112 int d2 = (int)get_and_check_float(2, "mod"); 96 int d2 = (int)lua_check_number(2, "mod");
113 lua_pushnumber (d1%d2); 97 lua_pushnumber (d1%d2);
114} 98}
115 99
116 100
117static void math_sqrt (void) 101static void math_sqrt (void)
118{ 102{
119 double d = get_and_check_float(1, "sqrt"); 103 double d = lua_check_number(1, "sqrt");
120 lua_pushnumber (sqrt(d)); 104 lua_pushnumber (sqrt(d));
121} 105}
122 106
@@ -147,10 +131,10 @@ static void math_pow (void)
147static void math_min (void) 131static void math_min (void)
148{ 132{
149 int i=1; 133 int i=1;
150 double dmin = get_and_check_float(i, "min"); 134 double dmin = lua_check_number(i, "min");
151 while (lua_getparam(++i) != LUA_NOOBJECT) 135 while (lua_getparam(++i) != LUA_NOOBJECT)
152 { 136 {
153 double d = get_and_check_float(i, "min"); 137 double d = lua_check_number(i, "min");
154 if (d < dmin) dmin = d; 138 if (d < dmin) dmin = d;
155 } 139 }
156 lua_pushnumber (dmin); 140 lua_pushnumber (dmin);
@@ -159,10 +143,10 @@ static void math_min (void)
159static void math_max (void) 143static void math_max (void)
160{ 144{
161 int i=1; 145 int i=1;
162 double dmax = get_and_check_float(i, "max"); 146 double dmax = lua_check_number(i, "max");
163 while (lua_getparam(++i) != LUA_NOOBJECT) 147 while (lua_getparam(++i) != LUA_NOOBJECT)
164 { 148 {
165 double d = get_and_check_float(i, "max"); 149 double d = lua_check_number(i, "max");
166 if (d > dmax) dmax = d; 150 if (d > dmax) dmax = d;
167 } 151 }
168 lua_pushnumber (dmax); 152 lua_pushnumber (dmax);
@@ -170,33 +154,33 @@ static void math_max (void)
170 154
171static void math_log (void) 155static void math_log (void)
172{ 156{
173 double d = get_and_check_float(1, "log"); 157 double d = lua_check_number(1, "log");
174 lua_pushnumber (log(d)); 158 lua_pushnumber (log(d));
175} 159}
176 160
177 161
178static void math_log10 (void) 162static void math_log10 (void)
179{ 163{
180 double d = get_and_check_float(1, "log10"); 164 double d = lua_check_number(1, "log10");
181 lua_pushnumber (log10(d)); 165 lua_pushnumber (log10(d));
182} 166}
183 167
184 168
185static void math_exp (void) 169static void math_exp (void)
186{ 170{
187 double d = get_and_check_float(1, "exp"); 171 double d = lua_check_number(1, "exp");
188 lua_pushnumber (exp(d)); 172 lua_pushnumber (exp(d));
189} 173}
190 174
191static void math_deg (void) 175static void math_deg (void)
192{ 176{
193 float d = get_and_check_float(1, "deg"); 177 float d = lua_check_number(1, "deg");
194 lua_pushnumber (d*180./PI); 178 lua_pushnumber (d*180./PI);
195} 179}
196 180
197static void math_rad (void) 181static void math_rad (void)
198{ 182{
199 float d = get_and_check_float(1, "rad"); 183 float d = lua_check_number(1, "rad");
200 lua_pushnumber (d/180.*PI); 184 lua_pushnumber (d/180.*PI);
201} 185}
202 186