Go to the previous, next section.

Scientific Functions

The functions described here perform trigonometric and other transcendental calculations. They generally produce floating-point answers correct to the full current precision. The H (Hyperbolic) and I (Inverse) flag keys must be used to get some of these functions from the keyboard.

One miscellanous command is shift-P (calc-pi), which pushes the value of @c{$\pi$} pi (at the current precision) onto the stack. With the Hyperbolic flag, it pushes the value e, the base of natural logarithms. With the Inverse flag, it pushes Euler's constant @c{$\gamma$} gamma (about 0.5772). With both Inverse and Hyperbolic, it pushes the "golden ratio" @c{$\phi$} phi (about 1.618). (At present, Euler's constant is not available to unlimited precision; Calc knows only the first 100 digits.) In Symbolic mode, these commands push the actual variables `pi', `e', `gamma', and `phi', respectively, instead of their values; see section Symbolic Mode.

The Q (calc-sqrt) [sqrt] function is described elsewhere; see section Basic Arithmetic. With the Inverse flag [sqr], this command computes the square of the argument.

See section Numeric Prefix Arguments, for a discussion of the effect of numeric prefix arguments on commands in this chapter which do not otherwise interpret a prefix argument.

Logarithmic Functions

The shift-L (calc-ln) [ln] command computes the natural logarithm of the real or complex number on the top of the stack. With the Inverse flag it computes the exponential function instead, although this is redundant with the E command.

The shift-E (calc-exp) [exp] command computes the exponential, i.e., e raised to the power of the number on the stack. The meanings of the Inverse and Hyperbolic flags follow from those for the calc-ln command.

The H L (calc-log10) [log10] command computes the common (base-10) logarithm of a number. (With the Inverse flag [exp10], it raises ten to a given power.) Note that the common logarithm of a complex number is computed by taking the natural logarithm and dividing by @c{$\ln10$} ln(10).

The B (calc-log) [log] command computes a logarithm to any base. For example, 1024 RET 2 B produces 10, since 2^10 = 1024. In certain cases like `log(3,9)', the result will be either 1:2 or 0.5 depending on the current Fraction Mode setting. With the Inverse flag [alog], this command is similar to ^ except that the order of the arguments is reversed.

The f I (calc-ilog) [ilog] command computes the integer logarithm of a number to any base. The number and the base must themselves be positive integers. This is the true logarithm, rounded down to an integer. Thus ilog(x,10) is 3 for all x in the range from 1000 to 9999. If both arguments are positive integers, exact integer arithmetic is used; otherwise, this is equivalent to `floor(log(x,b))'.

The f E (calc-expm1) [expm1] command computes exp(x)-1, but using an algorithm that produces a more accurate answer when the result is close to zero, i.e., when @c{$e^x$} exp(x) is close to one.

The f L (calc-lnp1) [lnp1] command computes ln(x+1), producing a more accurate answer when x is close to zero.

Trigonometric/Hyperbolic Functions

The shift-S (calc-sin) [sin] command computes the sine of an angle or complex number. If the input is an HMS form, it is interpreted as degrees-minutes-seconds; otherwise, the input is interpreted according to the current angular mode. It is best to use Radians mode when operating on complex numbers.

Calc's "units" mechanism includes angular units like deg, rad, and grad. While `sin(45 deg)' is not evaluated all the time, the u s (calc-simplify-units) command will simplify `sin(45 deg)' by taking the sine of 45 degrees, regardless of the current angular mode. See section Basic Operations on Units.

Also, the symbolic variable pi is not ordinarily recognized in arguments to trigonometric functions, as in `sin(3 pi / 4)', but the a s (calc-simplify) command recognizes many such formulas when the current angular mode is radians and symbolic mode is enabled; this example would be replaced by `sqrt(2) / 2'. See section Symbolic Mode. Beware, this simplification occurs even if you have stored a different value in the variable `pi'; this is one reason why changing built-in variables is a bad idea. Arguments of the form x plus a multiple of @c{$\pi/2$} pi/2 are also simplified. Calc includes similar formulas for cos and tan.

The a s command knows all angles which are integer multiples of pi/12, @c{$\pi/10$} pi/10, or @c{$\pi/8$} pi/8 radians. In degrees mode, analogous simplifications occur for integer multiples of 15 or 18 degrees, and for arguments plus multiples of 90 degrees.

With the Inverse flag, calc-sin computes an arcsine. This is also available as the calc-arcsin command or arcsin algebraic function. The returned argument is converted to degrees, radians, or HMS notation depending on the current angular mode.

With the Hyperbolic flag, calc-sin computes the hyperbolic sine, also available as calc-sinh [sinh]. With the Hyperbolic and Inverse flags, it computes the hyperbolic arcsine (calc-arcsinh) [arcsinh].

The shift-C (calc-cos) [cos] command computes the cosine of an angle or complex number, and shift-T (calc-tan) [tan] computes the tangent, along with all the various inverse and hyperbolic variants of these functions.

The f T (calc-arctan2) [arctan2] command takes two numbers from the stack and computes the arc tangent of their ratio. The result is in the full range from -180 (exclusive) to +180 (inclusive) degrees, or the analogous range in radians. A similar result would be obtained with / followed by I T, but the value would only be in the range from -90 to +90 degrees since the division loses information about the signs of the two components, and an error might result from an explicit division by zero which arctan2 would avoid. By (arbitrary) definition, `arctan2(0,0)=0'.

The calc-sincos [sincos] command computes the sine and cosine of a number, returning them as a vector of the form `[cos, sin]'. With the Inverse flag [arcsincos], this command takes a two-element vector as an argument and computes arctan2 of the elements. (This command does not accept the Hyperbolic flag.)

Advanced Mathematical Functions

Calc can compute a variety of less common functions that arise in various branches of mathematics. All of the functions described in this section allow arbitrary complex arguments and, except as noted, will work to arbitrarily large precisions. They can not at present handle error forms or intervals as arguments.

NOTE: These functions are still experimental. In particular, their accuracy is not guaranteed in all domains. It is advisable to set the current precision comfortably higher than you actually need when using these functions. Also, these functions may be impractically slow for some values of the arguments.

The f g (calc-gamma) [gamma] command computes the Euler gamma function. For positive integer arguments, this is related to the factorial function: `gamma(n+1) = fact(n)'. For general complex arguments the gamma function can be defined by the following definite integral: @c{$\Gamma(a) = \int_0^\infty t^{a-1} e^t dt$} gamma(a) = integ(t^(a-1) exp(t), t, 0, inf). (The actual implementation uses far more efficient computational methods.)

The f G (calc-inc-gamma) [gammaP] command computes the incomplete gamma function, denoted `P(a,x)'. This is defined by the integral, @c{$P(a,x) = \left( \int_0^x t^{a-1} e^t dt \right) / \Gamma(a)$} gammaP(a,x) = integ(t^(a-1) exp(t), t, 0, x) / gamma(a). This implies that `gammaP(a,inf) = 1' for any a (see the definition of the normal gamma function).

Several other varieties of incomplete gamma function are defined. The complement of P(a,x), called Q(a,x) = 1-P(a,x) by some authors, is computed by the I f G [gammaQ] command. You can think of this as taking the other half of the integral, from x to infinity.

The f b (calc-beta) [beta] command computes the Euler beta function, which is defined in terms of the gamma function as beta(a,b) = gamma(a) gamma(b) / gamma(a+b), or by beta(a,b) = integ(t^(a-1) (1-t)^(b-1), t, 0, 1).

The f B (calc-inc-beta) [betaI] command computes the incomplete beta function I(x,a,b). It is defined by betaI(x,a,b) = integ(t^(a-1) (1-t)^(b-1), t, 0, x) / beta(a,b). Once again, the H (hyperbolic) prefix gives the corresponding un-normalized version [betaB].

The f e (calc-erf) [erf] command computes the error function @c{$\hbox{erf}(x) = {2 \over \sqrt{\pi}} \int_0^x e^{-t^2} dt$} erf(x) = 2 integ(exp(-(t^2)), t, 0, x) / sqrt(pi). The complementary error function I f e (calc-erfc) [erfc] is the corresponding integral from `x' to infinity; the sum erf(x) + erfc(x) = 1.

The f j (calc-bessel-J) [besJ] and f y (calc-bessel-Y) [besY] commands compute the Bessel functions of the first and second kinds, respectively. In `besJ(n,x)' and `besY(n,x)' the "order" parameter n is often an integer, but is not required to be one. Calc's implementation of the Bessel functions currently limits the precision to 8 digits, and may not be exact even to that precision. Use with care!

Branch Cuts and Principal Values

All of the logarithmic, trigonometric, and other scientific functions are defined for complex numbers as well as for reals. This section describes the values returned in cases where the general result is a family of possible values. Calc follows section 12.5.3 of Steele's Common Lisp, the Language, second edition, in these matters. This section will describe each function briefly; for a more detailed discussion (including some nifty diagrams), consult Steele's book.

Note that the branch cuts for arctan and arctanh were changed between the first and second editions of Steele. Versions of Calc starting with 2.00 follow the second edition.

The new branch cuts exactly match those of the HP-28/48 calculators. They also match those of Mathematica 1.2, except that Mathematica's arctan cut is always in the right half of the complex plane, and its arctanh cut is always in the top half of the plane. Calc's cuts are continuous with quadrants I and III for arctan, or II and IV for arctanh.

Note: The current implementations of these functions with complex arguments are designed with proper behavior around the branch cuts in mind, not efficiency or accuracy. You may need to increase the floating precision and wait a while to get suitable answers from them.

For `sqrt(a+bi)': When a<0 and b is small but positive or zero, the result is close to the +i axis. For b small and negative, the result is close to the -i axis. The result always lies in the right half of the complex plane.

For `ln(a+bi)': The real part is defined as `ln(abs(a+bi))'. The imaginary part is defined as `arg(a+bi) = arctan2(b,a)'. Thus the branch cuts for sqrt and ln both lie on the negative real axis.

The following table describes these branch cuts in another way. If the real and imaginary parts of z are as shown, then the real and imaginary parts of f(z) will be as shown. Here eps stands for a small positive value; each occurrence of eps may stand for a different small value.

     z           sqrt(z)       ln(z)
----------------------------------------
   +,   0         +,  0       any, 0
   -,   0         0,  +       any, pi
   -, +eps      +eps, +      +eps, +
   -, -eps      +eps, -      +eps, -

For `z1^z2': This is defined by `exp(ln(z1)*z2)'. One interesting consequence of this is that `(-8)^1:3' does not evaluate to -2 as you might expect, but to the complex number (1., 1.732). Both of these are valid cube roots of -8 (as is (1., -1.732)); Calc chooses a perhaps less-obvious root for the sake of mathematical consistency.

For `arcsin(z)': This is defined by `-i*ln(i*z + sqrt(1-z^2))'. The branch cuts are on the real axis, less than -1 and greater than 1.

For `arccos(z)': This is defined by `-i*ln(z + i*sqrt(1-z^2))', or equivalently by `pi/2 - arcsin(z)'. The branch cuts are on the real axis, less than -1 and greater than 1.

For `arctan(z)': This is defined by `(ln(1+i*z) - ln(1-i*z)) / (2*i)'. The branch cuts are on the imaginary axis, below -i and above i.

For `arcsinh(z)': This is defined by `ln(z + sqrt(1+z^2))'. The branch cuts are on the imaginary axis, below -i and above i.

For `arccosh(z)': This is defined by `ln(z + (z+1)*sqrt((z-1)/(z+1)))'. The branch cut is on the real axis less than 1.

For `arctanh(z)': This is defined by `(ln(1+z) - ln(1-z)) / 2'. The branch cuts are on the real axis, less than -1 and greater than 1.

The following tables for arcsin, arccos, and arctan assume the current angular mode is radians. The hyperbolic functions operate independently of the angular mode.

       z             arcsin(z)            arccos(z)
-------------------------------------------------------
 (-1..1),  0      (-pi/2..pi/2), 0       (0..pi), 0
 (-1..1), +eps    (-pi/2..pi/2), +eps    (0..pi), -eps
 (-1..1), -eps    (-pi/2..pi/2), -eps    (0..pi), +eps
   <-1,    0          -pi/2,     +         pi,    -
   <-1,  +eps      -pi/2 + eps,  +      pi - eps, -
   <-1,  -eps      -pi/2 + eps,  -      pi - eps, +
    >1,    0           pi/2,     -          0,    +
    >1,  +eps       pi/2 - eps,  +        +eps,   -
    >1,  -eps       pi/2 - eps,  -        +eps,   +

       z            arccosh(z)         arctanh(z)
-----------------------------------------------------
 (-1..1),  0        0,  (0..pi)       any,     0
 (-1..1), +eps    +eps, (0..pi)       any,    +eps
 (-1..1), -eps    +eps, (-pi..0)      any,    -eps
   <-1,    0        +,    pi           -,     pi/2
   <-1,  +eps       +,  pi - eps       -,  pi/2 - eps
   <-1,  -eps       +, -pi + eps       -, -pi/2 + eps
    >1,    0        +,     0           +,    -pi/2
    >1,  +eps       +,   +eps          +,  pi/2 - eps
    >1,  -eps       +,   -eps          +, -pi/2 + eps

       z           arcsinh(z)           arctan(z)
-----------------------------------------------------
   0, (-1..1)    0, (-pi/2..pi/2)         0,     any
   0,   <-1      -,    -pi/2            -pi/2,    -
 +eps,  <-1      +, -pi/2 + eps       pi/2 - eps, -
 -eps,  <-1      -, -pi/2 + eps      -pi/2 + eps, -
   0,    >1      +,     pi/2             pi/2,    +
 +eps,   >1      +,  pi/2 - eps       pi/2 - eps, +
 -eps,   >1      -,  pi/2 - eps      -pi/2 + eps, +

Finally, the following identities help to illustrate the relationship between the complex trigonometric and hyperbolic functions. They are valid everywhere, including on the branch cuts.

sin(i*z)  = i*sinh(z)       arcsin(i*z)  = i*arcsinh(z)
cos(i*z)  =   cosh(z)       arcsinh(i*z) = i*arcsin(z)
tan(i*z)  = i*tanh(z)       arctan(i*z)  = i*arctanh(z)
sinh(i*z) = i*sin(z)        cosh(i*z)    =   cos(z)

The "advanced math" functions (gamma, Bessel, etc.) are also defined for general complex arguments, but their branch cuts and principal values are not rigorously specified at present.

Random Numbers

The "advanced math" functions (gamma, Bessel, etc.) are also defined for general complex arguments, but their branch cuts and principal values are not rigorously specified at present.

Random Numbers