gpt4 book ai didi

racket - 为什么 Racket 报告 π 是有理数?

转载 作者:行者123 更新时间:2023-12-04 19:29:05 26 4
gpt4 key购买 nike

正如任何中学数学学生都可以证明的那样,pi 是不合理的。

但是:

Welcome to Racket v5.3.6.
> pi
3.141592653589793
> (rational? pi)
#t

这是因为 pi 在底层机器的浮点格式中的表示精度有限,因此总是可以表示为某个 p/q,其中 q 是 10^n,n 是表示精度?

如果是这样,Racket(或其他类似行为的计划)抛出的任何数字怎么可能被认为是不合理的?因此,为什么要打扰 rational?功能?

更新:(rational? (sqrt 3))报告 #t

最佳答案

pi 返回的数字是理性的,因为documentation这么说。具体说:

All numbers are complex numbers. Some of them are real numbers, and all of the real numbers that can be represented are also rational numbers, except for +inf.0 (positive infinity), +inf.f (single-precision variant), -inf.0 (negative infinity), -inf.f (single-precision variant), +nan.0 (not-a-number), and +nan.f (single-precision variant). Among the rational numbers, some are integers, because round applied to the number produces the same number.



所以你的预感是对的。所有可表示的实数确实是有理数(无穷大和 NaN 除外),因为是的,数字存储在固定大小的寄存器中,因此机器不会存储无理数。

至于为什么 Racket 设计者会为 rational? 烦恼函数,这是个好问题。许多语言,如 Julia 和 Clojure,都有一个真实的、实际的、诚实的理性数据类型。 Racket 没有,因此,正如您所怀疑的那样,将实数的近乎完整的子集定义为有理数似乎很愚蠢。

但是您知道,有一种方法来讨论非 NaN、非 Infinity 值可能会很方便。我会称它为 finite ,但 Racket 称它为 rational .

关于racket - 为什么 Racket 报告 π 是有理数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46986587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com