-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathex28.c
38 lines (32 loc) · 1.2 KB
/
ex28.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Example 28. Mathematical gamma function
//8<----------------------------------------------------------------------------
#include <math.h>
#include <stdio.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
//8<----------------------------------------------------------------------------
static int gamma(lua_State *L) {
double z = luaL_checknumber(L, 1); // fetch argument
lua_pushnumber(L, tgamma(z)); // push value to return
return 1; // number of stack values to return
}
//8<----------------------------------------------------------------------------
int main() {
lua_State *L = luaL_newstate();
luaL_openlibs(L);
//8<----------------------------------------------------------------------------
// Add gamma to Lua's math module.
lua_getglobal(L, "math");
lua_pushcfunction(L, gamma);
lua_setfield(L, -2, "gamma");
lua_pop(L, 1); // global "math"
//8<----------------------------------------------------------------------------
if (lua_gettop(L) > 0) printf("stack size != 0\n");
luaL_dostring(L, "return math.gamma(3)");
printf("%f\n", lua_tonumber(L, -1));
lua_pop(L, 1);
lua_close(L);
return 0;
}
//8<----------------------------------------------------------------------------