SBCL 意外陷入浮点数?
SBCL surprise lapse into floating point?
考虑 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))
这给出了预期的 bignum 值 60!,即
8320987112741390144276341183223364380754172606361245952449277696409600000000000000
但是,下面的代码(不包括我能看到的任何浮点数强制转换)会生成一个浮点数答案:
(defun fact (n)
(if (= 0 n)
1
(* n (fact (- n 1)))))
(fact 60)
8.32098711274139e+81
问题是"why?"和"how can I write a straightforward, recursive fact
in SBCL (Steel-Bank Common Lisp) that produces a bignum result?"
强制转换发生在 emacs 端。 ob-lisp.el 调用读取结果。尝试在 *scratch* 缓冲区上对其进行评估,以亲自查看
(read "8320987112741390144276341183223364380754172606361245952449277696409600000000000000")
考虑 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))
这给出了预期的 bignum 值 60!,即
8320987112741390144276341183223364380754172606361245952449277696409600000000000000
但是,下面的代码(不包括我能看到的任何浮点数强制转换)会生成一个浮点数答案:
(defun fact (n)
(if (= 0 n)
1
(* n (fact (- n 1)))))
(fact 60)
8.32098711274139e+81
问题是"why?"和"how can I write a straightforward, recursive fact
in SBCL (Steel-Bank Common Lisp) that produces a bignum result?"
强制转换发生在 emacs 端。 ob-lisp.el 调用读取结果。尝试在 *scratch* 缓冲区上对其进行评估,以亲自查看
(read "8320987112741390144276341183223364380754172606361245952449277696409600000000000000")