如何使用双浮点数? [英] How to use double-float?
问题描述
我正在努力尝试弄清楚如何告诉Lisp我想使用double-float值.假设我有:
I am struggling a little trying to figure out how to tell Lisp that I want to use double-float values. Suppose I have:
(let ((x 1)) (format t "~A~%" (/ x 3.0)))
哪个给:
0.33333334
如果我想使用浮点数,可以尝试以下方法:
If I want to use double-float, I tried this:
(let ((x 1)) (declare (type double-float x)) (format t "~A~%" (/ x 3.0)))
0.33333334
因此结果不是双精度浮点数.但是,我可以像这样强制双浮动:
So the result is not a double-float. I can, however, force double-float like this:
(let ((x 1)) (format t "~A~%" (/ x 3.0d0)))
0.3333333333333333d0
现在我得到一个双浮点结果.
And now I get a double-float result.
所以我的问题是:如果我要定义一种形式或函数,希望算术运算为双精度浮点数,该如何确定呢?我已经阅读了许多有关使用declare
,proclaim
等的在线资源,但是还无法将其应用于获得我想要的结果.我不敢相信我知道如何在这种情况下利用它们,即使它们是正确的使用机制.
So my question is: if I'm defining a form or function in which I want arithmetic to be in double-float, how do I establish that? I've read lots of online resources on using declare
, proclaim
, etc, but haven't been able to apply it to getting the result I'm after. I'm not convinced I know how to utilize these in this context, or even if they are the correct mechanism to use.
如果我尝试执行long-float
或不是默认值的其他任何操作,也会出现相同的问题.
Same question would apply if I were trying to do long-float
or anything else that isn't the default.
推荐答案
如果要使用特殊的float格式进行计算,则必须告诉它.通常,如果您将双精度浮点数相除,则结果将是双精度浮点数.如果您有常量,则需要这样表示.
If you want to compute with a special float format you have to tell it. Usually if you divide double-floats, the result will be a double float. If you have constants, you need to denote them as such.
Common Lisp标准说:数值函数的结果是该函数的所有浮点参数中最大格式的浮点数..
The Common Lisp standard says: The result of a numerical function is a float of the largest format among all the floating-point arguments to the function..
以下内容的解释取决于几件事.这取决于阅读器阅读数字的方式.对于整数,可以指定基数.对于浮点数,取决于默认的浮点格式.
The interpretation of the following depends on a few things. It depends on how the reader reads numbers. For integers the base can be specified. For floats it depends on the default float format.
(let ((x 1)) (format t "~A~%" (/ x 3.0)))
让我们看看 *read-default-float-format*
会如何影响它:
Let's see how *read-default-float-format*
affects it:
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
还请注意,您可以使用指数标记:
Also note that you can specify the type for literal numbers by using an exponent marker:
- d =双浮点
- e =
*read-default-float-format*
的浮点数
- f =单浮点
- l =浮空
- s =浮空
- d = double-float
- e = float of
*read-default-float-format*
- f = single-float
- l = long-float
- s = short-float
示例:
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
明确指出了您所指的类型:
You can also coerce numbers to a certain type. The function COERCE
makes it explicit which type you mean:
CL-USER 17 > (let ((x 1))
(format t "~A~%" (/ (coerce x 'double-float) 3.0)))
0.3333333333333333D0
NIL
这篇关于如何使用双浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!