方案-“不精确” R5RS数字塔中的概念
在思考如何实施R5RS方案时,我对以下R5RS摘录(第22-23页)感到困惑:
(余数-13 -4) ==> -1
(余数-13 -4.0) ==> -1.0;不精确(lcm 32 -36) ==> 288
(lcm 32.0 -36) ==> 288.0;不精确(分母(/ 6 4))==> 2
(分母(精确->不精确(/ 6 4))) ==> 2.0
我们是否应该理解,即使 -4.0、32.0 和 (exact->inexact (/ 6 4)) 不精确,实现也应“记住”它们的精确等价物(-4、32 和 3/2 )以便进行整数除法、质因数分解等?
否则,实现如何能够成功给出上述答案?
预先感谢您对这个主题的任何启发! :)
尼古拉斯
While thinking about the way to implement Scheme R5RS, I have become puzzled about the following extract of R5RS (pages 22-23):
(remainder -13 -4) ==> -1
(remainder -13 -4.0) ==> -1.0 ; inexact(lcm 32 -36) ==> 288
(lcm 32.0 -36) ==> 288.0 ; inexact(denominator (/ 6 4)) ==> 2
(denominator (exact->inexact (/ 6 4))) ==> 2.0
Should we understand that, even if -4.0, 32.0 and (exact->inexact (/ 6 4)) are inexact, the implementation shall "remember" their exact equivalent (-4, 32 and 3/2) in order to proceed to integer division, prime factors decomposition, etc?
Otherwise, how could the implementation succeed in giving the above answers?
Thanks in advance for any light you could throw on this subject! :)
Nicolas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实现不需要记住精确的等价物,因为根据 R5RS,如果操作涉及不精确的操作数,则可以产生不精确的结果。例如:
在内部,解释器可以将
2
升级为float
并调用 float 的加法操作,无需记住任何内容:实际上,Scheme 中的上述加法被解释器视为:
There is no need for the implementation to remember the exact equivalent because as per R5RS it is OK to produce an inexact result given an operation involves an inexact operand. E.g:
Internally, the interpreter can upgrade
2
to afloat
and call the addition operation for floats, there is no need to remember anything:In effect, the above addition in Scheme is treated by the interpreter as:
它不必“记住”参数的原始准确性。它可以在计算过程中临时(内部)将数字转换为精确值,如果任何参数不精确,则将结果标记为不精确。
示例:(
后一个结果取决于实现。我引用的数字来自在 amd64 上运行的 Racket 5.0.2。您将从其他实现中得到不同的结果。)
对于潜伏者和档案:看起来不寻常的结果是因为大多数实现对不精确的数字使用 IEEE 754,这种数字(作为二进制浮点格式)无法以全精度表示 0.1(只有十进制浮点格式可以)。
事实上,如果您使用
(inexact->exact 0.1)
,您将不会得到1/10
,除非您的实现使用十进制浮点。It doesn't have to "remember" the original exactness of the arguments. It can temporarily (internally) convert the numbers to exact during the calculation, and tag the result as inexact if any argument is inexact.
Examples:
(The latter result is implementation-dependent. The number I quoted is from Racket 5.0.2 running on amd64. You'll get different results from other implementations.)
For lurkers and archives: the unusual-looking result is because most implementations use IEEE 754 for inexact numbers, which (being a binary floating-point format) cannot represent 0.1 with full precision (only a decimal floating-point format can).
In fact, if you use
(inexact->exact 0.1)
, you will not get1/10
, unless your implementation uses decimal floating-point.