1+ use std:: f32:: consts:: E ;
2+
13use libc:: { c_double, c_int, c_longlong, c_ulong, c_ulonglong, intptr_t, srand} ;
24use rand:: prelude:: * ;
35
@@ -7,8 +9,8 @@ use crate::lapi::{
79 lua_tointeger, lua_tointegerx, lua_type,
810} ;
911use 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,
1214} ;
1315use crate :: lstate:: lua_State;
1416use crate :: types:: { lua_CFunction, lua_Integer, lua_Number, lua_Unsigned, LUA_TNUMBER } ;
@@ -33,28 +35,6 @@ pub const LUA_MININTEGER: c_longlong = LLONG_MIN;
3335pub const LUA_MAXINTEGER : c_longlong = LLONG_MAX ;
3436pub const LUA_OPLT : c_int = 1 as c_int ;
3537
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-
5838/* ** (The range comparisons are tricky because of rounding. The tests
5939** here assume a two-complement representation, where MININTEGER always
6040** 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 {
8565 if lua_isinteger ( L , 1 as libc:: c_int ) != 0 {
8666 lua_settop ( L , 1 as libc:: c_int ) ;
8767 } 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 ( ) ) ;
9070 }
9171 return 1 ;
9272}
@@ -95,8 +75,8 @@ unsafe extern "C" fn math_ceil(L: *mut lua_State) -> c_int {
9575 if lua_isinteger ( L , 1 as libc:: c_int ) != 0 {
9676 lua_settop ( L , 1 as libc:: c_int ) ;
9777 } 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 ( ) ) ;
10080 }
10181 return 1 ;
10282}
@@ -127,7 +107,7 @@ unsafe extern "C" fn math_modf(L: *mut lua_State) -> c_int {
127107 lua_pushnumber ( L , 0.0 ) ;
128108 } else {
129109 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 ( ) } ;
131111 pushnumint ( L , ip) ;
132112 lua_pushnumber ( L , if n == ip { 0.0 } else { n - ip } ) ;
133113 }
@@ -146,7 +126,7 @@ unsafe extern "C" fn math_fmod(L: *mut lua_State) -> libc::c_int {
146126 lua_pushinteger ( L , lua_tointeger ( L , 1 ) % d) ;
147127 }
148128 } 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 ) ) ;
150130 }
151131 return 1 ;
152132}
@@ -204,40 +184,46 @@ unsafe extern "C" fn math_abs(L: *mut lua_State) -> c_int {
204184 }
205185 lua_pushinteger ( L , n) ;
206186 } 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 ( ) ) ;
208189 }
209190 return 1 as c_int ;
210191}
211192
212193unsafe 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 ( ) ) ;
214196 return 1 as c_int ;
215197}
216198
217199unsafe 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 ( ) ) ;
219202 return 1 as c_int ;
220203}
221204
222205unsafe 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 ( ) ) ;
224208 return 1 as c_int ;
225209}
226210
227211unsafe 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 ( ) ) ;
229214 return 1 as c_int ;
230215}
231216
232217unsafe 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 ( ) ) ;
234220 return 1 as c_int ;
235221}
236222
237223unsafe extern "C" fn math_atan ( L : * mut lua_State ) -> c_int {
238224 let y = luaL_checknumber ( L , 1 as c_int ) ;
239225 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) ) ;
241227 return 1 as c_int ;
242228}
243229
@@ -254,7 +240,8 @@ unsafe extern "C" fn math_toint(L: *mut lua_State) -> libc::c_int {
254240}
255241
256242unsafe 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 ( ) ) ;
258245 return 1 as c_int ;
259246}
260247
@@ -270,23 +257,24 @@ unsafe extern "C" fn math_log(L: *mut lua_State) -> c_int {
270257
271258 let res: lua_Number ;
272259 if lua_isnoneornil ( L , 2 ) {
273- res = log ( x ) ;
260+ res = x . log ( E . into ( ) ) ;
274261 } else {
275262 let base = luaL_checknumber ( L , 2 as c_int ) ;
276263 if base == 2.0 {
277- res = log2 ( x ) ;
264+ res = x . log2 ( ) ;
278265 } else if base == 10.0 {
279- res = log10 ( x ) ;
266+ res = x . log10 ( ) ;
280267 } else {
281- res = log ( x ) / log ( base ) ;
268+ res = x . log ( E . into ( ) ) / base . log ( E . into ( ) ) ;
282269 }
283270 }
284271 lua_pushnumber ( L , res as f64 ) ;
285272 return 1 as c_int ;
286273}
287274
288275unsafe 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 ( ) ) ;
290278 return 1 as c_int ;
291279}
292280
0 commit comments