bignum in emacs/elisp
Asked Answered
T

2

12

Does emacs have support for big numbers that don't fit in integers? If it does, how do I use them?

Takahashi answered 3/9, 2009 at 6:1 Comment(0)
B
12

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.

Belgian answered 3/9, 2009 at 7:5 Comment(1)
Looks like this'll do the job... calc looks a bit daunting at first look, but I'll figure it out eventually. Thanks.Takahashi
D
2

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.

Device answered 8/9, 2020 at 9:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.