Search code examples
common-lispsbclorg-babel

SBCL surprise lapse into floating point?


Consider the following code in Common Lisp:

  (defun range (max &key (min 0) (step 1))
    (loop for n from min below max by step
          collect n))
  (reduce #'* (range 61 :min 1))

This gives the expected bignum value of 60!, namely

8320987112741390144276341183223364380754172606361245952449277696409600000000000000

However, the following code, which doesn't include any floating-point coercions that I can see, produces a floating-point answer:

  (defun fact (n)
    (if (= 0 n)
        1
        (* n (fact (- n 1)))))

  (fact 60)
  8.32098711274139e+81

The questions are "why?" and "how can I write a straightforward, recursive fact in SBCL (Steel-Bank Common Lisp) that produces a bignum result?"


Solution

  • The coercion happens on the emacs side. ob-lisp.el calls read on the result. Try evaluating this on the *scratch* buffer to see for your self

    (read "8320987112741390144276341183223364380754172606361245952449277696409600000000000000")