Floating point SBCL convergence?

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 value of the bonus 60! Namely

8320987112741390144276341183223364380754172606361245952449277696409600000000000000

However, the following code, which does not include any restraining floating point operations that I see, produces a floating point response:

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

  (fact 60)
  8.32098711274139e+81

Questions: "why?" and "how can I write a simple, recursive factin SBCL (Steel-Bank Common Lisp) that produces a bignum result?"

+4
source share
1 answer

Coercion occurs on the side of emacs. ob-lisp.el causes the reading of the result. Try to evaluate it in the * scratch * buffer to see for yourself

(read "8320987112741390144276341183223364380754172606361245952449277696409600000000000000")
+5
source

Source: https://habr.com/ru/post/1618514/


All Articles