diff options
| -rw-r--r-- | lmathlib.c | 63 |
1 files changed, 31 insertions, 32 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lmathlib.c,v 1.76 2010/10/25 20:31:11 roberto Exp roberto $ | 2 | ** $Id: lmathlib.c,v 1.77 2010/11/11 15:39:12 roberto Exp roberto $ |
| 3 | ** Standard mathematical library | 3 | ** Standard mathematical library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -22,101 +22,100 @@ | |||
| 22 | #define RADIANS_PER_DEGREE (PI/180.0) | 22 | #define RADIANS_PER_DEGREE (PI/180.0) |
| 23 | 23 | ||
| 24 | 24 | ||
| 25 | /* macro 'l_tvar' allows the addition of an 'l' or 'f' to all | 25 | /* macro 'l_tg' allows the addition of an 'l' or 'f' to all math operations */ |
| 26 | math operations */ | 26 | #if !defined(l_tg) |
| 27 | #if !defined(l_l_tvar) | 27 | #define l_tg(x) (x) |
| 28 | #define l_tvar(x) (x) | ||
| 29 | #endif | 28 | #endif |
| 30 | 29 | ||
| 31 | 30 | ||
| 32 | 31 | ||
| 33 | static int math_abs (lua_State *L) { | 32 | static int math_abs (lua_State *L) { |
| 34 | lua_pushnumber(L, l_tvar(fabs)(luaL_checknumber(L, 1))); | 33 | lua_pushnumber(L, l_tg(fabs)(luaL_checknumber(L, 1))); |
| 35 | return 1; | 34 | return 1; |
| 36 | } | 35 | } |
| 37 | 36 | ||
| 38 | static int math_sin (lua_State *L) { | 37 | static int math_sin (lua_State *L) { |
| 39 | lua_pushnumber(L, l_tvar(sin)(luaL_checknumber(L, 1))); | 38 | lua_pushnumber(L, l_tg(sin)(luaL_checknumber(L, 1))); |
| 40 | return 1; | 39 | return 1; |
| 41 | } | 40 | } |
| 42 | 41 | ||
| 43 | static int math_sinh (lua_State *L) { | 42 | static int math_sinh (lua_State *L) { |
| 44 | lua_pushnumber(L, l_tvar(sinh)(luaL_checknumber(L, 1))); | 43 | lua_pushnumber(L, l_tg(sinh)(luaL_checknumber(L, 1))); |
| 45 | return 1; | 44 | return 1; |
| 46 | } | 45 | } |
| 47 | 46 | ||
| 48 | static int math_cos (lua_State *L) { | 47 | static int math_cos (lua_State *L) { |
| 49 | lua_pushnumber(L, l_tvar(cos)(luaL_checknumber(L, 1))); | 48 | lua_pushnumber(L, l_tg(cos)(luaL_checknumber(L, 1))); |
| 50 | return 1; | 49 | return 1; |
| 51 | } | 50 | } |
| 52 | 51 | ||
| 53 | static int math_cosh (lua_State *L) { | 52 | static int math_cosh (lua_State *L) { |
| 54 | lua_pushnumber(L, l_tvar(cosh)(luaL_checknumber(L, 1))); | 53 | lua_pushnumber(L, l_tg(cosh)(luaL_checknumber(L, 1))); |
| 55 | return 1; | 54 | return 1; |
| 56 | } | 55 | } |
| 57 | 56 | ||
| 58 | static int math_tan (lua_State *L) { | 57 | static int math_tan (lua_State *L) { |
| 59 | lua_pushnumber(L, l_tvar(tan)(luaL_checknumber(L, 1))); | 58 | lua_pushnumber(L, l_tg(tan)(luaL_checknumber(L, 1))); |
| 60 | return 1; | 59 | return 1; |
| 61 | } | 60 | } |
| 62 | 61 | ||
| 63 | static int math_tanh (lua_State *L) { | 62 | static int math_tanh (lua_State *L) { |
| 64 | lua_pushnumber(L, l_tvar(tanh)(luaL_checknumber(L, 1))); | 63 | lua_pushnumber(L, l_tg(tanh)(luaL_checknumber(L, 1))); |
| 65 | return 1; | 64 | return 1; |
| 66 | } | 65 | } |
| 67 | 66 | ||
| 68 | static int math_asin (lua_State *L) { | 67 | static int math_asin (lua_State *L) { |
| 69 | lua_pushnumber(L, l_tvar(asin)(luaL_checknumber(L, 1))); | 68 | lua_pushnumber(L, l_tg(asin)(luaL_checknumber(L, 1))); |
| 70 | return 1; | 69 | return 1; |
| 71 | } | 70 | } |
| 72 | 71 | ||
| 73 | static int math_acos (lua_State *L) { | 72 | static int math_acos (lua_State *L) { |
| 74 | lua_pushnumber(L, l_tvar(acos)(luaL_checknumber(L, 1))); | 73 | lua_pushnumber(L, l_tg(acos)(luaL_checknumber(L, 1))); |
| 75 | return 1; | 74 | return 1; |
| 76 | } | 75 | } |
| 77 | 76 | ||
| 78 | static int math_atan (lua_State *L) { | 77 | static int math_atan (lua_State *L) { |
| 79 | lua_pushnumber(L, l_tvar(atan)(luaL_checknumber(L, 1))); | 78 | lua_pushnumber(L, l_tg(atan)(luaL_checknumber(L, 1))); |
| 80 | return 1; | 79 | return 1; |
| 81 | } | 80 | } |
| 82 | 81 | ||
| 83 | static int math_atan2 (lua_State *L) { | 82 | static int math_atan2 (lua_State *L) { |
| 84 | lua_pushnumber(L, l_tvar(atan2)(luaL_checknumber(L, 1), | 83 | lua_pushnumber(L, l_tg(atan2)(luaL_checknumber(L, 1), |
| 85 | luaL_checknumber(L, 2))); | 84 | luaL_checknumber(L, 2))); |
| 86 | return 1; | 85 | return 1; |
| 87 | } | 86 | } |
| 88 | 87 | ||
| 89 | static int math_ceil (lua_State *L) { | 88 | static int math_ceil (lua_State *L) { |
| 90 | lua_pushnumber(L, l_tvar(ceil)(luaL_checknumber(L, 1))); | 89 | lua_pushnumber(L, l_tg(ceil)(luaL_checknumber(L, 1))); |
| 91 | return 1; | 90 | return 1; |
| 92 | } | 91 | } |
| 93 | 92 | ||
| 94 | static int math_floor (lua_State *L) { | 93 | static int math_floor (lua_State *L) { |
| 95 | lua_pushnumber(L, l_tvar(floor)(luaL_checknumber(L, 1))); | 94 | lua_pushnumber(L, l_tg(floor)(luaL_checknumber(L, 1))); |
| 96 | return 1; | 95 | return 1; |
| 97 | } | 96 | } |
| 98 | 97 | ||
| 99 | static int math_fmod (lua_State *L) { | 98 | static int math_fmod (lua_State *L) { |
| 100 | lua_pushnumber(L, l_tvar(fmod)(luaL_checknumber(L, 1), | 99 | lua_pushnumber(L, l_tg(fmod)(luaL_checknumber(L, 1), |
| 101 | luaL_checknumber(L, 2))); | 100 | luaL_checknumber(L, 2))); |
| 102 | return 1; | 101 | return 1; |
| 103 | } | 102 | } |
| 104 | 103 | ||
| 105 | static int math_modf (lua_State *L) { | 104 | static int math_modf (lua_State *L) { |
| 106 | lua_Number ip; | 105 | lua_Number ip; |
| 107 | lua_Number fp = l_tvar(modf)(luaL_checknumber(L, 1), &ip); | 106 | lua_Number fp = l_tg(modf)(luaL_checknumber(L, 1), &ip); |
| 108 | lua_pushnumber(L, ip); | 107 | lua_pushnumber(L, ip); |
| 109 | lua_pushnumber(L, fp); | 108 | lua_pushnumber(L, fp); |
| 110 | return 2; | 109 | return 2; |
| 111 | } | 110 | } |
| 112 | 111 | ||
| 113 | static int math_sqrt (lua_State *L) { | 112 | static int math_sqrt (lua_State *L) { |
| 114 | lua_pushnumber(L, l_tvar(sqrt)(luaL_checknumber(L, 1))); | 113 | lua_pushnumber(L, l_tg(sqrt)(luaL_checknumber(L, 1))); |
| 115 | return 1; | 114 | return 1; |
| 116 | } | 115 | } |
| 117 | 116 | ||
| 118 | static int math_pow (lua_State *L) { | 117 | static int math_pow (lua_State *L) { |
| 119 | lua_pushnumber(L, l_tvar(pow)(luaL_checknumber(L, 1), | 118 | lua_pushnumber(L, l_tg(pow)(luaL_checknumber(L, 1), |
| 120 | luaL_checknumber(L, 2))); | 119 | luaL_checknumber(L, 2))); |
| 121 | return 1; | 120 | return 1; |
| 122 | } | 121 | } |
| @@ -125,11 +124,11 @@ static int math_log (lua_State *L) { | |||
| 125 | lua_Number x = luaL_checknumber(L, 1); | 124 | lua_Number x = luaL_checknumber(L, 1); |
| 126 | lua_Number res; | 125 | lua_Number res; |
| 127 | if (lua_isnoneornil(L, 2)) | 126 | if (lua_isnoneornil(L, 2)) |
| 128 | res = l_tvar(log)(x); | 127 | res = l_tg(log)(x); |
| 129 | else { | 128 | else { |
| 130 | lua_Number base = luaL_checknumber(L, 2); | 129 | lua_Number base = luaL_checknumber(L, 2); |
| 131 | if (base == 10.0) res = l_tvar(log10)(x); | 130 | if (base == 10.0) res = l_tg(log10)(x); |
| 132 | else res = l_tvar(log)(x)/l_tvar(log)(base); | 131 | else res = l_tg(log)(x)/l_tg(log)(base); |
| 133 | } | 132 | } |
| 134 | lua_pushnumber(L, res); | 133 | lua_pushnumber(L, res); |
| 135 | return 1; | 134 | return 1; |
| @@ -140,12 +139,12 @@ static int math_log10 (lua_State *L) { | |||
| 140 | luaL_error(L, "function " LUA_QL("log10") | 139 | luaL_error(L, "function " LUA_QL("log10") |
| 141 | " is deprecated; use log(x, 10) instead"); | 140 | " is deprecated; use log(x, 10) instead"); |
| 142 | #endif | 141 | #endif |
| 143 | lua_pushnumber(L, l_tvar(log10)(luaL_checknumber(L, 1))); | 142 | lua_pushnumber(L, l_tg(log10)(luaL_checknumber(L, 1))); |
| 144 | return 1; | 143 | return 1; |
| 145 | } | 144 | } |
| 146 | 145 | ||
| 147 | static int math_exp (lua_State *L) { | 146 | static int math_exp (lua_State *L) { |
| 148 | lua_pushnumber(L, l_tvar(exp)(luaL_checknumber(L, 1))); | 147 | lua_pushnumber(L, l_tg(exp)(luaL_checknumber(L, 1))); |
| 149 | return 1; | 148 | return 1; |
| 150 | } | 149 | } |
| 151 | 150 | ||
| @@ -161,14 +160,14 @@ static int math_rad (lua_State *L) { | |||
| 161 | 160 | ||
| 162 | static int math_frexp (lua_State *L) { | 161 | static int math_frexp (lua_State *L) { |
| 163 | int e; | 162 | int e; |
| 164 | lua_pushnumber(L, l_tvar(frexp)(luaL_checknumber(L, 1), &e)); | 163 | lua_pushnumber(L, l_tg(frexp)(luaL_checknumber(L, 1), &e)); |
| 165 | lua_pushinteger(L, e); | 164 | lua_pushinteger(L, e); |
| 166 | return 2; | 165 | return 2; |
| 167 | } | 166 | } |
| 168 | 167 | ||
| 169 | static int math_ldexp (lua_State *L) { | 168 | static int math_ldexp (lua_State *L) { |
| 170 | lua_pushnumber(L, l_tvar(ldexp)(luaL_checknumber(L, 1), | 169 | lua_pushnumber(L, l_tg(ldexp)(luaL_checknumber(L, 1), |
| 171 | luaL_checkint(L, 2))); | 170 | luaL_checkint(L, 2))); |
| 172 | return 1; | 171 | return 1; |
| 173 | } | 172 | } |
| 174 | 173 | ||
| @@ -214,14 +213,14 @@ static int math_random (lua_State *L) { | |||
| 214 | case 1: { /* only upper limit */ | 213 | case 1: { /* only upper limit */ |
| 215 | lua_Number u = luaL_checknumber(L, 1); | 214 | lua_Number u = luaL_checknumber(L, 1); |
| 216 | luaL_argcheck(L, 1.0 <= u, 1, "interval is empty"); | 215 | luaL_argcheck(L, 1.0 <= u, 1, "interval is empty"); |
| 217 | lua_pushnumber(L, l_tvar(floor)(r*u) + 1.0); /* int in [1, u] */ | 216 | lua_pushnumber(L, l_tg(floor)(r*u) + 1.0); /* int in [1, u] */ |
| 218 | break; | 217 | break; |
| 219 | } | 218 | } |
| 220 | case 2: { /* lower and upper limits */ | 219 | case 2: { /* lower and upper limits */ |
| 221 | lua_Number l = luaL_checknumber(L, 1); | 220 | lua_Number l = luaL_checknumber(L, 1); |
| 222 | lua_Number u = luaL_checknumber(L, 2); | 221 | lua_Number u = luaL_checknumber(L, 2); |
| 223 | luaL_argcheck(L, l <= u, 2, "interval is empty"); | 222 | luaL_argcheck(L, l <= u, 2, "interval is empty"); |
| 224 | lua_pushnumber(L, l_tvar(floor)(r*(u-l+1)) + l); /* int in [l, u] */ | 223 | lua_pushnumber(L, l_tg(floor)(r*(u-l+1)) + l); /* int in [l, u] */ |
| 225 | break; | 224 | break; |
| 226 | } | 225 | } |
| 227 | default: return luaL_error(L, "wrong number of arguments"); | 226 | default: return luaL_error(L, "wrong number of arguments"); |
