1
+ use std:: f32:: consts:: E ;
2
+
1
3
use libc:: { c_double, c_int, c_longlong, c_ulong, c_ulonglong, intptr_t, srand} ;
2
4
use rand:: prelude:: * ;
3
5
@@ -7,8 +9,8 @@ use crate::lapi::{
7
9
lua_tointeger, lua_tointegerx, lua_type,
8
10
} ;
9
11
use crate :: lauxlib:: {
10
- luaL_Reg, luaL_argerror, luaL_checkany, luaL_checkinteger, luaL_checknumber, luaL_newlib ,
11
- luaL_optnumber,
12
+ luaL_Reg, luaL_argerror, luaL_checkany, luaL_checkinteger, luaL_checknumber, luaL_error ,
13
+ luaL_newlib , luaL_optnumber,
12
14
} ;
13
15
use crate :: lstate:: lua_State;
14
16
use crate :: types:: { lua_CFunction, lua_Integer, lua_Number, lua_Unsigned, LUA_TNUMBER } ;
@@ -33,28 +35,6 @@ pub const LUA_MININTEGER: c_longlong = LLONG_MIN;
33
35
pub const LUA_MAXINTEGER : c_longlong = LLONG_MAX ;
34
36
pub const LUA_OPLT : c_int = 1 as c_int ;
35
37
36
- extern "C" {
37
- pub fn sin ( x : c_double ) -> c_double ;
38
- pub fn cos ( x : c_double ) -> c_double ;
39
- pub fn tan ( x : c_double ) -> c_double ;
40
- pub fn acos ( x : c_double ) -> c_double ;
41
- pub fn asin ( x : c_double ) -> c_double ;
42
- pub fn atan2 ( x : c_double , y : c_double ) -> c_double ;
43
- pub fn ceil ( x : c_double ) -> c_double ;
44
- pub fn floor ( x : c_double ) -> c_double ;
45
- pub fn exp ( x : c_double ) -> c_double ;
46
- pub fn fabs ( x : c_double ) -> c_double ;
47
- pub fn sqrt ( x : c_double ) -> c_double ;
48
- pub fn fmod ( x : c_double , y : c_double ) -> c_double ;
49
-
50
- pub fn log ( x : c_double ) -> c_double ;
51
- pub fn log2 ( x : c_double ) -> c_double ;
52
- pub fn log10 ( x : c_double ) -> c_double ;
53
- pub fn rand ( ) -> c_double ;
54
-
55
- pub fn luaL_error ( L : * mut lua_State , fmt : * const libc:: c_char , args: ...) -> c_int ;
56
- }
57
-
58
38
/* ** (The range comparisons are tricky because of rounding. The tests
59
39
** here assume a two-complement representation, where MININTEGER always
60
40
** has an exact representation as a float; MAXINTEGER may not have one,
@@ -85,8 +65,8 @@ unsafe extern "C" fn math_floor(L: *mut lua_State) -> c_int {
85
65
if lua_isinteger ( L , 1 as libc:: c_int ) != 0 {
86
66
lua_settop ( L , 1 as libc:: c_int ) ;
87
67
} else {
88
- let d = floor ( luaL_checknumber ( L , 1 as libc:: c_int ) ) ;
89
- pushnumint ( L , d ) ;
68
+ let n = luaL_checknumber ( L , 1 as libc:: c_int ) ;
69
+ pushnumint ( L , n . floor ( ) ) ;
90
70
}
91
71
return 1 ;
92
72
}
@@ -95,8 +75,8 @@ unsafe extern "C" fn math_ceil(L: *mut lua_State) -> c_int {
95
75
if lua_isinteger ( L , 1 as libc:: c_int ) != 0 {
96
76
lua_settop ( L , 1 as libc:: c_int ) ;
97
77
} else {
98
- let d = ceil ( luaL_checknumber ( L , 1 as libc:: c_int ) ) ;
99
- pushnumint ( L , d ) ;
78
+ let n = luaL_checknumber ( L , 1 as libc:: c_int ) ;
79
+ pushnumint ( L , n . ceil ( ) ) ;
100
80
}
101
81
return 1 ;
102
82
}
@@ -127,7 +107,7 @@ unsafe extern "C" fn math_modf(L: *mut lua_State) -> c_int {
127
107
lua_pushnumber ( L , 0.0 ) ;
128
108
} else {
129
109
let n = luaL_checknumber ( L , 1 ) ;
130
- let ip: f64 = if n < 0.0 { ceil ( n ) } else { floor ( n ) } ;
110
+ let ip: f64 = if n < 0.0 { n . ceil ( ) } else { n . floor ( ) } ;
131
111
pushnumint ( L , ip) ;
132
112
lua_pushnumber ( L , if n == ip { 0.0 } else { n - ip } ) ;
133
113
}
@@ -146,7 +126,7 @@ unsafe extern "C" fn math_fmod(L: *mut lua_State) -> libc::c_int {
146
126
lua_pushinteger ( L , lua_tointeger ( L , 1 ) % d) ;
147
127
}
148
128
} else {
149
- lua_pushnumber ( L , fmod ( luaL_checknumber ( L , 1 ) , luaL_checknumber ( L , 2 ) ) ) ;
129
+ lua_pushnumber ( L , luaL_checknumber ( L , 1 ) % luaL_checknumber ( L , 2 ) ) ;
150
130
}
151
131
return 1 ;
152
132
}
@@ -204,40 +184,46 @@ unsafe extern "C" fn math_abs(L: *mut lua_State) -> c_int {
204
184
}
205
185
lua_pushinteger ( L , n) ;
206
186
} else {
207
- lua_pushnumber ( L , fabs ( luaL_checknumber ( L , 1 as c_int ) ) ) ;
187
+ let n = luaL_checknumber ( L , 1 as c_int ) ;
188
+ lua_pushnumber ( L , n. abs ( ) ) ;
208
189
}
209
190
return 1 as c_int ;
210
191
}
211
192
212
193
unsafe extern "C" fn math_sin ( L : * mut lua_State ) -> c_int {
213
- lua_pushnumber ( L , sin ( luaL_checknumber ( L , 1 as c_int ) ) ) ;
194
+ let n = luaL_checknumber ( L , 1 as c_int ) ;
195
+ lua_pushnumber ( L , n. sin ( ) ) ;
214
196
return 1 as c_int ;
215
197
}
216
198
217
199
unsafe extern "C" fn math_cos ( L : * mut lua_State ) -> c_int {
218
- lua_pushnumber ( L , cos ( luaL_checknumber ( L , 1 as c_int ) ) ) ;
200
+ let n = luaL_checknumber ( L , 1 as c_int ) ;
201
+ lua_pushnumber ( L , n. cos ( ) ) ;
219
202
return 1 as c_int ;
220
203
}
221
204
222
205
unsafe extern "C" fn math_tan ( L : * mut lua_State ) -> c_int {
223
- lua_pushnumber ( L , tan ( luaL_checknumber ( L , 1 as c_int ) ) ) ;
206
+ let n = luaL_checknumber ( L , 1 as c_int ) ;
207
+ lua_pushnumber ( L , n. tan ( ) ) ;
224
208
return 1 as c_int ;
225
209
}
226
210
227
211
unsafe extern "C" fn math_asin ( L : * mut lua_State ) -> c_int {
228
- lua_pushnumber ( L , asin ( luaL_checknumber ( L , 1 as c_int ) ) ) ;
212
+ let n = luaL_checknumber ( L , 1 as c_int ) ;
213
+ lua_pushnumber ( L , n. asin ( ) ) ;
229
214
return 1 as c_int ;
230
215
}
231
216
232
217
unsafe extern "C" fn math_acos ( L : * mut lua_State ) -> c_int {
233
- lua_pushnumber ( L , acos ( luaL_checknumber ( L , 1 as c_int ) ) ) ;
218
+ let n = luaL_checknumber ( L , 1 as c_int ) ;
219
+ lua_pushnumber ( L , n. acos ( ) ) ;
234
220
return 1 as c_int ;
235
221
}
236
222
237
223
unsafe extern "C" fn math_atan ( L : * mut lua_State ) -> c_int {
238
224
let y = luaL_checknumber ( L , 1 as c_int ) ;
239
225
let x = luaL_optnumber ( L , 2 as c_int , 1 as c_int as lua_Number ) ;
240
- lua_pushnumber ( L , atan2 ( y , x) ) ;
226
+ lua_pushnumber ( L , y . atan2 ( x) ) ;
241
227
return 1 as c_int ;
242
228
}
243
229
@@ -254,7 +240,8 @@ unsafe extern "C" fn math_toint(L: *mut lua_State) -> libc::c_int {
254
240
}
255
241
256
242
unsafe extern "C" fn math_sqrt ( L : * mut lua_State ) -> c_int {
257
- lua_pushnumber ( L , sqrt ( luaL_checknumber ( L , 1 as c_int ) ) ) ;
243
+ let n = luaL_checknumber ( L , 1 as c_int ) ;
244
+ lua_pushnumber ( L , n. sqrt ( ) ) ;
258
245
return 1 as c_int ;
259
246
}
260
247
@@ -270,23 +257,24 @@ unsafe extern "C" fn math_log(L: *mut lua_State) -> c_int {
270
257
271
258
let res: lua_Number ;
272
259
if lua_isnoneornil ( L , 2 ) {
273
- res = log ( x ) ;
260
+ res = x . log ( E . into ( ) ) ;
274
261
} else {
275
262
let base = luaL_checknumber ( L , 2 as c_int ) ;
276
263
if base == 2.0 {
277
- res = log2 ( x ) ;
264
+ res = x . log2 ( ) ;
278
265
} else if base == 10.0 {
279
- res = log10 ( x ) ;
266
+ res = x . log10 ( ) ;
280
267
} else {
281
- res = log ( x ) / log ( base ) ;
268
+ res = x . log ( E . into ( ) ) / base . log ( E . into ( ) ) ;
282
269
}
283
270
}
284
271
lua_pushnumber ( L , res as f64 ) ;
285
272
return 1 as c_int ;
286
273
}
287
274
288
275
unsafe extern "C" fn math_exp ( L : * mut lua_State ) -> c_int {
289
- lua_pushnumber ( L , exp ( luaL_checknumber ( L , 1 as c_int ) ) ) ;
276
+ let n = luaL_checknumber ( L , 1 as c_int ) ;
277
+ lua_pushnumber ( L , n. exp ( ) ) ;
290
278
return 1 as c_int ;
291
279
}
292
280
0 commit comments