On this page:
4.1 Mathematical Constants
e
log2e
log10e
sqrt2
sqrt1/  2
sqrt3
2*pi
4*pi
pi/  2
pi/  4
sqrtpi
2/  sqrtpi
1/  pi
2/  pi
ln10
1/  ln10
ln2
1/  ln2
lnpi
euler
4.2 Testing for Infinities and Not-a-Number
infinite
finite?
4.3 Elementary Functions
log1p
expm1
hypot
acosh
asinh
atahh
ldexp
frexp
4.4 Testing the Sign of Numbers
sign
4.5 Approximate Comparisons of Real Numbers
fcmp
4.6 Log10 and Decibels (d  B)
log10
d  B

4 Mathematical Functions

    4.1 Mathematical Constants

    4.2 Testing for Infinities and Not-a-Number

    4.3 Elementary Functions

    4.4 Testing the Sign of Numbers

    4.5 Approximate Comparisons of Real Numbers

    4.6 Log10 and Decibels (dB)

This chapter describes the basic mathematical constants and functions provided by the 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 williams/science/math))

4.1 Mathematical Constants

The following are the mathematical constants defined by the Science Collection:

syntax

e

The base of the exponentials, e

syntax

log2e

The base two logarithm of e, log2 e

syntax

log10e

The base ten logarithm of e, log10 e

syntax

sqrt2

The square root of two, 2

syntax

sqrt1/2

The square root of one half, √½

syntax

sqrt3

The square root of three, 3

+The constant pi was added to racket/math, which is included in the racket language. Therefore, it has been removed from the Science Collection.

syntax

2*pi

Pi time two, 2π

syntax

4*pi

Pi time four, 4π

syntax

pi/2

Pi divided by two, π/2

syntax

pi/4

Pi divided by four, π/4

syntax

sqrtpi

The square root of pi, π)

syntax

2/sqrtpi

Two divided by the square root of pi, 2/√π

syntax

1/pi

The reciprocal of pi, 1/π

syntax

2/pi

Twice the reciprocal of pi, 2/π

syntax

ln10

The natural log of ten, ln 10 or loge 10

syntax

1/ln10

The inverse of the natural log of ten, 1/ln 10

syntax

ln2

The natural log of two, ln 2 or loge 2

syntax

1/ln2

The inverse of the natural log of two, 1/ln 2

syntax

lnpi

The natural log of pi, ln π or loge π

syntax

euler

Euler’s constant, γ

4.2 Testing for Infinities and Not-a-Number

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

Racket now provides the predicate functions nan? and infinite?. These have been removed from the Science Collection.

procedure

(infinite x)  (or/c -1 #f 1)

  x : any/c
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, #f for anything that is not a real number.

procedure

(finite? x)  boolean?

  x : any/c
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, #f for anything that is not a real number.

4.3 Elementary Functions

The following functions provide some elementary mathematical functions that are not provide by Racket.

procedure

(log1p x)  number?

  x : real?
Computes the value of log(1 + x) in a way that is accurate for small x.

procedure

(expm1 x)  real?

  x : real?
Computes the value of exp(x - 1) in a way that is accurate for small x.

procedure

(hypot x y)  real?

  x : real?
  y : real?
Computes the value of (x2 + y2)½ in a way that avoids overflow.

procedure

(acosh x)  real?

  x : real?
Computes the value of the hyperbolic arccosine, arccosh, of x.

procedure

(asinh x)  real?

  x : real?
Computes the value of the hyperbolic arcsine, arcsinh, of x.

procedure

(atahh x)  real?

  x : real?
Computes the value of the hyperbolic arctangent, arctanh, of x.

procedure

(ldexp x e)  real?

  x : real?
  e : integer?
Computes the value of x × 2e.

procedure

(frexp x)  
real? integer?
  x : real?
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

procedure

(sign x)  (integer-in -1 1)

  x : real?
Returns the sign of x as 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 functions implements the approximate floating-point comparison algorithm proposed by D.E. Knuth in Section 4.2.2 of Seminumerical Algorithms (3rd edition) [Knuth].

procedure

(fcmp x y epsilon)  (integer-in -1 1)

  x : real?
  y : real?
  epsilon : real?
Determines whether x and y are approximately equal to within a relative 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 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 packege fcmp by T.C. Belding.

4.6 Log10 and Decibels (dB)

procedure

(log10 x)  real?

  x : real?
Returns the log base 10 of x, log10(x).

procedure

(dB x)  real?

  x : real?
Returns the value of x in decibels, 10*log10(x).