Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
这工作正常:
[1]> ((lambda (x) (/ x x)) 5) 1
但是这个:
[2]> ((lambda (x y) (/ x y)) 5 2) 5/2
给我“5/2”而不是 2.5。我该如何解决?
Common Lisp 尽可能执行有理算术。如果你想要浮点数,你要么必须提供至少一个浮点数作为算术函数的输入,要么对结果使用显式强制函数。
((lambda (x y) (float (/ x y)) 5 2)
或者
((lambda (x y) (/ x y)) 5.0 2)
有理算术通常比浮点更精确。考虑一下:
(setf x1 (/ 1 3)) => 1/3 (setf x2 (float (/ 1 3)) => 0.33333333 (* x1 3) => 1 (* x2 3) => 0.99999999