Skip to content
Browse files

Imported from 5.1/lmathx.tar.gz.

  • Loading branch information...
0 parents commit 6f1b886600775ada93864f0ebd1ed79c5bd6a1eb Luiz Henrique de Figueiredo committed with davidm Apr 5, 2010
Showing with 453 additions and 0 deletions.
  1. +66 −0 Makefile
  2. +35 −0 README
  3. +270 −0 lmathx.c
  4. +82 −0 test.lua
66 Makefile
@@ -0,0 +1,66 @@
+# makefile for mathx library for Lua
+
+# change these to reflect your Lua installation
+LUA= /tmp/lhf/lua-5.1.4
+LUAINC= $(LUA)/src
+LUALIB= $(LUA)/src
+LUABIN= $(LUA)/src
+
+# these will probably work if Lua has been installed globally
+#LUA= /usr/local
+#LUAINC= $(LUA)/include
+#LUALIB= $(LUA)/lib
+#LUABIN= $(LUA)/bin
+
+# probably no need to change anything below here
+CC= gcc
+CFLAGS= $(INCS) $(WARN) -O2 $G
+WARN= -ansi -pedantic -Wall
+INCS= -I$(LUAINC)
+MAKESO= $(CC) -shared
+#MAKESO= env MACOSX_DEPLOYMENT_TARGET=10.3 $(CC) -bundle -undefined dynamic_lookup
+
+MYNAME= mathx
+MYLIB= l$(MYNAME)
+T= $(MYNAME).so
+OBJS= $(MYLIB).o
+TEST= test.lua
+
+all: test
+
+test: $T
+ $(LUABIN)/lua $(TEST)
+
+o: $(MYLIB).o
+
+so: $T
+
+$T: $(OBJS)
+ $(MAKESO) -o $@ $(OBJS)
+
+clean:
+ rm -f $(OBJS) $T core core.*
+
+doc: $T
+ @echo "$(MYNAME) library:"
+ @$(LUABIN)/lua -e 'for k in pairs(math) do math[k]=nil print(k) end' -l$(MYNAME) -e 'for k in pairs(math) do print(k.."*") end' | sort | column
+
+# distribution
+
+FTP= www:www/ftp/lua/5.1
+F= http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/5.1/$A
+D= $(MYNAME)
+A= $(MYLIB).tar.gz
+TOTAR= Makefile,README,$(MYLIB).c,test.lua
+
+distr: clean
+ tar zcvf $A -C .. $D/{$(TOTAR)}
+ touch -r $A .stamp
+ scp -p $A $(FTP)
+
+diff: clean
+ wget -q -N $F
+ tar zxf $A
+ diff $D .
+
+# eof
35 README
@@ -0,0 +1,35 @@
+This code is extends the Lua math library with the functions available in C99.
+
+To try the library, just edit Makefile to reflect your installation of Lua and
+then run make. This will build the library and run a simple test. For detailed
+installation instructions, see
+ http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/install.html
+
+There is no manual: see the summary below and a C99 reference manual, e.g.
+http://www.dinkumware.com/manuals/default.aspx?manual=compleat&page=math.html
+
+Read also test.lua, which shows the library in action.
+
+This code is hereby placed in the public domain.
+
+Please send comments, suggestions, and bug reports to lhf@tecgraf.puc-rio.br .
+
+-------------------------------------------------------------------------------
+
+mathx library:
+abs deg frexp log1p* random
+acos erf* gamma* log2* randomseed
+acosh* erfc* huge logb* remainder*
+asin exp hypot* max rint*
+asinh* exp2* infinity* min round*
+atan expm1* isfinite* mod scalbn*
+atan2 fdim* isinf* modf signbit*
+atanh* floor isnan* nan* sin
+cbrt* fma* isnormal* nearbyint* sinh
+ceil fmax* ldexp nextafter* sqrt
+copysign* fmin* lgamma* pi tan
+cos fmod log pow tanh
+cosh fpclassify* log10 rad trunc*
+
+*added in mathx
+-------------------------------------------------------------------------------
270 lmathx.c
@@ -0,0 +1,270 @@
+/*
+* lmathx.c
+* C99 math functions for Lua
+* Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>
+* 04 Apr 2010 22:53:32
+* This code is hereby placed in the public domain.
+*/
+
+#define _GNU_SOURCE 1
+#include <math.h>
+#include <stdio.h>
+
+#include "lua.h"
+#include "lualib.h"
+#include "lauxlib.h"
+
+#define A(i) luaL_checknumber(L,i)
+
+static int Lacosh(lua_State *L)
+{
+ lua_pushnumber(L,acosh(A(1)));
+ return 1;
+}
+
+static int Lasinh(lua_State *L)
+{
+ lua_pushnumber(L,asinh(A(1)));
+ return 1;
+}
+
+static int Latanh(lua_State *L)
+{
+ lua_pushnumber(L,atanh(A(1)));
+ return 1;
+}
+
+static int Lcbrt(lua_State *L)
+{
+ lua_pushnumber(L,cbrt(A(1)));
+ return 1;
+}
+
+static int Lcopysign(lua_State *L)
+{
+ lua_pushnumber(L,copysign(A(1),A(2)));
+ return 1;
+}
+
+static int Lerf(lua_State *L)
+{
+ lua_pushnumber(L,erf(A(1)));
+ return 1;
+}
+
+static int Lerfc(lua_State *L)
+{
+ lua_pushnumber(L,erfc(A(1)));
+ return 1;
+}
+
+static int Lexp2(lua_State *L)
+{
+ lua_pushnumber(L,exp2(A(1)));
+ return 1;
+}
+
+static int Lexpm1(lua_State *L)
+{
+ lua_pushnumber(L,expm1(A(1)));
+ return 1;
+}
+
+static int Lfdim(lua_State *L)
+{
+ lua_pushnumber(L,fdim(A(1),A(2)));
+ return 1;
+}
+
+static int Lfma(lua_State *L)
+{
+ lua_pushnumber(L,fma(A(1),A(2),A(3)));
+ return 1;
+}
+
+static int Lfmax(lua_State *L)
+{
+ int i,n=lua_gettop(L);
+ lua_Number m=A(1);
+ for (i=2; i<=n; i++) m=fmax(m,A(i));
+ lua_pushnumber(L,m);
+ return 1;
+}
+
+static int Lfmin(lua_State *L)
+{
+ int i,n=lua_gettop(L);
+ lua_Number m=A(1);
+ for (i=2; i<=n; i++) m=fmin(m,A(i));
+ lua_pushnumber(L,m);
+ return 1;
+}
+
+static int Lfpclassify(lua_State *L)
+{
+ switch (fpclassify(A(1)))
+ {
+ case FP_INFINITE: lua_pushliteral(L,"inf"); break;
+ case FP_NAN: lua_pushliteral(L,"nan"); break;
+ case FP_NORMAL: lua_pushliteral(L,"normal"); break;
+ case FP_SUBNORMAL: lua_pushliteral(L,"subnormal"); break;
+ case FP_ZERO: lua_pushliteral(L,"zero"); break;
+ }
+ return 1;
+}
+
+static int Lhypot(lua_State *L)
+{
+ lua_pushnumber(L,hypot(A(1),A(2)));
+ return 1;
+}
+
+static int Lisfinite(lua_State *L)
+{
+ lua_pushboolean(L,isfinite(A(1)));
+ return 1;
+}
+
+static int Lisinf(lua_State *L)
+{
+ lua_pushboolean(L,isinf(A(1)));
+ return 1;
+}
+
+static int Lisnan(lua_State *L)
+{
+ lua_pushboolean(L,isnan(A(1)));
+ return 1;
+}
+
+static int Lisnormal(lua_State *L)
+{
+ lua_pushboolean(L,isnormal(A(1)));
+ return 1;
+}
+
+static int Llgamma(lua_State *L)
+{
+ lua_pushnumber(L,lgamma(A(1)));
+ return 1;
+}
+
+static int Llog1p(lua_State *L)
+{
+ lua_pushnumber(L,log1p(A(1)));
+ return 1;
+}
+
+static int Llog2(lua_State *L)
+{
+ lua_pushnumber(L,log2(A(1)));
+ return 1;
+}
+
+static int Llogb(lua_State *L)
+{
+ lua_pushnumber(L,logb(A(1)));
+ return 1;
+}
+
+static int Lnearbyint(lua_State *L)
+{
+ lua_pushnumber(L,nearbyint(A(1)));
+ return 1;
+}
+
+static int Lnextafter(lua_State *L)
+{
+ lua_pushnumber(L,nextafter(A(1),A(2)));
+ return 1;
+}
+
+static int Lremainder(lua_State *L)
+{
+ lua_pushnumber(L,remainder(A(1),A(2)));
+ return 1;
+}
+
+static int Lrint(lua_State *L)
+{
+ lua_pushnumber(L,rint(A(1)));
+ return 1;
+}
+
+static int Lround(lua_State *L)
+{
+ lua_pushnumber(L,round(A(1)));
+ return 1;
+}
+
+static int Lscalbn(lua_State *L)
+{
+ lua_pushnumber(L,scalbn(A(1),(int)A(2)));
+ return 1;
+}
+
+static int Lsignbit(lua_State *L)
+{
+ lua_pushboolean(L,signbit(A(1)));
+ return 1;
+}
+
+static int Ltgamma(lua_State *L)
+{
+ lua_pushnumber(L,tgamma(A(1)));
+ return 1;
+}
+
+static int Ltrunc(lua_State *L)
+{
+ lua_pushnumber(L,trunc(A(1)));
+ return 1;
+}
+
+static const luaL_Reg R[] =
+{
+ { "acosh", Lacosh },
+ { "asinh", Lasinh },
+ { "atanh", Latanh },
+ { "cbrt", Lcbrt },
+ { "copysign", Lcopysign },
+ { "erfc", Lerfc },
+ { "erf", Lerf },
+ { "exp2", Lexp2 },
+ { "expm1", Lexpm1 },
+ { "fdim", Lfdim },
+ { "fma", Lfma },
+ { "fmax", Lfmax },
+ { "fmin", Lfmin },
+ { "fpclassify", Lfpclassify },
+ { "gamma", Ltgamma },
+ { "hypot", Lhypot },
+ { "isfinite", Lisfinite },
+ { "isinf", Lisinf },
+ { "isnan", Lisnan },
+ { "isnormal", Lisnormal },
+ { "lgamma", Llgamma },
+ { "log1p", Llog1p },
+ { "log2", Llog2 },
+ { "logb", Llogb },
+ { "nearbyint", Lnearbyint },
+ { "nextafter", Lnextafter },
+ { "remainder", Lremainder },
+ { "rint", Lrint },
+ { "round", Lround },
+ { "scalbn", Lscalbn },
+ { "signbit", Lsignbit },
+ { "trunc", Ltrunc },
+ { NULL, NULL }
+};
+
+
+LUALIB_API int luaopen_mathx(lua_State *L)
+{
+ luaL_register(L,LUA_MATHLIBNAME,R);
+ lua_pushnumber(L,INFINITY);
+ lua_setfield(L,-2,"infinity");
+ lua_pushnumber(L,NAN);
+ lua_setfield(L,-2,"nan");
+ return 1;
+}
82 test.lua
@@ -0,0 +1,82 @@
+-- test mathx library
+
+------------------------------------------------------------------------------
+require"mathx"
+
+version="mathx library for ".. _VERSION.." / Aug 2009"
+print(version)
+
+print""
+function f(x)
+ print(x,math.isfinite(x),math.isnan(x),math.isnormal(x),math.fpclassify(x))
+end
+
+print("x","finite","nan","normal","fpclassify")
+f(0)
+f(1/-math.infinity)
+f(1/0)
+f(0/0)
+f(math.infinity)
+f(math.nan)
+f(3.45)
+
+print""
+x=1
+while x~=0 do
+ y,x=x,x/2
+ if not math.isnormal(x) then break end
+end
+f(y)
+f(x)
+f(math.nextafter(x,1))
+f(math.nextafter(0,1))
+
+while x~=0 do
+ y,x=x,x/2
+end
+f(y)
+f(x)
+
+print""
+function f(x)
+ local a=math.nextafter(x,-1/0)
+ local b=math.nextafter(x, 1/0)
+ print(a,x,b,x-a,b-x)
+end
+f(1)
+f(0)
+
+print""
+print("max",math.fmax(1,math.nan,2), math.max(math.nan,1,2))
+print("min",math.fmin(1,math.nan,2), math.min(math.nan,1,2))
+
+print""
+print("log2(32)",math.log2(32))
+print("exp2(5)","",math.exp2(5))
+print("cbrt(2)","",math.cbrt(2))
+print("6!","",math.gamma(6+1))
+print("hypot(5,12)",math.hypot(5,12))
+print("fma(3,2,1)",math.fma(3,2,1))
+
+print""
+function f(x)
+ print(x,math.floor(x),math.trunc(x),math.round(x),math.ceil(x))
+end
+print("x","floor","trunc","round","ceil")
+f(-1.2)
+f(-1.7)
+f(1.2)
+f(1.7)
+
+print""
+function f(x,y)
+ print(x,y,x%y,math.remainder(x,y))
+end
+print("x","y","%","remainder")
+f(13,3)
+f(13,-3)
+f(-13,3)
+f(-13,-3)
+
+print""
+print(version)

0 comments on commit 6f1b886

Please sign in to comment.
Something went wrong with that request. Please try again.