# Math Library

This is a basic math library, providing many of the functions which are available in D or C99. If you pass an integer where a floating point number would be needed, it will be converted to a float automatically.

## math.e

The constant e (2.7182818...)

## math.pi

The constant pi (3.1415926...)

## math.nan

A value which means 'not a number.' This is returned by some of the math functions to indicate that the result is not a real number.

## math.infinity

Positive infinity. This (and its negative) is returned by some of the math functions and math operations to indicate that the result is too big to fit into a floating point number.

## math.abs(n)

Returns the absolute value of the number. Will return a number of the same type as the input.

## math.sin(n)

Returns the sine of the angle, which is in radians. Always returns a float.

## math.cos(n)

Returns the cosine of the angle, which is in radians. Always returns a float.

## math.tan(n)

Returns the tangent of the angle, which is in radians. Always returns a float.

## math.asin(n)

Returns the inverse sine of the number. Always returns a float. Returns math.nan if the input is outside the range [-1.0, 1.0].

## math.acos(n)

Returns the inverse cosine of the number. Always returns a float. Returns math.nan if the input is outside the range [-1.0, 1.0].

## math.atan(n)

Returns the inverse tangent of the number. Always returns a float in the range [-pi / 2, pi / 2]. This works for all inputs in the range (-infinity, infinity).

## math.atan2(y, x)

Normally, when you use the inverse tangent, you pass it y/x, given that y and x are coordinates on a Cartesian plane. However, this causes information about which quadrant the result should be in to be lost. Thus, math.atan will map two different angles to the same return value. math.atan2 allows you to pass in the two values separately, and so it is able to determine which quadrant the result should be in. The return value will then be in the range [-pi, pi]. This works for all inputs in the range (-infinity, infinity). The result when both inputs are 0 is 0.

## math.sqrt(n)

Returns the square root of the input. Returns -math.nan if a number less than 0 is passed in. The result is always a float.

## math.cbrt(n)

Returns the cube root of the input. Always returns a float. This works for all inputs in the range(-infinity, infinity).

## math.pow(b, p)

Raises the base b to the p power. Fractional and negative powers are legal as well.

Calculates en.

## math.ln(n)

Calculates the natural log of the input. This is the inverse of en (math.exp).

## math.log2(n)

Calculates the base-2 logarithm of the input. This is the inverse of 2n.

## math.log10(n)

Calculates the base-10 logarithm of the input. This is the inverse of 10n.

## math.hypot(x, y)

Calculates the length of the hypotenuse of a right triangle given sides of length x and y. This is the same as calculating sqrt(x2 + y2).

## math.gamma(n)

Calculates the gamma function of the input. The gamma function is like the factorial (!) function but extended to all real numbers. Only inputs greater than zero are really useful, though. This function is slightly different from factorial in that if you pass it an integral parameter, you will get the factorial of one less than the input. So math.gamma(5) will yield 24, or 4!.

## math.lgamma(n)

Returns the natural log of the gamma function of the input. See above for a description of the gamma function.

## math.ceil(n)

Rounds the number up to the next integer closer to positive infinity. So math.ceil(1.2) yields 2; math.ceil(-1.2) yields -1; and math.ceil of any integer will return the same number. The result is always a floating point number.

## math.floor(n)

Rounds the number down to the next integer closer to negative infinity. So math.floor(1.2) yields 1; math.floor(-1.2) yields -2; and math.floor of any integer will return the same number. The result is always a floating point number.

## math.trunc(n)

Returns the integral part of a number, simply discarding any digits after the decimal point. Always returns an int.

## math.round(n)

Rounds the number to the nearest integer. Always returns an int.

## math.isNan(n)

Returns true if the input is math.nan, and false otherwise.

## math.isInf(n)

Returns true is the input is positive or negative infinity, and false otherwise.

## math.sign(n)

Returns an integer representing the sign of the number. If the number is less than 0, the result is -1; if the number is greater than 0, the result is 1; and if the result is 0, the result is 0.

## math.rand([[lo,] hi])

Returns a random integer. If no parameters are given, the value will be a random integer in the range [-231, 231). If one parameter is given, it will act as the upper noninclusive bound on the values returned, so math.rand(10) will return integers in the range [0, 10). Passing a single negative number, such as math.rand(-10) won't work properly; instead use -math.rand(10) to get numbers in the range (-10, 0]. If two parameters are given, the first is the lower inclusive bound, and the second is the upper noninclusive bound. So math.rand(-10, -5) will return numbers in the range [-10, -5).

## math.frand([[lo,] hi])

Returns a random float. If no parameters are given, the value will be a random float in the range [0.0, 1.0]. If one parameter is given, it will act as the upper inclusive bound on the values returned, so math.frand(10) will return floats in the range [0.0, 10.0]. Passing a single negative number, such as math.frand(-10) will work properly and will give numbers in the range [-10.0, 0.0]. If two parameters are given, the first is the lower bound and the second the upper, both inclusive. So math.frand(-10, -5) will return numbers in the range [-10.0, -5.0].