diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-09-16 16:25:59 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-09-16 16:25:59 -0300 |
| commit | b945fae40e270c8085ccdd8485bb01121f0a8406 (patch) | |
| tree | 9c9bf7d1e29987a2db72e1ec65116a393838a205 /lmathlib.c | |
| parent | dadba4d6ed9f7432185816abcbb788125aa991ff (diff) | |
| download | lua-b945fae40e270c8085ccdd8485bb01121f0a8406.tar.gz lua-b945fae40e270c8085ccdd8485bb01121f0a8406.tar.bz2 lua-b945fae40e270c8085ccdd8485bb01121f0a8406.zip | |
Lua standard mathematical library
Diffstat (limited to 'lmathlib.c')
| -rw-r--r-- | lmathlib.c | 195 |
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 | |||
| 24 | static void math_abs (void) | ||
| 25 | { | ||
| 26 | double d = luaL_check_number(1); | ||
| 27 | if (d < 0) d = -d; | ||
| 28 | lua_pushnumber(d); | ||
| 29 | } | ||
| 30 | |||
| 31 | static void math_sin (void) | ||
| 32 | { | ||
| 33 | lua_pushnumber(sin(TORAD(luaL_check_number(1)))); | ||
| 34 | } | ||
| 35 | |||
| 36 | static void math_cos (void) | ||
| 37 | { | ||
| 38 | lua_pushnumber(cos(TORAD(luaL_check_number(1)))); | ||
| 39 | } | ||
| 40 | |||
| 41 | static void math_tan (void) | ||
| 42 | { | ||
| 43 | lua_pushnumber(tan(TORAD(luaL_check_number(1)))); | ||
| 44 | } | ||
| 45 | |||
| 46 | static void math_asin (void) | ||
| 47 | { | ||
| 48 | lua_pushnumber(asin(TODEGREE(luaL_check_number(1)))); | ||
| 49 | } | ||
| 50 | |||
| 51 | static void math_acos (void) | ||
| 52 | { | ||
| 53 | lua_pushnumber(acos(TODEGREE(luaL_check_number(1)))); | ||
| 54 | } | ||
| 55 | |||
| 56 | static void math_atan (void) | ||
| 57 | { | ||
| 58 | lua_pushnumber(atan(TODEGREE(luaL_check_number(1)))); | ||
| 59 | } | ||
| 60 | |||
| 61 | static void math_atan2 (void) | ||
| 62 | { | ||
| 63 | lua_pushnumber(TODEGREE(atan2(luaL_check_number(1), luaL_check_number(2)))); | ||
| 64 | } | ||
| 65 | |||
| 66 | static void math_ceil (void) | ||
| 67 | { | ||
| 68 | lua_pushnumber(ceil(luaL_check_number(1))); | ||
| 69 | } | ||
| 70 | |||
| 71 | static void math_floor (void) | ||
| 72 | { | ||
| 73 | lua_pushnumber(floor(luaL_check_number(1))); | ||
| 74 | } | ||
| 75 | |||
| 76 | static void math_mod (void) | ||
| 77 | { | ||
| 78 | lua_pushnumber(fmod(luaL_check_number(1), luaL_check_number(2))); | ||
| 79 | } | ||
| 80 | |||
| 81 | static void math_sqrt (void) | ||
| 82 | { | ||
| 83 | lua_pushnumber(sqrt(luaL_check_number(1))); | ||
| 84 | } | ||
| 85 | |||
| 86 | static void math_pow (void) | ||
| 87 | { | ||
| 88 | lua_pushnumber(pow(luaL_check_number(1), luaL_check_number(2))); | ||
| 89 | } | ||
| 90 | |||
| 91 | static void math_log (void) | ||
| 92 | { | ||
| 93 | lua_pushnumber(log(luaL_check_number(1))); | ||
| 94 | } | ||
| 95 | |||
| 96 | static void math_log10 (void) | ||
| 97 | { | ||
| 98 | lua_pushnumber(log10(luaL_check_number(1))); | ||
| 99 | } | ||
| 100 | |||
| 101 | static void math_exp (void) | ||
| 102 | { | ||
| 103 | lua_pushnumber(exp(luaL_check_number(1))); | ||
| 104 | } | ||
| 105 | |||
| 106 | static void math_deg (void) | ||
| 107 | { | ||
| 108 | lua_pushnumber(luaL_check_number(1)*180./PI); | ||
| 109 | } | ||
| 110 | |||
| 111 | static void math_rad (void) | ||
| 112 | { | ||
| 113 | lua_pushnumber(luaL_check_number(1)/180.*PI); | ||
| 114 | } | ||
| 115 | |||
| 116 | |||
| 117 | |||
| 118 | static 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 | |||
| 131 | static 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 | |||
| 144 | static 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 | |||
| 155 | static void math_randomseed (void) | ||
| 156 | { | ||
| 157 | srand(luaL_check_number(1)); | ||
| 158 | } | ||
| 159 | |||
| 160 | |||
| 161 | static 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 | */ | ||
| 188 | void 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 | |||
