diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-01-29 14:00:40 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-01-29 14:00:40 -0200 |
commit | 181a837cac39fe9a411b64d67239d98785518c33 (patch) | |
tree | a152450c524d9aef4cde0f667fdb53677b64fb4f /lmathlib.c | |
parent | 0730a56d38241d70f49cc6fe650c4995fe7dcb2c (diff) | |
download | lua-181a837cac39fe9a411b64d67239d98785518c33.tar.gz lua-181a837cac39fe9a411b64d67239d98785518c33.tar.bz2 lua-181a837cac39fe9a411b64d67239d98785518c33.zip |
small improvement in the support of 'float' as lua_Number
Diffstat (limited to 'lmathlib.c')
-rw-r--r-- | lmathlib.c | 72 |
1 files changed, 34 insertions, 38 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lmathlib.c,v 1.80 2011/07/05 12:49:35 roberto Exp roberto $ | 2 | ** $Id: lmathlib.c,v 1.81 2012/05/18 17:47:53 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 | */ |
@@ -17,106 +17,101 @@ | |||
17 | #include "lualib.h" | 17 | #include "lualib.h" |
18 | 18 | ||
19 | 19 | ||
20 | /* macro 'l_tg' allows the addition of an 'l' or 'f' to all math operations */ | ||
21 | #if !defined(l_tg) | ||
22 | #define l_tg(x) (x) | ||
23 | #endif | ||
24 | |||
25 | |||
26 | #undef PI | 20 | #undef PI |
27 | #define PI (l_tg(3.1415926535897932384626433832795)) | 21 | #define PI ((lua_Number)(3.1415926535897932384626433832795)) |
28 | #define RADIANS_PER_DEGREE (PI/180.0) | 22 | #define RADIANS_PER_DEGREE ((lua_Number)(PI/180.0)) |
29 | 23 | ||
30 | 24 | ||
31 | 25 | ||
32 | static int math_abs (lua_State *L) { | 26 | static int math_abs (lua_State *L) { |
33 | lua_pushnumber(L, l_tg(fabs)(luaL_checknumber(L, 1))); | 27 | lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1))); |
34 | return 1; | 28 | return 1; |
35 | } | 29 | } |
36 | 30 | ||
37 | static int math_sin (lua_State *L) { | 31 | static int math_sin (lua_State *L) { |
38 | lua_pushnumber(L, l_tg(sin)(luaL_checknumber(L, 1))); | 32 | lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1))); |
39 | return 1; | 33 | return 1; |
40 | } | 34 | } |
41 | 35 | ||
42 | static int math_sinh (lua_State *L) { | 36 | static int math_sinh (lua_State *L) { |
43 | lua_pushnumber(L, l_tg(sinh)(luaL_checknumber(L, 1))); | 37 | lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1))); |
44 | return 1; | 38 | return 1; |
45 | } | 39 | } |
46 | 40 | ||
47 | static int math_cos (lua_State *L) { | 41 | static int math_cos (lua_State *L) { |
48 | lua_pushnumber(L, l_tg(cos)(luaL_checknumber(L, 1))); | 42 | lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1))); |
49 | return 1; | 43 | return 1; |
50 | } | 44 | } |
51 | 45 | ||
52 | static int math_cosh (lua_State *L) { | 46 | static int math_cosh (lua_State *L) { |
53 | lua_pushnumber(L, l_tg(cosh)(luaL_checknumber(L, 1))); | 47 | lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1))); |
54 | return 1; | 48 | return 1; |
55 | } | 49 | } |
56 | 50 | ||
57 | static int math_tan (lua_State *L) { | 51 | static int math_tan (lua_State *L) { |
58 | lua_pushnumber(L, l_tg(tan)(luaL_checknumber(L, 1))); | 52 | lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1))); |
59 | return 1; | 53 | return 1; |
60 | } | 54 | } |
61 | 55 | ||
62 | static int math_tanh (lua_State *L) { | 56 | static int math_tanh (lua_State *L) { |
63 | lua_pushnumber(L, l_tg(tanh)(luaL_checknumber(L, 1))); | 57 | lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1))); |
64 | return 1; | 58 | return 1; |
65 | } | 59 | } |
66 | 60 | ||
67 | static int math_asin (lua_State *L) { | 61 | static int math_asin (lua_State *L) { |
68 | lua_pushnumber(L, l_tg(asin)(luaL_checknumber(L, 1))); | 62 | lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1))); |
69 | return 1; | 63 | return 1; |
70 | } | 64 | } |
71 | 65 | ||
72 | static int math_acos (lua_State *L) { | 66 | static int math_acos (lua_State *L) { |
73 | lua_pushnumber(L, l_tg(acos)(luaL_checknumber(L, 1))); | 67 | lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1))); |
74 | return 1; | 68 | return 1; |
75 | } | 69 | } |
76 | 70 | ||
77 | static int math_atan (lua_State *L) { | 71 | static int math_atan (lua_State *L) { |
78 | lua_pushnumber(L, l_tg(atan)(luaL_checknumber(L, 1))); | 72 | lua_pushnumber(L, l_mathop(atan)(luaL_checknumber(L, 1))); |
79 | return 1; | 73 | return 1; |
80 | } | 74 | } |
81 | 75 | ||
82 | static int math_atan2 (lua_State *L) { | 76 | static int math_atan2 (lua_State *L) { |
83 | lua_pushnumber(L, l_tg(atan2)(luaL_checknumber(L, 1), | 77 | lua_pushnumber(L, l_mathop(atan2)(luaL_checknumber(L, 1), |
84 | luaL_checknumber(L, 2))); | 78 | luaL_checknumber(L, 2))); |
85 | return 1; | 79 | return 1; |
86 | } | 80 | } |
87 | 81 | ||
88 | static int math_ceil (lua_State *L) { | 82 | static int math_ceil (lua_State *L) { |
89 | lua_pushnumber(L, l_tg(ceil)(luaL_checknumber(L, 1))); | 83 | lua_pushnumber(L, l_mathop(ceil)(luaL_checknumber(L, 1))); |
90 | return 1; | 84 | return 1; |
91 | } | 85 | } |
92 | 86 | ||
93 | static int math_floor (lua_State *L) { | 87 | static int math_floor (lua_State *L) { |
94 | lua_pushnumber(L, l_tg(floor)(luaL_checknumber(L, 1))); | 88 | lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1))); |
95 | return 1; | 89 | return 1; |
96 | } | 90 | } |
97 | 91 | ||
98 | static int math_fmod (lua_State *L) { | 92 | static int math_fmod (lua_State *L) { |
99 | lua_pushnumber(L, l_tg(fmod)(luaL_checknumber(L, 1), | 93 | lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1), |
100 | luaL_checknumber(L, 2))); | 94 | luaL_checknumber(L, 2))); |
101 | return 1; | 95 | return 1; |
102 | } | 96 | } |
103 | 97 | ||
104 | static int math_modf (lua_State *L) { | 98 | static int math_modf (lua_State *L) { |
105 | lua_Number ip; | 99 | lua_Number ip; |
106 | lua_Number fp = l_tg(modf)(luaL_checknumber(L, 1), &ip); | 100 | lua_Number fp = l_mathop(modf)(luaL_checknumber(L, 1), &ip); |
107 | lua_pushnumber(L, ip); | 101 | lua_pushnumber(L, ip); |
108 | lua_pushnumber(L, fp); | 102 | lua_pushnumber(L, fp); |
109 | return 2; | 103 | return 2; |
110 | } | 104 | } |
111 | 105 | ||
112 | static int math_sqrt (lua_State *L) { | 106 | static int math_sqrt (lua_State *L) { |
113 | lua_pushnumber(L, l_tg(sqrt)(luaL_checknumber(L, 1))); | 107 | lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1))); |
114 | return 1; | 108 | return 1; |
115 | } | 109 | } |
116 | 110 | ||
117 | static int math_pow (lua_State *L) { | 111 | static int math_pow (lua_State *L) { |
118 | lua_pushnumber(L, l_tg(pow)(luaL_checknumber(L, 1), | 112 | lua_Number x = luaL_checknumber(L, 1); |
119 | luaL_checknumber(L, 2))); | 113 | lua_Number y = luaL_checknumber(L, 2); |
114 | lua_pushnumber(L, l_mathop(pow)(x, y)); | ||
120 | return 1; | 115 | return 1; |
121 | } | 116 | } |
122 | 117 | ||
@@ -124,11 +119,11 @@ static int math_log (lua_State *L) { | |||
124 | lua_Number x = luaL_checknumber(L, 1); | 119 | lua_Number x = luaL_checknumber(L, 1); |
125 | lua_Number res; | 120 | lua_Number res; |
126 | if (lua_isnoneornil(L, 2)) | 121 | if (lua_isnoneornil(L, 2)) |
127 | res = l_tg(log)(x); | 122 | res = l_mathop(log)(x); |
128 | else { | 123 | else { |
129 | lua_Number base = luaL_checknumber(L, 2); | 124 | lua_Number base = luaL_checknumber(L, 2); |
130 | if (base == 10.0) res = l_tg(log10)(x); | 125 | if (base == (lua_Number)10.0) res = l_mathop(log10)(x); |
131 | else res = l_tg(log)(x)/l_tg(log)(base); | 126 | else res = l_mathop(log)(x)/l_mathop(log)(base); |
132 | } | 127 | } |
133 | lua_pushnumber(L, res); | 128 | lua_pushnumber(L, res); |
134 | return 1; | 129 | return 1; |
@@ -136,13 +131,13 @@ static int math_log (lua_State *L) { | |||
136 | 131 | ||
137 | #if defined(LUA_COMPAT_LOG10) | 132 | #if defined(LUA_COMPAT_LOG10) |
138 | static int math_log10 (lua_State *L) { | 133 | static int math_log10 (lua_State *L) { |
139 | lua_pushnumber(L, l_tg(log10)(luaL_checknumber(L, 1))); | 134 | lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1))); |
140 | return 1; | 135 | return 1; |
141 | } | 136 | } |
142 | #endif | 137 | #endif |
143 | 138 | ||
144 | static int math_exp (lua_State *L) { | 139 | static int math_exp (lua_State *L) { |
145 | lua_pushnumber(L, l_tg(exp)(luaL_checknumber(L, 1))); | 140 | lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1))); |
146 | return 1; | 141 | return 1; |
147 | } | 142 | } |
148 | 143 | ||
@@ -158,14 +153,15 @@ static int math_rad (lua_State *L) { | |||
158 | 153 | ||
159 | static int math_frexp (lua_State *L) { | 154 | static int math_frexp (lua_State *L) { |
160 | int e; | 155 | int e; |
161 | lua_pushnumber(L, l_tg(frexp)(luaL_checknumber(L, 1), &e)); | 156 | lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e)); |
162 | lua_pushinteger(L, e); | 157 | lua_pushinteger(L, e); |
163 | return 2; | 158 | return 2; |
164 | } | 159 | } |
165 | 160 | ||
166 | static int math_ldexp (lua_State *L) { | 161 | static int math_ldexp (lua_State *L) { |
167 | lua_pushnumber(L, l_tg(ldexp)(luaL_checknumber(L, 1), | 162 | lua_Number x = luaL_checknumber(L, 1); |
168 | luaL_checkint(L, 2))); | 163 | lua_Number ep = luaL_checknumber(L, 2); |
164 | lua_pushnumber(L, l_mathop(ldexp)(x, ep)); | ||
169 | return 1; | 165 | return 1; |
170 | } | 166 | } |
171 | 167 | ||
@@ -210,15 +206,15 @@ static int math_random (lua_State *L) { | |||
210 | } | 206 | } |
211 | case 1: { /* only upper limit */ | 207 | case 1: { /* only upper limit */ |
212 | lua_Number u = luaL_checknumber(L, 1); | 208 | lua_Number u = luaL_checknumber(L, 1); |
213 | luaL_argcheck(L, 1.0 <= u, 1, "interval is empty"); | 209 | luaL_argcheck(L, (lua_Number)1.0 <= u, 1, "interval is empty"); |
214 | lua_pushnumber(L, l_tg(floor)(r*u) + 1.0); /* int in [1, u] */ | 210 | lua_pushnumber(L, l_mathop(floor)(r*u) + (lua_Number)(1.0)); /* [1, u] */ |
215 | break; | 211 | break; |
216 | } | 212 | } |
217 | case 2: { /* lower and upper limits */ | 213 | case 2: { /* lower and upper limits */ |
218 | lua_Number l = luaL_checknumber(L, 1); | 214 | lua_Number l = luaL_checknumber(L, 1); |
219 | lua_Number u = luaL_checknumber(L, 2); | 215 | lua_Number u = luaL_checknumber(L, 2); |
220 | luaL_argcheck(L, l <= u, 2, "interval is empty"); | 216 | luaL_argcheck(L, l <= u, 2, "interval is empty"); |
221 | lua_pushnumber(L, l_tg(floor)(r*(u-l+1)) + l); /* int in [l, u] */ | 217 | lua_pushnumber(L, l_mathop(floor)(r*(u-l+1)) + l); /* [l, u] */ |
222 | break; | 218 | break; |
223 | } | 219 | } |
224 | default: return luaL_error(L, "wrong number of arguments"); | 220 | default: return luaL_error(L, "wrong number of arguments"); |