lisp - 获取字符串而不是数字

标签 lisp common-lisp

这很好用:

[1]> ((lambda (x) (/ x x)) 5)
1

但是这个:

[2]> ((lambda (x y) (/ x y)) 5 2)
5/2

给我“5/2”而不是 2.5。我该如何解决?

最佳答案

Common Lisp 尽可能执行有理算术。如果您需要 float ,则必须至少提供一个 float 作为算术函数的输入,或者对结果使用显式强制转换函数。

((lambda (x y) (float (/ x y)) 5 2)

((lambda (x y) (/ x y)) 5.0 2)

有理算术通常比 float 更精确。考虑一下:

(setf x1 (/ 1 3)) => 1/3
(setf x2 (float (/ 1 3)) => 0.33333333
(* x1 3) => 1
(* x2 3) => 0.99999999

关于lisp - 获取字符串而不是数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16154556/

相关文章:

com - 是否有开源的 Common Lisp COM 包装器?

sockets - 从 lisp 中的套接字流读取行

lisp - Common Lisp - 如何组合这两个简单的多项式函数?

compiler-construction - 使用读取宏编译 Lisp 代码

lisp - 从 Common Lisp 的列表中按值删除重复项

recursion - 普通口齿不清 : Recursive "is-equal" function - results are incorrect

data-structures - Common Lisp 的优先级队列?

windows - 我怎样才能简单地 "run"lisp 文件

lisp - 关于 Common Lisp 作用域的微妙之处(动态与词法)

common-lisp - 在Common Lisp中,符号名称是否有定义的最大长度?