Common Lisp the Language, 2nd Edition


next up previous contents index
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.

change_begin
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).
change_end

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.




next up previous contents index
Next: PrecisionContagion, and Up: Common Lisp the Language Previous: An Example


AI.Repository@cs.cmu.edu