Next: Category rational, Up: Unions of Number Types [Contents][Index]
The category of integers is probably the most transparent one. This is of course due to the fact, that SXEmacs did provide integers before. Now it would be unregrettably stupid, if we invented another read-syntax for the additional integers. Hence big integers are unified transparently with ordinary 28-bit integers. The subcategory of ordinary emacs integers is called int or fixnum.
Nevertheless, we provide functions to distinguish them, and to coerce from the one type to the other.
This predicate tests whether its argument is an ordinary emacs
integer, and return t
if so, nil
otherwise.
This is roughly the same as intp
and is provided for
compatibility to XEmacs.
This predicate tests whether its argument is a big integer (as
provided by GMP-MPZ or BSD-MP), and returns t
if so, nil
otherwise.
This is roughly the same as bigzp
and is provided for
compatibility to XEmacs.
Unfortunately, ordinary emacs integers show some behaviour which in the new category of integers is not bearable anymore. This may lead to confusion in simpler cases, if not even to wrong code.
The following example shows the most significant change:
In an old fixnum-only SXEmacs: (+ 1 134217727) ⇒ -134217728
The same in a SXEmacs with ENT:
(+ 1 134217727) ⇒ 134217728
Now the resulting integer is of bigz type.
(bigzp (+ 1 134217727)) ⇒ t
Now, in general we can state, that whenever space is too tight for an integer to fit into the built-in integer type, this integer is coerced to the type ‘bigz’.
The reverse is not necessarily true. But there is an auto-coercion, known as canonicalisation, to bang big integers to ints, when they are small enough and to bang fractions which cancel to denominator 1 to integers, but some functions acquiesce their output as is. See the following examples:
(bigzp (2^ 42)) ⇒ t (bigzp (/ (2^ 42) (2^ 30))) ⇒ nil
(factorial 4) ⇒ 24 (bigzp (factorial 4)) ⇒ t
We say, that /
canonicalises its results, whereas
factorial
does not. Canonicalisation usually depends on the
size of output compared to average input values. This is much too
vague for practice.
However, in order to be absolutely sure about the world where resulting integers (even fractions which cancel to integers) live, there is a canonicalisation function.
Return the canonical form of number.
This function determines the smallest category a number can live in, and coerces to there. Hereby smallest category means the category with the least cardinality.
(bigzp (factorial 4)) ⇒ t (bigzp (canonicalize-number (factorial 4))) ⇒ nil
Canonicalisation actually begins in the expression reader already, consider the quotient ‘4/2’, which cancels to ‘2’.
4/2 ⇒ 2 (bigqp 4/2) ⇒ nil
Next: Category rational, Up: Unions of Number Types [Contents][Index]