annotate src/lua/lmathlib.c @ 513:3dbb863eb801

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