Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

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.

Members

  1. math.e
  2. math.pi
  3. math.nan
  4. math.infinity
  5. math.intSize
  6. math.intMin
  7. math.intMax
  8. math.floatSize
  9. math.floatMin
  10. math.floatMax
  11. math.abs(n)
  12. math.sin(n)
  13. math.cos(n)
  14. math.tan(n)
  15. math.asin(n)
  16. math.acos(n)
  17. math.atan(n)
  18. math.atan2(y, x)
  19. math.sqrt(n)
  20. math.cbrt(n)
  21. math.pow(b, p)
  22. math.exp(n)
  23. math.ln(n)
  24. math.log2(n)
  25. math.log10(n)
  26. math.hypot(x, y)
  27. math.gamma(n)
  28. math.lgamma(n)
  29. math.ceil(n)
  30. math.floor(n)
  31. math.round(n)
  32. math.trunc(n)
  33. math.isNan(n)
  34. math.isInf(n)
  35. math.sign(n)
  36. math.rand([[lo,] hi])
  37. math.frand([[lo,] hi])
  38. math.max(vararg)
  39. math.min(vararg)

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

The size, in bytes, of the int type. (By default, 8.)

math.intMin

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

math.intMax

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

math.floatSize

The size, in bytes, of the float type. (By default, 8).

math.floatMin

The smallest positive number the float type can hold.

math.floatMax

The largest positive number the float type can hold.

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.

math.exp(n)

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.round(n)

Rounds the given floating point number to the nearest integer. Always returns an int.

math.trunc(n)

Returns the integral part of a number, simply discarding any digits after the decimal point. 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 [-263, 263). 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].

math.max(vararg)

Gets the largest value of all the arguments. The arguments don't necessarily have to be ints or floats. Passing 0 arguments is an error.

math.min(vararg)

Gets the smallest value of all the arguments. The arguments don't necessarily have to be ints or floats. Passing 0 arguments is an error.