Previous: Rest arguments, Up: Functions


5.10.4 Mathematical functions

Asymptote has built-in versions of the standard libm mathematical real(real) functions sin, cos, tan, asin, acos, atan, exp, log, pow10, log10, sinh, cosh, tanh, asinh, acosh, atanh, sqrt, cbrt, fabs, as well as the identity function identity. Asymptote also defines the order n Bessel functions of the first kind J(int n, real) and second kind Y(int n, real), as well as the gamma function gamma, the error function erf, and the complementary error function erfc. The standard real(real, real) functions atan2, hypot, fmod, remainder are also included.

The functions degrees(real radians) and radians(real degrees) can be used to convert between radians and degrees. The function Degrees(real radians) returns the angle in degrees in the interval [0,360). For convenience, Asymptote defines variants Sin, Cos, Tan, aSin, aCos, and aTan of the standard trigonometric functions using degrees rather than radians.

The functions floor, ceil, and round differ from their usual definitions in that they all return an int value rather than a real (since that is normally what one wants). The functions Floor, Ceil, and Round are respectively similar, except that if the result cannot be converted to a valid int, they return intMax for positive arguments and -intMax for negative arguments, rather than generating an integer overflow. We also define a function sgn, which returns the sign of its real argument as an integer (-1, 0, or 1).

There is an abs(int) function, as well as an abs(real) function (equivalent to fabs(real)) and an abs(pair) function (equivalent to length(pair)).

Random numbers can be seeded with srand(int) and generated with the int rand() function, which returns a random integer between 0 and the integer randMax. A Gaussian random number generator Gaussrand and a collection of statistics routines, including histogram, are provided in the base file stats.asy.

When configured with the GNU Scientific Library (GSL), available from http://www.gnu.org/software/gsl/, Asymptote contains an internal module gsl that defines the airy functions Ai(real), Bi(real), Ai_deriv(real), Bi_deriv(real), zero_Ai(int), zero_Bi(int), zero_Ai_deriv(int), zero_Bi_deriv(int), the Bessel functions I(int, real), K(int, real), j(int, real), y(int, real), i_scaled(int, real), k_scaled(int, real), J(real, real), Y(real, real), I(real, real), K(real, real), zero_J(real, int), the elliptic functions F(real, real), E(real, real), and P(real, real), the exponential/trigonometric integrals Ei, Si, and Ci, the Legendre polynomials Pl(int, real), and the Riemann zeta function zeta(real). For example, to compute the sine integral Si of 1.0:

import gsl;
write(Si(1.0));