如果你想使用特殊的浮点格式进行计算,你必须告诉它。通常,如果将双浮点数相除,结果将是双浮点数。如果有常量,则需要这样表示它们。
Common Lisp 标准说:数值函数的结果是该函数的所有浮点参数中最大格式的浮点数。.
以下内容的解释取决于一些事情。这取决于读者如何读取数字。对于整数,可以指定基数。对于浮点数,它取决于默认的浮点数格式。
(let ((x 1)) (format t "~A~%" (/ x 3.0)))
让我们看看如何*read-default-float-format* http://www.lispworks.com/documentation/HyperSpec/Body/v_rd_def.htm影响它:
CL-USER 9 > *read-default-float-format*
SINGLE-FLOAT
CL-USER 10 > (let ((x 1)) (format t "~A~%" (/ x 3.0)))
0.33333334
NIL
CL-USER 11 > (setf *read-default-float-format* 'double-float)
DOUBLE-FLOAT
CL-USER 12 > (let ((x 1)) (format t "~A~%" (/ x 3.0)))
0.3333333333333333
NIL
另请注意,您可以使用指数标记 http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_e.htm#exponent_marker:
- d = 双浮点数
- e = 浮点数
*read-default-float-format*
- f = 单浮点数
- l = 长浮点数
- s = 短浮点
Example:
CL-USER 15 > (setf *read-default-float-format* 'single-float)
SINGLE-FLOAT
CL-USER 16 > (let ((x 1)) (format t "~A~%" (/ x 3.0d0)))
0.3333333333333333D0
NIL
你也可以coerce数字到某种类型。功能COERCE http://www.lispworks.com/documentation/HyperSpec/Body/f_coerce.htm#coerce明确您的意思是哪种类型:
CL-USER 17 > (let ((x 1))
(format t "~A~%" (/ (coerce x 'double-float) 3.0)))
0.3333333333333333D0
NIL