Lua 5.1.4: lmathlib.c
L0001
L0006
L0007
L0008 #include <stdlib.h>
L0009 #include <math.h>
L0010
L0011 #define lmathlib_c
L0012 #define LUA_LIB
L0013
L0014 #include "lua.h"
L0015
L0016 #include "lauxlib.h"
L0017 #include "lualib.h"
L0018
L0019
L0020 #undef PI
L0021 #define PI (3.14159265358979323846)
L0022 #define RADIANS_PER_DEGREE (PI/180.0)
L0023
L0024
L0025
L0026 static int math_abs (lua_State *L) {
L0027 lua_pushnumber(L, fabs(luaL_checknumber(L, 1)));
L0028 return 1;
L0029 }
L0030
L0031 static int math_sin (lua_State *L) {
L0032 lua_pushnumber(L, sin(luaL_checknumber(L, 1)));
L0033 return 1;
L0034 }
L0035
L0036 static int math_sinh (lua_State *L) {
L0037 lua_pushnumber(L, sinh(luaL_checknumber(L, 1)));
L0038 return 1;
L0039 }
L0040
L0041 static int math_cos (lua_State *L) {
L0042 lua_pushnumber(L, cos(luaL_checknumber(L, 1)));
L0043 return 1;
L0044 }
L0045
L0046 static int math_cosh (lua_State *L) {
L0047 lua_pushnumber(L, cosh(luaL_checknumber(L, 1)));
L0048 return 1;
L0049 }
L0050
L0051 static int math_tan (lua_State *L) {
L0052 lua_pushnumber(L, tan(luaL_checknumber(L, 1)));
L0053 return 1;
L0054 }
L0055
L0056 static int math_tanh (lua_State *L) {
L0057 lua_pushnumber(L, tanh(luaL_checknumber(L, 1)));
L0058 return 1;
L0059 }
L0060
L0061 static int math_asin (lua_State *L) {
L0062 lua_pushnumber(L, asin(luaL_checknumber(L, 1)));
L0063 return 1;
L0064 }
L0065
L0066 static int math_acos (lua_State *L) {
L0067 lua_pushnumber(L, acos(luaL_checknumber(L, 1)));
L0068 return 1;
L0069 }
L0070
L0071 static int math_atan (lua_State *L) {
L0072 lua_pushnumber(L, atan(luaL_checknumber(L, 1)));
L0073 return 1;
L0074 }
L0075
L0076 static int math_atan2 (lua_State *L) {
L0077 lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
L0078 return 1;
L0079 }
L0080
L0081 static int math_ceil (lua_State *L) {
L0082 lua_pushnumber(L, ceil(luaL_checknumber(L, 1)));
L0083 return 1;
L0084 }
L0085
L0086 static int math_floor (lua_State *L) {
L0087 lua_pushnumber(L, floor(luaL_checknumber(L, 1)));
L0088 return 1;
L0089 }
L0090
L0091 static int math_fmod (lua_State *L) {
L0092 lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
L0093 return 1;
L0094 }
L0095
L0096 static int math_modf (lua_State *L) {
L0097 double ip;
L0098 double fp = modf(luaL_checknumber(L, 1), &ip);
L0099 lua_pushnumber(L, ip);
L0100 lua_pushnumber(L, fp);
L0101 return 2;
L0102 }
L0103
L0104 static int math_sqrt (lua_State *L) {
L0105 lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
L0106 return 1;
L0107 }
L0108
L0109 static int math_pow (lua_State *L) {
L0110 lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
L0111 return 1;
L0112 }
L0113
L0114 static int math_log (lua_State *L) {
L0115 lua_pushnumber(L, log(luaL_checknumber(L, 1)));
L0116 return 1;
L0117 }
L0118
L0119 static int math_log10 (lua_State *L) {
L0120 lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
L0121 return 1;
L0122 }
L0123
L0124 static int math_exp (lua_State *L) {
L0125 lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
L0126 return 1;
L0127 }
L0128
L0129 static int math_deg (lua_State *L) {
L0130 lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE);
L0131 return 1;
L0132 }
L0133
L0134 static int math_rad (lua_State *L) {
L0135 lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE);
L0136 return 1;
L0137 }
L0138
L0139 static int math_frexp (lua_State *L) {
L0140 int e;
L0141 lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));
L0142 lua_pushinteger(L, e);
L0143 return 2;
L0144 }
L0145
L0146 static int math_ldexp (lua_State *L) {
L0147 lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));
L0148 return 1;
L0149 }
L0150
L0151
L0152
L0153 static int math_min (lua_State *L) {
L0154 int n = lua_gettop(L);
L0155 lua_Number dmin = luaL_checknumber(L, 1);
L0156 int i;
L0157 for (i=2; i<=n; i++) {
L0158 lua_Number d = luaL_checknumber(L, i);
L0159 if (d < dmin)
L0160 dmin = d;
L0161 }
L0162 lua_pushnumber(L, dmin);
L0163 return 1;
L0164 }
L0165
L0166
L0167 static int math_max (lua_State *L) {
L0168 int n = lua_gettop(L);
L0169 lua_Number dmax = luaL_checknumber(L, 1);
L0170 int i;
L0171 for (i=2; i<=n; i++) {
L0172 lua_Number d = luaL_checknumber(L, i);
L0173 if (d > dmax)
L0174 dmax = d;
L0175 }
L0176 lua_pushnumber(L, dmax);
L0177 return 1;
L0178 }
L0179
L0180
L0181 static int math_random (lua_State *L) {
L0182
L0184 lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
L0185 switch (lua_gettop(L)) {
L0186 case 0: {
L0187 lua_pushnumber(L, r);
L0188 break;
L0189 }
L0190 case 1: {
L0191 int u = luaL_checkint(L, 1);
L0192 luaL_argcheck(L, 1<=u, 1, "interval is empty");
L0193 lua_pushnumber(L, floor(r*u)+1);
L0194 break;
L0195 }
L0196 case 2: {
L0197 int l = luaL_checkint(L, 1);
L0198 int u = luaL_checkint(L, 2);
L0199 luaL_argcheck(L, l<=u, 2, "interval is empty");
L0200 lua_pushnumber(L, floor(r*(u-l+1))+l);
L0201 break;
L0202 }
L0203 default: return luaL_error(L, "wrong number of arguments");
L0204 }
L0205 return 1;
L0206 }
L0207
L0208
L0209 static int math_randomseed (lua_State *L) {
L0210 srand(luaL_checkint(L, 1));
L0211 return 0;
L0212 }
L0213
L0214
L0215 static const luaL_Reg mathlib[] = {
L0216 {"abs", math_abs},
L0217 {"acos", math_acos},
L0218 {"asin", math_asin},
L0219 {"atan2", math_atan2},
L0220 {"atan", math_atan},
L0221 {"ceil", math_ceil},
L0222 {"cosh", math_cosh},
L0223 {"cos", math_cos},
L0224 {"deg", math_deg},
L0225 {"exp", math_exp},
L0226 {"floor", math_floor},
L0227 {"fmod", math_fmod},
L0228 {"frexp", math_frexp},
L0229 {"ldexp", math_ldexp},
L0230 {"log10", math_log10},
L0231 {"log", math_log},
L0232 {"max", math_max},
L0233 {"min", math_min},
L0234 {"modf", math_modf},
L0235 {"pow", math_pow},
L0236 {"rad", math_rad},
L0237 {"random", math_random},
L0238 {"randomseed", math_randomseed},
L0239 {"sinh", math_sinh},
L0240 {"sin", math_sin},
L0241 {"sqrt", math_sqrt},
L0242 {"tanh", math_tanh},
L0243 {"tan", math_tan},
L0244 {NULL, NULL}
L0245 };
L0246
L0247
L0248
L0251 LUALIB_API int luaopen_math (lua_State *L) {
L0252 luaL_register(L, LUA_MATHLIBNAME, mathlib);
L0253 lua_pushnumber(L, PI);
L0254 lua_setfield(L, -2, "pi");
L0255 lua_pushnumber(L, HUGE_VAL);
L0256 lua_setfield(L, -2, "huge");
L0257 #if defined(LUA_COMPAT_MOD)
L0258 lua_getfield(L, -1, "fmod");
L0259 lua_setfield(L, -2, "mod");
L0260 #endif
L0261 return 1;
L0262 }
L0263
Generated by pretty.lua