Chapter 4Mathematical Functions

This chapter describes the basic mathematical constants and functions provided by the PLT Scheme Science Collection.

The constants and functions described in this chapter are defined in the math.ss file in the science collection and are made available using the form:

(require (planet "math.ss" ("williams" "science.plt")))


4.1  Mathematical Constants

Table 5.1 shows the mathematical constants defined by the PLT Scheme Science Collection.

 e The base of exponentials, log2e The base two logarithm of e, log10e The base ten logarithm of e, sqrt2 The square root of two, sqrt1/2 The square root of one half, sqrt3 The square root of three, pi The constant pi pi/2 Pi divided by two, pi/4 Pi divided by four, sqrtpi The square root of pi, 2/sqrtpi Two divided by the square root of pi, 1/pi The reciprocal of pi, 2/pi Twice the reciprocal of pi, ln10 The natural log of ten, ln2 The natural log of two, lnpi The natural log of pi, euler Euler's constant,
Table 1:  Mathematical Constants

4.2  Infinities and Not-a-Number

PLT Scheme provides +inf.0 (positive infinity), -inf.0 (negative infinity), +nan.0 (not a number), and -nan.0 (same as +nan.0) as inexact numerical constants. The following functions are provided as a convenience for checking for infinities and not-a-number.

nan?

 Function: (nan? x) Contract: (-> any? boolean?)
This function returns true, #t, if x is not-a-number (i.e., equivalent to +nan.0 or, therefore, -nan.0), and false, #f, otherwise. Note that this is not the same as (not (number? x)), which is true if x is not a number.

infinite?

 Function: (infinite? x) Contract: (-> any? (union (integer-in -1 1) boolean?))
This function returns 1 if x is positive infinity (i.e., equivalent to +inf.0), - 1 if x is negative infinity (i.e., equivalent to -inf.0), and false, #f, otherwise. Note that (finite? x) is not equivalent to (not (infinite? x)), since both finite? and infinite? return false for anything that is not a real number.

finite?

 Function: (finite? x) Contract: (-> any? boolean?)
This function returns true, #t, if x is a finite real number, and false, #f otherwise. Note that (finite? x) is not equivalent to (not (infinite? x)), since both finite? and infinite? return false for anything that is not a real number.

4.3  Elementary Functions

The following functions provide some elementary mathematical functions that are not provided by PLT Scheme.

log1p

 Function: (log1p x) Contract: (-> real? number?)
This function computes the value of log (1 + x) in a way that is accurate for small x.

expm1

 Function: (expm1 x) Contract: (-> real? real?)
This function computes the value of exp x - 1 in a way that is accurate for small x.

hypot

 Function: (hypot x y) Contract: (-> real? real? real?)
This function computes the value of (x2 + y2)1/2 in a way that avoids overflow.

acosh

 Function: (acosh x) Contract: (-> real? real?)
This function computes the value of the hyperbolic arccosine, arccosh, of x.

asinh

 Function: (asinh x) Contract: (-> real? real?)
This function computes the value of the hyperbolic arcsine, arcsinh, of x.

atanh

 Function: (atanh x) Contract: (-> real? real?)
This function computes the value of the hyperbolic arctangent, arctanh, of x.

ldexp

 Function: (ldexp x e) Contract: (-> real? integer? real?)
This function computes the value of x × 2e.

frexp

 Function: (frexp x) Contract: (-> real? (values real? integer?))
This function splits the real number x into a normalized fraction f and exponent e, such that x = f × 2e and 0.5 < f < 1. The function returns f and e as multiple values. If x is zero, both f and e are returned as zero.

4.4  Testing the Sign of Numbers

sign

 Function: (sign x) Contract: (-> real? (integer-in -1 1))
This function returns the sign of x: 1 if x > 0 and - 1 if x < 0. Note that the sign of zero is positive, regardless of its floating-point sign bit.

4.5  Approximate Comparisons of Real Numbers

It is sometimes useful to be able to compare two real (in particular, floating-point) numbers approximately, to allow for rounding and truncation errors. The following function implements the approximate floating-point comparison algorithm proposed by D.E. Knuth in Section 4.2.2 of Seminumerical Algorithms (3rd edition).

fcmp

 Function: (fcmp x y epsilon) Contract: (-> real? real? real? (integer-in -1 1))
This function determines whether x and y are approximately equal to a relatively accuracy, epsilon. The relative accuracy is measured using an interval of 2 × delta, where delta = 2k × epsilon and k is the maximum base 2 exponent of x and y as computed by the function frexp. If x and y lie within this interval, they are considered approximately equal and the function returns 0. Otherwise, if x < y, the function returns - 1, or if x > y, the function returns 1.

The implementation of this function is based on the package fcmp by T.C. Belding.