bignum in emacs/elisp

Emacs Lispers frustrated by Emacs’s lack of bignum handling: calc.el provides very good bignum capabilities.—EmacsWiki

calc.el is part of the GNU Emacs distribution. See its source code for available functions. You can immediately start playing with it by typing M-x quick-calc. You may also want to check bigint.el package, that is a non-standard, lightweight implementation for handling bignums.


Emacs 27.1 supports bignums natively (see the NEWS file of Emacs):

** Emacs Lisp integers can now be of arbitrary size. Emacs uses the GNU Multiple Precision (GMP) library to support integers whose size is too large to support natively. The integers supported natively are known as "fixnums", while the larger ones are "bignums". The new predicates 'bignump' and 'fixnump' can be used to distinguish between these two types of integers.

All the arithmetic, comparison, and logical (a.k.a. "bitwise") operations where bignums make sense now support both fixnums and bignums. However, note that unlike fixnums, bignums will not compare equal with 'eq', you must use 'eql' instead. (Numerical comparison with '=' works on both, of course.)

Since large bignums consume a lot of memory, Emacs limits the size of the largest bignum a Lisp program is allowed to create. The nonnegative value of the new variable 'integer-width' specifies the maximum number of bits allowed in a bignum. Emacs signals an integer overflow error if this limit is exceeded.

Several primitive functions formerly returned floats or lists of integers to represent integers that did not fit into fixnums. These functions now simply return integers instead. Affected functions include functions like 'encode-char' that compute code-points, functions like 'file-attributes' that compute file sizes and other attributes, functions like 'process-id' that compute process IDs, and functions like 'user-uid' and 'group-gid' that compute user and group IDs.

Bignums are automatically chosen when arithmetic calculations with fixnums overflow the fixnum-range. The expression (bignump most-positive-fixnum) returns nil while (bignump (+ most-positive-fixnum 1)) returns t.