Common Lisp the Language, 2nd Edition

Next: PrecisionContagion, and Up: Common Lisp the Language Previous: An Example

# 12. Numbers

Common Lisp provides several different representations for numbers. These representations may be divided into four categories: integers, ratios, floating-point numbers, and complex numbers. Many numeric functions will accept any kind of number; they are generic. Other functions accept only certain kinds of numbers.

Note that this remark, predating the design of the Common Lisp Object System, uses the term ``generic'' in a generic sense and not necessarily in the technical sense used by CLOS (see chapter 2).

In general, numbers in Common Lisp are not true objects; `eq` cannot be counted upon to operate on them reliably. In particular, it is possible that the expression

    (let ((x z) (y z)) (eq x y))

may be false rather than true if the value of `z` is a number.

Rationale: This odd breakdown of `eq` in the case of numbers allows the implementor enough design freedom to produce exceptionally efficient numerical code on conventional architectures. MacLisp requires this freedom, for example, in order to produce compiled numerical code equal in speed to Fortran. Common Lisp makes this same restriction, if not for this freedom, then at least for the sake of compatibility.

If two objects are to be compared for ``identity,'' but either might be a number, then the predicate `eql` is probably appropriate; if both objects are known to be numbers, then `=` may be preferable.

- Precision, Contagion, and Coercion
- Predicates on Numbers
- Comparisons on Numbers
- Arithmetic Operations
- Irrational and Transcendental Functions
  - Exponential and Logarithmic Functions
  - Trigonometric and Related Functions
  - Branch Cuts, Principal Values, and Boundary Conditions in the Complex Plane
- Type Conversions and Component Extractions on Numbers
- Logical Operations on Numbers
- Byte Manipulation Functions
- Random Numbers
- Implementation Parameters

Next: PrecisionContagion, and Up: Common Lisp the Language Previous: An Example