其他语言是否有内置的理性类型(如方案)?

发布于 2024-11-15 01:27:28 字数 59 浏览 4 评论 0原文

我没听说过,大多数语言似乎只是进行整数除法或浮点数。是否发现它是方案中的问题,因此没有在其他语言中使用?

I haven't heard of any, most languages seem to just have division of ints round or be a floating point number. Was it found to be a problem in scheme and so not used in other languages?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

翻了热茶 2024-11-22 01:27:28

Common Lisp:

CL-USER> (+ 4/5 3/2)
23/10

因素:

( scratchpad ) 4/5 3/2 + .
23/10

Haskell 98:

Prelude> (4/5) + (3/2) :: Rational
23 % 10

Common Lisp:

CL-USER> (+ 4/5 3/2)
23/10

Factor:

( scratchpad ) 4/5 3/2 + .
23/10

Haskell 98:

Prelude> (4/5) + (3/2) :: Rational
23 % 10
毅然前行 2024-11-22 01:27:28

你问的是分数吗?如果是这样,Smalltalk 有它们:

(4/5) + (3/2) 

计算结果为:

(23/10)

Are you asking about fractions? If so, Smalltalk has them:

(4/5) + (3/2) 

evaluates to:

(23/10)
人事已非 2024-11-22 01:27:28

你问:“是不是发现scheme有问题,所以没有在其他语言中使用?”答案是“不”,但这是一个有趣的问题。从广义上讲,Scheme/Racket 或许是一个语言家族的象征,当在“正确”和“易于实现”之间进行选择时,它们每次都会选择“正确”。许多其他语言所做的选择是明确地将数字的表示形式公开为小型有限集的元素,并要求程序员在该范围内进行操作。相反,Scheme/Racket 提供了一种可以处理任意大数字的表示形式,仅受评估代码的机器内存的限制。这与决定没有什么不同
几乎所有现代编程语言都使用垃圾收集,而不是强迫程序员显式分配和释放内存。

正如克里斯指出的那样,数字作为有理数的表示几乎总是与“bignums”齐头并进。有很多语言支持 bignums——Scheme、Racket、Ruby、Python 等——当然,任何图灵完备的语言都可以扩展来处理 bignums,包括 C。

You ask: "Was it found to be a problem in scheme and so not used in other languages?" The answer to that is "no", but it's an interesting question. Broadly speaking, Scheme/Racket is perhaps emblematic of a family of languages which, when given a choice between "correct" and "simple to implement", choose "correct" every time. The choice made by many other languages is to explicitly expose the representations of numbers as elements of a small finite set, and to require the programmer to operate in that sphere. Scheme/Racket instead provides a representation that can handle arbitrarily large numbers, limited only by the memory of the machine evaluating the code. This is not unlike the decision
made by nearly all modern programming languages to use Garbage Collection, rather than forcing the programmer to explicitly allocate and deallocate memory.

As Chris points out, the representation of numbers as rationals almost always goes hand-in-hand with "bignums". There are a bunch of languages that support bignums--Scheme, Racket, Ruby, Python, etc.--and of course, any turing-complete language can be extended to handle bignums, including C.

梦在深巷 2024-11-22 01:27:28

该计划的合理性得到了大数的支持。大多数语言没有内置的 bignum。

在我看来,没有内置 bignum 的内置有理数是没有意义的,因为如果没有 bignum,您在某个点之后就会开始失去精度,并且您还可以通过使用浮点来提前了解损失。

Scheme's rationals have bignums backing them. Most languages don't have built-in bignums.

In my opinion, it's pointless to have built-in rationals without built-in bignums, because without bignums, you start to lose precision after a certain point, and you may as well be upfront about the lossiness by using floating-point.

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