A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 291 lines 6.4 kB view raw
1/* 2** $Id: lmathlib.c,v 1.67.1.1 2007/12/27 13:02:25 roberto Exp $ 3** Standard mathematical library 4** See Copyright Notice in lua.h 5*/ 6 7 8#if 0 9#include <stdlib.h> 10#include <math.h> 11#endif 12 13#define lmathlib_c 14#define LUA_LIB 15 16#include "lua.h" 17 18#include "lauxlib.h" 19#include "lualib.h" 20 21 22#undef PI 23#define PI (3.14159265358979323846) 24#define RADIANS_PER_DEGREE (PI/180.0) 25#define DEGREES_PER_RADIAN (180.0/PI) 26 27 28static int math_abs (lua_State *L) { 29 /* Was: lua_pushnumber(L, fabs(luaL_checknumber(L, 1))); */ 30 lua_Number n = luaL_checknumber(L, 1); 31 lua_pushnumber(L, n < 0 ? -n : n); 32 return 1; 33} 34 35#if 0 36static int math_sin (lua_State *L) { 37 lua_pushnumber(L, sin(luaL_checknumber(L, 1))); 38 return 1; 39} 40 41static int math_sinh (lua_State *L) { 42 lua_pushnumber(L, sinh(luaL_checknumber(L, 1))); 43 return 1; 44} 45 46static int math_cos (lua_State *L) { 47 lua_pushnumber(L, cos(luaL_checknumber(L, 1))); 48 return 1; 49} 50 51static int math_cosh (lua_State *L) { 52 lua_pushnumber(L, cosh(luaL_checknumber(L, 1))); 53 return 1; 54} 55 56static int math_tan (lua_State *L) { 57 lua_pushnumber(L, tan(luaL_checknumber(L, 1))); 58 return 1; 59} 60 61static int math_tanh (lua_State *L) { 62 lua_pushnumber(L, tanh(luaL_checknumber(L, 1))); 63 return 1; 64} 65 66static int math_asin (lua_State *L) { 67 lua_pushnumber(L, asin(luaL_checknumber(L, 1))); 68 return 1; 69} 70 71static int math_acos (lua_State *L) { 72 lua_pushnumber(L, acos(luaL_checknumber(L, 1))); 73 return 1; 74} 75 76static int math_atan (lua_State *L) { 77 lua_pushnumber(L, atan(luaL_checknumber(L, 1))); 78 return 1; 79} 80 81static int math_atan2 (lua_State *L) { 82 lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); 83 return 1; 84} 85 86static int math_ceil (lua_State *L) { 87 lua_pushnumber(L, ceil(luaL_checknumber(L, 1))); 88 return 1; 89} 90 91static int math_floor (lua_State *L) { 92 lua_pushnumber(L, floor(luaL_checknumber(L, 1))); 93 return 1; 94} 95#endif 96 97static int math_fmod (lua_State *L) { 98 /* Was: lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); */ 99 lua_Number n = luaL_checknumber(L, 1); 100 lua_Number d = luaL_checknumber(L, 2); 101 luaL_argcheck(L, d != 0, 2, "division by zero"); 102 lua_pushnumber(L, n % d); 103 return 1; 104} 105 106#if 0 107static int math_modf (lua_State *L) { 108 double ip; 109 double fp = modf(luaL_checknumber(L, 1), &ip); 110 lua_pushnumber(L, ip); 111 lua_pushnumber(L, fp); 112 return 2; 113} 114 115static int math_sqrt (lua_State *L) { 116 lua_pushnumber(L, sqrt(luaL_checknumber(L, 1))); 117 return 1; 118} 119 120static int math_pow (lua_State *L) { 121 lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); 122 return 1; 123} 124 125static int math_log (lua_State *L) { 126 lua_pushnumber(L, log(luaL_checknumber(L, 1))); 127 return 1; 128} 129 130static int math_log10 (lua_State *L) { 131 lua_pushnumber(L, log10(luaL_checknumber(L, 1))); 132 return 1; 133} 134 135static int math_exp (lua_State *L) { 136 lua_pushnumber(L, exp(luaL_checknumber(L, 1))); 137 return 1; 138} 139#endif 140static int math_deg (lua_State *L) { 141 lua_pushnumber(L, luaL_checknumber(L, 1)*DEGREES_PER_RADIAN); 142 return 1; 143} 144static int math_rad (lua_State *L) { 145 lua_pushnumber(L, (luaL_checknumber(L, 1)*100)/(DEGREES_PER_RADIAN*100)); 146 return 1; 147} 148 149#if 0 150static int math_frexp (lua_State *L) { 151 int e; 152 lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e)); 153 lua_pushinteger(L, e); 154 return 2; 155} 156 157static int math_ldexp (lua_State *L) { 158 lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2))); 159 return 1; 160} 161#endif 162 163 164static int math_min (lua_State *L) { 165 int n = lua_gettop(L); /* number of arguments */ 166 lua_Number dmin = luaL_checknumber(L, 1); 167 int i; 168 for (i=2; i<=n; i++) { 169 lua_Number d = luaL_checknumber(L, i); 170 if (d < dmin) 171 dmin = d; 172 } 173 lua_pushnumber(L, dmin); 174 return 1; 175} 176 177 178static int math_max (lua_State *L) { 179 int n = lua_gettop(L); /* number of arguments */ 180 lua_Number dmax = luaL_checknumber(L, 1); 181 int i; 182 for (i=2; i<=n; i++) { 183 lua_Number d = luaL_checknumber(L, i); 184 if (d > dmax) 185 dmax = d; 186 } 187 lua_pushnumber(L, dmax); 188 return 1; 189} 190 191 192static int math_random (lua_State *L) { 193 /* We're not SunOS */ 194 lua_Number r = (lua_Number)(rb->rand()); 195 switch (lua_gettop(L)) { /* check number of arguments */ 196 case 0: { /* no arguments */ 197 lua_pushnumber(L, r); /* Number between 0 and RAND_MAX */ 198 break; 199 } 200 case 1: { /* only upper limit */ 201 int u = luaL_checkint(L, 1); 202 luaL_argcheck(L, 1<=u, 1, "interval is empty"); 203 lua_pushnumber(L, r%u+1); /* int between 1 and `u' */ 204 break; 205 } 206 case 2: { /* lower and upper limits */ 207 int l = luaL_checkint(L, 1); 208 int u = luaL_checkint(L, 2); 209 luaL_argcheck(L, l<=u, 2, "interval is empty"); 210 lua_pushnumber(L, r%(u-l+1)+l); /* int between `l' and `u' */ 211 break; 212 } 213 default: return luaL_error(L, "wrong number of arguments"); 214 } 215 return 1; 216} 217 218 219static int math_randomseed (lua_State *L) { 220 rb->srand(luaL_checkint(L, 1)); 221 return 0; 222} 223 224static int math_ident (lua_State *L) { /* ROCKLUA ADDED */ 225 /* Ceil & floor Doesn't change anything in fixed point arithmetic */ 226 lua_pushnumber(L, luaL_checknumber(L, 1)); 227 return 1; 228} 229 230static const luaL_Reg mathlib[] = { 231 {"abs", math_abs}, 232#if 0 233 {"acos", math_acos}, 234 {"asin", math_asin}, 235 {"atan2", math_atan2}, 236 {"atan", math_atan}, 237 {"ceil", math_ceil}, 238 {"cosh", math_cosh}, 239 {"cos", math_cos}, 240#endif 241 {"deg", math_deg}, 242#if 0 243 {"exp", math_exp}, 244 {"floor", math_floor}, 245#endif 246 {"fmod", math_fmod}, 247#if 0 248 {"frexp", math_frexp}, 249 {"ldexp", math_ldexp}, 250 {"log10", math_log10}, 251 {"log", math_log}, 252#endif 253 {"max", math_max}, 254 {"min", math_min}, 255#if 0 256 {"modf", math_modf}, 257 {"pow", math_pow}, 258#endif 259 {"rad", math_rad}, 260 {"random", math_random}, 261 {"randomseed", math_randomseed}, 262#if 0 263 {"sinh", math_sinh}, 264 {"sin", math_sin}, 265 {"sqrt", math_sqrt}, 266 {"tanh", math_tanh}, 267 {"tan", math_tan}, 268#endif 269 {"ceil", math_ident}, 270 {"floor", math_ident}, 271 {NULL, NULL} 272}; 273 274 275/* 276** Open math library 277*/ 278LUALIB_API int luaopen_math (lua_State *L) { 279 luaL_register(L, LUA_MATHLIBNAME, mathlib); 280#if 0 /* No use in adding floating point constants when there's no FP */ 281 lua_pushnumber(L, PI); 282 lua_setfield(L, -2, "pi"); 283 lua_pushnumber(L, HUGE_VAL); 284 lua_setfield(L, -2, "huge"); 285#if defined(LUA_COMPAT_MOD) 286 lua_getfield(L, -1, "fmod"); 287 lua_setfield(L, -2, "mod"); 288#endif 289#endif 290 return 1; 291}