Search code examples
lispelispinteger-overflow

elisp: number can't get greater than (expt 2 60)?


In a previous post, someone shows that you can express any number in elisp because it switchs over to big_nums automatically.

But when I call (setq long_max (expt 2 60)), I get

1152921504606846976

And when I call (setq long_max (expt 2 61)), I get

-2305843009213693952

And when I call (setq long_max (expt 2 62)), I get

0

What gives? I found that the numbers from the link above, 32768 32768, get me 0 as well. Why am I not getting the same sized integers. For context, I am using Spacemacs with Org-Mode

Thanks!


Solution

  • Emacs Lisp does not have bignums by default, as answered here: bignum in emacs/elisp (which also references the powerful calc packages).

    The wrap-around behavior you see is because computation is made using modular arithmetic: see https://www.gnu.org/software/emacs/manual/html_node/elisp/Integer-Basics.html#Integer-Basics.