# MathLib (Luaj API)
Date: 2015-04-29

## org.luaj.vm2.lib
Class MathLib

    java.lang.Object
      org.luaj.vm2.Varargs
          org.luaj.vm2.LuaValue
              org.luaj.vm2.LuaFunction
                  org.luaj.vm2.lib.LibFunction
                      org.luaj.vm2.lib.TwoArgFunction
                          org.luaj.vm2.lib.MathLib

Direct Known Subclasses:
  JseMathLib

    public class MathLib

extends TwoArgFunction

Subclass of `LibFunction` which implements the lua standard `math` library.

It contains only the math library support that is possible on JME. For a more complete implementation based on math functions specific to JSE use `JseMathLib`. In Particular the following math functions are not implemented by this library:

- acos
- asin
- atan
- cosh
- log
- sinh
- tanh
- atan2

The implementations of `exp()` and `pow()` are constructed by hand for JME, so will be slower and less accurate than when executed on the JSE platform.

Typically, this library is included as part of a call to either `JsePlatform.standardGlobals()` or `JmePlatform.standardGlobals()`

     Globals globals = JsePlatform.standardGlobals();
     System.out.println( globals.get("math").get("sqrt").call( LuaValue.valueOf(2) ) );

When using `JsePlatform` as in this example, the subclass `JseMathLib` will be included, which also includes this base functionality.

To instantiate and use it directly, link it into your globals table via `LuaValue.load(LuaValue)` using code such as:

     Globals globals = new Globals();
     globals.load(new JseBaseLib());
     globals.load(new PackageLib());
     globals.load(new MathLib());
     System.out.println( globals.get("math").get("sqrt").call( LuaValue.valueOf(2) ) );

Doing so will ensure the library is properly initialized and loaded into the globals table.

This has been implemented to match as closely as possible the behavior in the corresponding library in C.

See Also:
  `LibFunction`, `JsePlatform`, `JmePlatform`, `JseMathLib`, Lua 5.2 Math Lib Reference

Nested Class Summary
`protected static class` `MathLib.BinaryOp`
`protected static class` `MathLib.UnaryOp`

Field Summary
`static MathLib` `MATHLIB` Pointer to the latest MathLib instance, used only to dispatch math.exp to tha correct platform math library.

Fields inherited from class org.luaj.vm2.lib.LibFunction
`name, opcode`

Fields inherited from class org.luaj.vm2.LuaFunction
`s_metatable`

Fields inherited from class org.luaj.vm2.LuaValue
`ADD, CALL, CONCAT, DIV, EMPTYSTRING, ENV, EQ, FALSE, INDEX, LE, LEN, LT, METATABLE, MINUSONE, MOD, MODE, MUL, NEWINDEX, NIL, NILS, NONE, NOVALS, ONE, POW, SUB, TBOOLEAN, TFUNCTION, TINT, TLIGHTUSERDATA, TNIL, TNONE, TNUMBER, TOSTRING, TRUE, TSTRING, TTABLE, TTHREAD, TUSERDATA, TVALUE, TYPE_NAMES, UNM, ZERO`

Constructor Summary
`MathLib()` Construct a MathLib, which can be initialized by calling it with a modname string, and a global environment table as arguments using `call(LuaValue, LuaValue)`.

Method Summary
`LuaValue` `call(LuaValue modname, LuaValue env)` Perform one-time initialization on the library by creating a table containing the library functions, adding that table to the supplied environment, adding the table to package.loaded, and returning table as the return value.
`static double` `dpow_d(double a, double b)`
`protected static double` `dpow_default(double a, double b)` Default JME version computes using longhand heuristics.
`double` `dpow_lib(double a, double b)` Hook to override default dpow behavior with faster implementation.
`static LuaValue` `dpow(double a, double b)` compute power using installed math library, or default if there is no math library installed

Methods inherited from class org.luaj.vm2.lib.TwoArgFunction
`call, call, call, invoke`

Methods inherited from class org.luaj.vm2.lib.LibFunction
`bind, bind, call, newupe, newupl, newupn, tojstring`

Methods inherited from class org.luaj.vm2.LuaFunction
`checkfunction, classnamestub, getmetatable, isfunction, name, optfunction, strvalue, type, typename`

Methods inherited from class org.luaj.vm2.LuaValue

`add, add, add, and, arg, arg1, argerror, argerror, aritherror, aritherror, arithmt, arithmtwith, assert_, buffer, call, callmt, checkboolean, checkclosure, checkdouble, checkglobals, checkint, checkinteger, checkjstring, checklong, checkmetatag, checknotnil, checknumber, checknumber, checkstring, checktable, checkthread, checkuserdata, checkuserdata, compareerror, compareerror, comparemt, concat, concat, concatmt, concatTo, concatTo, concatTo, div, div, div, divInto, eq_b, eq, eqmtcall, equals, error, get, get, get, gettable, gt_b, gt_b, gt_b, gt, gt, gt, gteq_b, gteq_b, gteq_b, gteq, gteq, gteq, illegal, inext, initupvalue1, invoke, invoke, invoke, invoke, invoke, invokemethod, invokemethod, invokemethod, invokemethod, invokemethod, invokemethod, isboolean, isclosure, isint, isinttype, islong, isnil, isnumber, isstring, istable, isthread, isuserdata, isuserdata, isvalidkey, len, lenerror, length, listOf, listOf, load, lt_b, lt_b, lt_b, lt, lt, lt, lteq_b, lteq_b, lteq_b, lteq, lteq, lteq, metatableOf, metatag, method, method, method, method, method, method, mod, mod, mod, modFrom, mul, mul, mul, narg, neg, neq_b, neq, next, not, onInvoke, optboolean, optclosure, optdouble, optint, optinteger, optjstring, optlong, optnumber, optstring, opttable, optthread, optuserdata, optuserdata, optvalue, or, pow, pow, pow, powWith, powWith, presize, raweq, raweq, raweq, raweq, raweq, rawget, rawget, rawget, rawlen, rawset, rawset, rawset, rawset, rawset, rawset, rawset, rawsetlist, set, set, set, set, set, set, set, setmetatable, settable, strcmp, strcmp, strongvalue, sub, sub, sub, subargs, subFrom, subFrom, tableOf, tableOf, tableOf, tableOf, tableOf, tableOf, tailcallOf, testfor_b, toboolean, tobyte, tochar, todouble, tofloat, toint, tolong, tonumber, toshort, tostring, toString, touserdata, touserdata, typerror, unimplemented, userdataOf, userdataOf, valueOf, valueOf, valueOf, valueOf, valueOf, valueOf, varargsOf, varargsOf, varargsOf, varargsOf, varargsOf, varargsOf`

Methods inherited from class org.luaj.vm2.Varargs

`argcheck, checkboolean, checkclosure, checkdouble, checkfunction, checkint, checkinteger, checkjstring, checklong, checknotnil, checknumber, checkstring, checktable, checkthread, checkuserdata, checkuserdata, checkvalue, eval, isfunction, isnil, isnoneornil, isnumber, isstring, istable, isTailcall, isthread, isuserdata, isvalue, optboolean, optclosure, optdouble, optfunction, optint, optinteger, optjstring, optlong, optnumber, optstring, opttable, optthread, optuserdata, optuserdata, optvalue, toboolean, tobyte, tochar, todouble, tofloat, toint, tojstring, tolong, toshort, touserdata, touserdata, type`

Methods inherited from class java.lang.Object
`clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

Field Detail

### MATHLIB

    public static MathLib MATHLIB

  Pointer to the latest MathLib instance, used only to dispatch math.exp to tha correct platform math library.

Constructor Detail

### MathLib

    public MathLib()

  Construct a MathLib, which can be initialized by calling it with a modname string, and a global environment table as arguments using `call(LuaValue, LuaValue)`.

Method Detail

### call

    public LuaValue call(LuaValue modname,
                         LuaValue env)

  Perform one-time initialization on the library by creating a table containing the library functions, adding that table to the supplied environment, adding the table to package.loaded, and returning table as the return value.

  Specified by:
    `call` in class `TwoArgFunction`
  Parameters:
    `modname` - the module name supplied if this is loaded via 'require'.
    `env` - the environment to load into, typically a Globals instance.
  Returns:
    First return value `(this(arg1,arg2))`, or `LuaValue.NIL` if there were none.
  See Also:
    `LuaValue.call()`, `LuaValue.call(LuaValue)`, `LuaValue.call(LuaValue, LuaValue, LuaValue)`, `LuaValue.invoke(LuaValue, Varargs)`, `LuaValue.method(String,LuaValue,LuaValue)`, `LuaValue.method(LuaValue,LuaValue,LuaValue)`

### dpow

    public static LuaValue dpow(double a,
                                double b)

  compute power using installed math library, or default if there is no math library installed

### dpow_d

    public static double dpow_d(double a,
                                double b)

### dpow_lib

    public double dpow_lib(double a,
                           double b)

  Hook to override default dpow behavior with faster implementation.

### dpow_default

    protected static double dpow_default(double a,
                                         double b)

  Default JME version computes using longhand heuristics.

Copyright © 2007-2015 Luaj.org. All Rights Reserved.