The Croc Programming Language

module math

Math functions. What more is there to say?

global e

The constant e (2.7182818...).

global pi

The constant pi (3.1415926...).

global nan

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

global 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.

global intSize

The size, in bytes, of the Croc int type. By default, this is 8. Nonstandard implementations may differ.

global intMin

The smallest (most negative) number the Croc int type can hold.

global intMax

The largest (most positive) number the Croc int type can hold.

global floatSize

The size, in bytes, of the Croc float type. By default, this is 8. Nonstandard implementations may differ.

global floatMin

The smallest positive non-zero number the Croc float type can hold.

global floatMax

The largest positive non-infinity number the Croc float type can hold.

global function abs(v: int|float)

Returns:

the absolute value of the number. The returned value will be the same type as was passed in.

global function sin(v: int|float)

Returns:

the sine of the angle, which is assumed to be in radians. Always returns a float.

global function cos(v: int|float)

Returns:

the cosine of the angle, which is assumed to be in radians. Always returns a float.

global function tan(v: int|float)

Returns:

the tangent of the angle, which is assumed to be in radians. Always returns a float.

global function asin(v: int|float)

Returns:

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

global function acos(v: int|float)

Returns:

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

global function atan(v: int|float)

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).

global function atan2(dy: int|float, dx: int|float)

Returns:

the inverse tangent, extended to all four quadrants by passing the x and y distances separately.

Normally, when you use the inverse tangent, you pass it dy/dx, given that dy and dx are coordinates on a Cartesian plane. The problem is that this causes information about which quadrant the result should be in to be lost. Because of this, atan will map two different angles to the same return value. atan2 allows you to pass in the two values separately, so it can 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.

global function sqrt(v: int|float)

Returns:

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

global function cbrt(v: int|float)

Returns:

the cube root of the input. The result is always a float. This works for all inputs in the range (-infinity, infinity).

global function pow(base: int|float, exp: int|float)

Returns:

base raised to the exp power. Fractional and negative powers are legal as well. Always returns a float.

global function exp(v: int|float)

Returns:

ev. Always returns a float.

global function ln(v: int|float)

Returns:

the natural logarithm of v. This is the inverse of exp. Always returns a float.

global function log2(v: int|float)

Returns:

the base-2 logarithm of v. This is the inverse if 2v. Always returns a float.

global function log10(v: int|float)

Returns:

the base-10 logarithm of v. This is the inverse if 10v. Always returns a float.

global function hypot(dx: int|float, dy: int|float)

Returns:

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

global function gamma(v: int|float)

Returns:

the gamma function of the input. Always returns a float.

The gamma function is like factorial (!) function but extended to all real numbers. This function is slightly different from factorial in that if you pass it an integer v, you will get (v - 1)!. So math.gamma(5) gives 4! = 24.

global function lgamma(v: int|float)

Returns:

the natural log of the gamma function of the input.

global function ceil(v: int|float)

Returns:

v rounded up to the next integer closer to infinity, or if v is already a whole number, returns it unmodified. Always returns a float. See iceil for a version that returns an int.

global function floor(v: int|float)

Returns:

v rounded down to the next integer closer to negative infinity, or if v is already a whole number, returns it unmodified. Always returns a float. See ifloor for a version that returns an int.

global function round(v: int|float)

Returns:

v rounded to the nearest integer, or if v is already a whole number, returns it unmodified. Always returns a float. See iround for a version that returns an int.

global function trunc(v: int|float)

Returns:

the whole number part of the value, discarding the digits after the decimal point. Always returns a float. See itrunc for a version that returns an int.

global function iceil(v: int|float)
global function ifloor(v: int|float)
global function iround(v: int|float)
global function itrunc(v: int|float)

These functions operate the same as ceil, floor, round, and trunc, except the value is cast to an integer before being returned.

global function isNan(v: int|float)

Returns:

true if the input is nan, and false otherwise.

global function isInf(v: int|float)

Returns:

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

global function sign(v: int|float)

Returns:

an integer representing the sign of the number. If v < 0, returns -1; if v > 0, returns 1; and if v == 0, returns 0.

global function rand(a: int = null, b: int = null)

Returns:

a random integer.

If no parameters are given, the value will be a random integer in the range [-263, 263).

If one parameter is given, if it's positive, it will act as the upper noninclusive bound on the values returned, so math.rand(10) will return integers in the range [0, 10). If it's negative, it will act as the lower noninclusive bound on the values returned, so math.rand(-10) will return integers 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).

Throws:
RangeError

if only one parameter is passed, and it is 0.

ValueError

if two parameters are passed, and they are equal.

global function frand(a: int|float = null, b: int|float = null)

Returns:

a random float.

If no parameters are given, the value will be a random float in the range [0.0, 1.0]. Note that the upper bound is inclusive!

If one parameter is given, if it's positive, 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]. If it's negative, it will act as the lower inclusive bound, so math.frand(-10) will return 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].

global function seedRand(seed: int)

Seeds the VM-wide random number generator with the given seed.

The random number generator is seeded with the current time when the VM is created, so normally you won't have to call this unless you want to replicate a certain sequence of random numbers.

Params:
seed

will be used as the seed. Only the lower 32 bits will be used; the upper 32 will be discarded.

global function getRandSeed()

Returns:

the last value that the VM-wide random number generator was seeded with.

global function max(vararg)

Returns:

the largest value of its parameters. Note that this is a generic function; the parameters don't have to be numbers, they can be any comparable type.

Throws:
ParamError

if no parameters are passed.

global function min(vararg)

Returns:

the smallest value of its parameters. Note that this is a generic function; the parameters don't have to be numbers, they can be any comparable type.

Throws:
ParamError

if no parameters are passed.

HTML and JavaScript source derived from by Victor Nakoryakov; Page generated on 15 Nov 2014 10:28:14