diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1995-10-09 09:48:38 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1995-10-09 09:48:38 -0300 |
| commit | a47e8c7dd061d694b2562deb5995b5084993d0ed (patch) | |
| tree | 7780791a6d22ed9dafd4cd0cf96d713e1ab494ea | |
| parent | 79ce6198763b4418f67ca6519cb5f745022f4a61 (diff) | |
| download | lua-a47e8c7dd061d694b2562deb5995b5084993d0ed.tar.gz lua-a47e8c7dd061d694b2562deb5995b5084993d0ed.tar.bz2 lua-a47e8c7dd061d694b2562deb5995b5084993d0ed.zip | |
small "re-engineering" on parameter checking.
correction of function "atan2".
| -rw-r--r-- | mathlib.c | 187 |
1 files changed, 42 insertions, 145 deletions
| @@ -3,7 +3,7 @@ | |||
| 3 | ** Mathematics library to LUA | 3 | ** Mathematics library to LUA |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | char *rcs_mathlib="$Id: mathlib.c,v 1.10 1995/10/02 17:03:33 roberto Exp roberto $"; | 6 | char *rcs_mathlib="$Id: mathlib.c,v 1.11 1995/10/04 13:52:09 roberto Exp $"; |
| 7 | 7 | ||
| 8 | #include <stdio.h> /* NULL */ | 8 | #include <stdio.h> /* NULL */ |
| 9 | #include <math.h> | 9 | #include <math.h> |
| @@ -17,15 +17,25 @@ char *rcs_mathlib="$Id: mathlib.c,v 1.10 1995/10/02 17:03:33 roberto Exp roberto | |||
| 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 | |||
| 21 | static 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 | |||
| 28 | static 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 | |||
| 20 | static void math_abs (void) | 36 | static void math_abs (void) |
| 21 | { | 37 | { |
| 22 | double d; | 38 | double d = get_and_check_float(1, "abs"); |
| 23 | lua_Object o = lua_getparam (1); | ||
| 24 | if (o == LUA_NOOBJECT) | ||
| 25 | lua_error ("too few arguments to function `abs'"); | ||
| 26 | if (!lua_isnumber(o)) | ||
| 27 | lua_error ("incorrect arguments to function `abs'"); | ||
| 28 | d = lua_getnumber(o); | ||
| 29 | if (d < 0) d = -d; | 39 | if (d < 0) d = -d; |
| 30 | lua_pushnumber (d); | 40 | lua_pushnumber (d); |
| 31 | } | 41 | } |
| @@ -33,13 +43,7 @@ static void math_abs (void) | |||
| 33 | 43 | ||
| 34 | static void math_sin (void) | 44 | static void math_sin (void) |
| 35 | { | 45 | { |
| 36 | double d; | 46 | double d = get_and_check_float(1, "sin"); |
| 37 | lua_Object o = lua_getparam (1); | ||
| 38 | if (o == LUA_NOOBJECT) | ||
| 39 | lua_error ("too few arguments to function `sin'"); | ||
| 40 | if (!lua_isnumber(o)) | ||
| 41 | lua_error ("incorrect arguments to function `sin'"); | ||
| 42 | d = lua_getnumber(o); | ||
| 43 | lua_pushnumber (sin(TORAD(d))); | 47 | lua_pushnumber (sin(TORAD(d))); |
| 44 | } | 48 | } |
| 45 | 49 | ||
| @@ -47,13 +51,7 @@ static void math_sin (void) | |||
| 47 | 51 | ||
| 48 | static void math_cos (void) | 52 | static void math_cos (void) |
| 49 | { | 53 | { |
| 50 | double d; | 54 | double d = get_and_check_float(1, "cos"); |
| 51 | lua_Object o = lua_getparam (1); | ||
| 52 | if (o == LUA_NOOBJECT) | ||
| 53 | lua_error ("too few arguments to function `cos'"); | ||
| 54 | if (!lua_isnumber(o)) | ||
| 55 | lua_error ("incorrect arguments to function `cos'"); | ||
| 56 | d = lua_getnumber(o); | ||
| 57 | lua_pushnumber (cos(TORAD(d))); | 55 | lua_pushnumber (cos(TORAD(d))); |
| 58 | } | 56 | } |
| 59 | 57 | ||
| @@ -61,117 +59,64 @@ static void math_cos (void) | |||
| 61 | 59 | ||
| 62 | static void math_tan (void) | 60 | static void math_tan (void) |
| 63 | { | 61 | { |
| 64 | double d; | 62 | double d = get_and_check_float(1, "tan"); |
| 65 | lua_Object o = lua_getparam (1); | ||
| 66 | if (o == LUA_NOOBJECT) | ||
| 67 | lua_error ("too few arguments to function `tan'"); | ||
| 68 | if (!lua_isnumber(o)) | ||
| 69 | lua_error ("incorrect arguments to function `tan'"); | ||
| 70 | d = lua_getnumber(o); | ||
| 71 | lua_pushnumber (tan(TORAD(d))); | 63 | lua_pushnumber (tan(TORAD(d))); |
| 72 | } | 64 | } |
| 73 | 65 | ||
| 74 | 66 | ||
| 75 | static void math_asin (void) | 67 | static void math_asin (void) |
| 76 | { | 68 | { |
| 77 | double d; | 69 | double d = get_and_check_float(1, "asin"); |
| 78 | lua_Object o = lua_getparam (1); | ||
| 79 | if (o == LUA_NOOBJECT) | ||
| 80 | lua_error ("too few arguments to function `asin'"); | ||
| 81 | if (!lua_isnumber(o)) | ||
| 82 | lua_error ("incorrect arguments to function `asin'"); | ||
| 83 | d = lua_getnumber(o); | ||
| 84 | lua_pushnumber (TODEGREE(asin(d))); | 70 | lua_pushnumber (TODEGREE(asin(d))); |
| 85 | } | 71 | } |
| 86 | 72 | ||
| 87 | 73 | ||
| 88 | static void math_acos (void) | 74 | static void math_acos (void) |
| 89 | { | 75 | { |
| 90 | double d; | 76 | double d = get_and_check_float(1, "acos"); |
| 91 | lua_Object o = lua_getparam (1); | ||
| 92 | if (o == LUA_NOOBJECT) | ||
| 93 | lua_error ("too few arguments to function `acos'"); | ||
| 94 | if (!lua_isnumber(o)) | ||
| 95 | lua_error ("incorrect arguments to function `acos'"); | ||
| 96 | d = lua_getnumber(o); | ||
| 97 | lua_pushnumber (TODEGREE(acos(d))); | 77 | lua_pushnumber (TODEGREE(acos(d))); |
| 98 | } | 78 | } |
| 99 | 79 | ||
| 100 | 80 | ||
| 101 | |||
| 102 | static void math_atan (void) | 81 | static void math_atan (void) |
| 103 | { | 82 | { |
| 104 | double d; | 83 | double d = get_and_check_float(1, "atan"); |
| 105 | lua_Object o = lua_getparam (1); | ||
| 106 | if (o == LUA_NOOBJECT) | ||
| 107 | lua_error ("too few arguments to function `atan'"); | ||
| 108 | if (!lua_isnumber(o)) | ||
| 109 | lua_error ("incorrect arguments to function `atan'"); | ||
| 110 | d = lua_getnumber(o); | ||
| 111 | lua_pushnumber (TODEGREE(atan(d))); | 84 | lua_pushnumber (TODEGREE(atan(d))); |
| 112 | } | 85 | } |
| 113 | 86 | ||
| 114 | 87 | ||
| 115 | static void math_atan2 (void) | 88 | static void math_atan2 (void) |
| 116 | { | 89 | { |
| 117 | int d1, d2; | 90 | double d1 = get_and_check_float(1, "atan2"); |
| 118 | lua_Object o1 = lua_getparam (1); | 91 | double d2 = get_and_check_float(2, "atan2"); |
| 119 | lua_Object o2 = lua_getparam (2); | ||
| 120 | if (!lua_isnumber(o1) || !lua_isnumber(o2)) | ||
| 121 | lua_error ("incorrect arguments to function `atan2'"); | ||
| 122 | d1 = (int) lua_getnumber(o1); | ||
| 123 | d2 = (int) lua_getnumber(o2); | ||
| 124 | lua_pushnumber (TODEGREE(atan2(d1, d2))); | 92 | lua_pushnumber (TODEGREE(atan2(d1, d2))); |
| 125 | } | 93 | } |
| 126 | 94 | ||
| 127 | 95 | ||
| 128 | static void math_ceil (void) | 96 | static void math_ceil (void) |
| 129 | { | 97 | { |
| 130 | double d; | 98 | double d = get_and_check_float(1, "ceil"); |
| 131 | lua_Object o = lua_getparam (1); | ||
| 132 | if (o == LUA_NOOBJECT) | ||
| 133 | lua_error ("too few arguments to function `ceil'"); | ||
| 134 | if (!lua_isnumber(o)) | ||
| 135 | lua_error ("incorrect arguments to function `ceil'"); | ||
| 136 | d = lua_getnumber(o); | ||
| 137 | lua_pushnumber (ceil(d)); | 99 | lua_pushnumber (ceil(d)); |
| 138 | } | 100 | } |
| 139 | 101 | ||
| 140 | 102 | ||
| 141 | static void math_floor (void) | 103 | static void math_floor (void) |
| 142 | { | 104 | { |
| 143 | double d; | 105 | double d = get_and_check_float(1, "floor"); |
| 144 | lua_Object o = lua_getparam (1); | ||
| 145 | if (o == LUA_NOOBJECT) | ||
| 146 | lua_error ("too few arguments to function `floor'"); | ||
| 147 | if (!lua_isnumber(o)) | ||
| 148 | lua_error ("incorrect arguments to function `floor'"); | ||
| 149 | d = lua_getnumber(o); | ||
| 150 | lua_pushnumber (floor(d)); | 106 | lua_pushnumber (floor(d)); |
| 151 | } | 107 | } |
| 152 | 108 | ||
| 153 | static void math_mod (void) | 109 | static void math_mod (void) |
| 154 | { | 110 | { |
| 155 | int d1, d2; | 111 | int d1 = (int)get_and_check_float(1, "mod"); |
| 156 | lua_Object o1 = lua_getparam (1); | 112 | int d2 = (int)get_and_check_float(2, "mod"); |
| 157 | lua_Object o2 = lua_getparam (2); | ||
| 158 | if (!lua_isnumber(o1) || !lua_isnumber(o2)) | ||
| 159 | lua_error ("incorrect arguments to function `mod'"); | ||
| 160 | d1 = (int) lua_getnumber(o1); | ||
| 161 | d2 = (int) lua_getnumber(o2); | ||
| 162 | lua_pushnumber (d1%d2); | 113 | lua_pushnumber (d1%d2); |
| 163 | } | 114 | } |
| 164 | 115 | ||
| 165 | 116 | ||
| 166 | static void math_sqrt (void) | 117 | static void math_sqrt (void) |
| 167 | { | 118 | { |
| 168 | double d; | 119 | double d = get_and_check_float(1, "sqrt"); |
| 169 | lua_Object o = lua_getparam (1); | ||
| 170 | if (o == LUA_NOOBJECT) | ||
| 171 | lua_error ("too few arguments to function `sqrt'"); | ||
| 172 | if (!lua_isnumber(o)) | ||
| 173 | lua_error ("incorrect arguments to function `sqrt'"); | ||
| 174 | d = lua_getnumber(o); | ||
| 175 | lua_pushnumber (sqrt(d)); | 120 | lua_pushnumber (sqrt(d)); |
| 176 | } | 121 | } |
| 177 | 122 | ||
| @@ -202,104 +147,56 @@ static void math_pow (void) | |||
| 202 | static void math_min (void) | 147 | static void math_min (void) |
| 203 | { | 148 | { |
| 204 | int i=1; | 149 | int i=1; |
| 205 | double d, dmin; | 150 | double dmin = get_and_check_float(i, "min"); |
| 206 | lua_Object o; | 151 | while (lua_getparam(++i) != LUA_NOOBJECT) |
| 207 | if ((o = lua_getparam(i++)) == LUA_NOOBJECT) | ||
| 208 | lua_error ("too few arguments to function `min'"); | ||
| 209 | if (!lua_isnumber(o)) | ||
| 210 | lua_error ("incorrect arguments to function `min'"); | ||
| 211 | dmin = lua_getnumber (o); | ||
| 212 | while ((o = lua_getparam(i++)) != LUA_NOOBJECT) | ||
| 213 | { | 152 | { |
| 214 | if (!lua_isnumber(o)) | 153 | double d = get_and_check_float(i, "min"); |
| 215 | lua_error ("incorrect arguments to function `min'"); | ||
| 216 | d = lua_getnumber (o); | ||
| 217 | if (d < dmin) dmin = d; | 154 | if (d < dmin) dmin = d; |
| 218 | } | 155 | } |
| 219 | lua_pushnumber (dmin); | 156 | lua_pushnumber (dmin); |
| 220 | } | 157 | } |
| 221 | 158 | ||
| 222 | |||
| 223 | static void math_max (void) | 159 | static void math_max (void) |
| 224 | { | 160 | { |
| 225 | int i=1; | 161 | int i=1; |
| 226 | double d, dmax; | 162 | double dmax = get_and_check_float(i, "max"); |
| 227 | lua_Object o; | 163 | while (lua_getparam(++i) != LUA_NOOBJECT) |
| 228 | if ((o = lua_getparam(i++)) == LUA_NOOBJECT) | ||
| 229 | lua_error ("too few arguments to function `max'"); | ||
| 230 | if (!lua_isnumber(o)) | ||
| 231 | lua_error ("incorrect arguments to function `max'"); | ||
| 232 | dmax = lua_getnumber (o); | ||
| 233 | while ((o = lua_getparam(i++)) != LUA_NOOBJECT) | ||
| 234 | { | 164 | { |
| 235 | if (!lua_isnumber(o)) | 165 | double d = get_and_check_float(i, "max"); |
| 236 | lua_error ("incorrect arguments to function `max'"); | ||
| 237 | d = lua_getnumber (o); | ||
| 238 | if (d > dmax) dmax = d; | 166 | if (d > dmax) dmax = d; |
| 239 | } | 167 | } |
| 240 | lua_pushnumber (dmax); | 168 | lua_pushnumber (dmax); |
| 241 | } | 169 | } |
| 242 | 170 | ||
| 243 | |||
| 244 | static void math_log (void) | 171 | static void math_log (void) |
| 245 | { | 172 | { |
| 246 | double d; | 173 | double d = get_and_check_float(1, "log"); |
| 247 | lua_Object o = lua_getparam (1); | ||
| 248 | if (o == LUA_NOOBJECT) | ||
| 249 | lua_error ("too few arguments to function `log'"); | ||
| 250 | if (!lua_isnumber(o)) | ||
| 251 | lua_error ("incorrect arguments to function `log'"); | ||
| 252 | d = lua_getnumber(o); | ||
| 253 | lua_pushnumber (log(d)); | 174 | lua_pushnumber (log(d)); |
| 254 | } | 175 | } |
| 255 | 176 | ||
| 256 | 177 | ||
| 257 | static void math_log10 (void) | 178 | static void math_log10 (void) |
| 258 | { | 179 | { |
| 259 | double d; | 180 | double d = get_and_check_float(1, "log10"); |
| 260 | lua_Object o = lua_getparam (1); | ||
| 261 | if (o == LUA_NOOBJECT) | ||
| 262 | lua_error ("too few arguments to function `log10'"); | ||
| 263 | if (!lua_isnumber(o)) | ||
| 264 | lua_error ("incorrect arguments to function `log10'"); | ||
| 265 | d = lua_getnumber(o); | ||
| 266 | lua_pushnumber (log10(d)); | 181 | lua_pushnumber (log10(d)); |
| 267 | } | 182 | } |
| 268 | 183 | ||
| 269 | 184 | ||
| 270 | static void math_exp (void) | 185 | static void math_exp (void) |
| 271 | { | 186 | { |
| 272 | double d; | 187 | double d = get_and_check_float(1, "exp"); |
| 273 | lua_Object o = lua_getparam (1); | ||
| 274 | if (o == LUA_NOOBJECT) | ||
| 275 | lua_error ("too few arguments to function `exp'"); | ||
| 276 | if (!lua_isnumber(o)) | ||
| 277 | lua_error ("incorrect arguments to function `exp'"); | ||
| 278 | d = lua_getnumber(o); | ||
| 279 | lua_pushnumber (exp(d)); | 188 | lua_pushnumber (exp(d)); |
| 280 | } | 189 | } |
| 281 | 190 | ||
| 282 | static void math_deg (void) | 191 | static void math_deg (void) |
| 283 | { | 192 | { |
| 284 | float d; | 193 | float d = get_and_check_float(1, "deg"); |
| 285 | lua_Object o = lua_getparam (1); | ||
| 286 | if (o == LUA_NOOBJECT) | ||
| 287 | lua_error ("too few arguments to function `deg'"); | ||
| 288 | if (!lua_isnumber(o)) | ||
| 289 | lua_error ("incorrect arguments to function `deg'"); | ||
| 290 | d = lua_getnumber(o); | ||
| 291 | lua_pushnumber (d*180./PI); | 194 | lua_pushnumber (d*180./PI); |
| 292 | } | 195 | } |
| 293 | 196 | ||
| 294 | static void math_rad (void) | 197 | static void math_rad (void) |
| 295 | { | 198 | { |
| 296 | float d; | 199 | float d = get_and_check_float(1, "rad"); |
| 297 | lua_Object o = lua_getparam (1); | ||
| 298 | if (o == LUA_NOOBJECT) | ||
| 299 | lua_error ("too few arguments to function `rad'"); | ||
| 300 | if (!lua_isnumber(o)) | ||
| 301 | lua_error ("incorrect arguments to function `rad'"); | ||
| 302 | d = lua_getnumber(o); | ||
| 303 | lua_pushnumber (d/180.*PI); | 200 | lua_pushnumber (d/180.*PI); |
| 304 | } | 201 | } |
| 305 | 202 | ||
