Class MathLib

All Implemented Interfaces:
KryoSerializable
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 org.luaj.vm2.lib.jse.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 org.luaj.vm2.lib.jse.JsePlatform#standardGlobals() or

 
 Globals globals = JsePlatform.standardGlobals();
 System.out.println( globals.get("math").get("sqrt").call( LuaValue.valueOf(2) ) );
  
When using org.luaj.vm2.lib.jse.JsePlatform as in this example, the subclass org.luaj.vm2.lib.jse.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:
  • Field Details

    • MATHLIB

      public static MathLib MATHLIB
      Pointer to the latest MathLib instance, used only to dispatch math.exp to tha correct platform math library.
  • Constructor Details

    • 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 Details