LIBM(3lua) LUA Library Manual LIBM(3lua)

libm
Lua interface to the math(3) library

local lm = require 'libm'

The libm Lua binding provides access to the math(3) library. If these functions are given an argument outside the domain of the function the return value will be NaN. This can be checked with lm.isnan().

Many of these functions have native equivalents in the Lua math library. Others, like round(3), cbrt(3), and the hyperbolic trigonometry functions, are not provided without an auxiliary module. The functions that are redundant are provided for completeness.

Since these functions map quite closely to the functions in the math library, detailed information about their behavior can be found in the man pages for each function in section 3 of the manual.

Name Man page Description
lm.acos acos(3) inverse trigonometric function
lm.acosh acosh(3) inverse hyperbolic function
lm.asin asin(3) inverse trigonometric function
lm.asinh asinh(3) inverse hyperbolic function
lm.atan atan(3) inverse trigonometric function
lm.atanh atanh(3) inverse hyperbolic function
lm.atan2 atan2(3) inverse trigonometric function
lm.cbrt sqrt(3) cube root
lm.ceil ceil(3) integer no less than
lm.copysign copysign(3) copy sign bit
lm.cos cos(3) trigonometric function
lm.cosh cosh(3) hyperbolic function
lm.erf erf(3) error function
lm.erfc erf(3) complementary error function
lm.exp exp(3) base e exponential
lm.exp2 exp2(3) base 2 exponential
lm.expm1 expm1(3) exp(x)-1
lm.fabs fabs(3) absolute value
lm.fdim erf(3) positive difference
lm.finite finite(3) test for finity
lm.floor floor(3) integer no greater than
lm.fma fmod(3) fused multiply-add
lm.fmax fmax(3) maximum
lm.fmin fmin(3) minimum
lm.fmod fmod(3) remainder
lm.hypot hypot(3) Euclidean distance
lm.ilogb ilogb(3) exponent extraction
lm.isinf isinf(3) test for infinity
lm.isnan isnan(3) test for not-a-number
lm.j0 j0(3) Bessel function
lm.j1 j0(3) Bessel function
lm.jn j0(3) Bessel function
lm.lgamma lgamma(3) log gamma function
lm.log log(3) natural logarithm
lm.log10 log(3) logarithm to base 10
lm.log1p log(3) log(1+x)
lm.nan nan(3) return quiet NaN
lm.nextafter nextafter(3) next representable number
lm.pow pow(3) exponential x**y
lm.remainder remainder(3) remainder
lm.rint rint(3) round to nearest integer
lm.scalbn scalbn(3) exponent adjustment
lm.sin sin(3) trigonometric function
lm.sinh sinh(3) hyperbolic function
lm.sqrt sqrt(3) square root
lm.tan tan(3) trigonometric function
lm.tanh tanh(3) hyperbolic function
lm.trunc trunc(3) nearest integral value
lm.y0 j0(3) Bessel function
lm.y1 j0(3) Bessel function
lm.yn j0(3) Bessel function

Name Value Description
lm.M_E 2.7182818284590452354 e
lm.M_LOG2E 1.4426950408889634074 log 2e
lm.M_LOG10E 0.43429448190325182765 log 10e
lm.M_LN2 0.69314718055994530942 log e2
lm.M_LN10 2.30258509299404568402 log e10
lm.M_PI 3.14159265358979323846 pi
lm.M_PI_2 1.57079632679489661923 pi/2
lm.M_PI_4 0.78539816339744830962 pi/4
lm.M_1_PI 0.31830988618379067154 1/pi
lm.M_2_PI 0.63661977236758134308 2/pi
lm.M_2_SQRTPI 1.12837916709551257390 2/sqrt(pi)
lm.M_SQRT2 1.41421356237309504880 sqrt(2)
lm.M_SQRT1_2 0.70710678118654752440 1/sqrt(2)

The functions finite, isfinite, isinf, and isnan return integers in the math library, the results are converted to lua boolean values by this interface.

The single precision float, and long double data types, implemented in several of the libm routines, are not supported by this interface.

The math library has a long development history, but if a bug exists there, it will certainly exist in this interface as well.

A libm Lua binding appeared in NetBSD 9.4.

The libm Lua binding was written by Phillip Rulon.
November 2, 2022 NetBSD 10.0