方案-“不精确” R5RS数字塔中的概念

发布于 2024-10-16 22:04:53 字数 516 浏览 8 评论 0原文

在思考如何实施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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海风掠过北极光 2024-10-23 22:04:53

实现不需要记住精确的等价物,因为根据 R5RS,如果操作涉及不精确的操作数,则可以产生不精确的结果。例如:

> (+ -1.0 2)
=> 1.0

在内部,解释器可以将 2 升级为 float 并调用 float 的加法操作,无需记住任何内容:

/* Assuming that the interpreter implements primitive operations in C. */
SchemeObject* addInts(SchemeObject* a, SchemeObject* b)
{
    if (a->type == FLOAT || b->type == FLOAT)
    {
        cast_int_value_to_float (a);
        cast_int_value_to_float (b);
        return addFloats (a, b);
    }
    return make_new_int_object (get_int_value (a) + get_int_value (b));
 }

实际上,Scheme 中的上述加法被解释器视为:

> (+ -1.0 2.0)
=> 1.0   

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:

> (+ -1.0 2)
=> 1.0

Internally, the interpreter can upgrade 2 to a float and call the addition operation for floats, there is no need to remember anything:

/* Assuming that the interpreter implements primitive operations in C. */
SchemeObject* addInts(SchemeObject* a, SchemeObject* b)
{
    if (a->type == FLOAT || b->type == FLOAT)
    {
        cast_int_value_to_float (a);
        cast_int_value_to_float (b);
        return addFloats (a, b);
    }
    return make_new_int_object (get_int_value (a) + get_int_value (b));
 }

In effect, the above addition in Scheme is treated by the interpreter as:

> (+ -1.0 2.0)
=> 1.0   
孤独陪着我 2024-10-23 22:04:53

它不必“记住”参数的原始准确性。它可以在计算过程中临时(内部)将数字转换为精确值,如果任何参数不精确,则将结果标记为不精确。

示例:(

(denominator 1/10)  ; 10
(denominator 0.1)   ; 3.602879701896397e+16

后一个结果取决于实现。我引用的数字来自在 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:

(denominator 1/10)  ; 10
(denominator 0.1)   ; 3.602879701896397e+16

(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 get 1/10, unless your implementation uses decimal floating-point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文