在 Haskell 中,是否存在无穷大 :: Num a =>一个?
我正在尝试实现一种数据结构,如果我使用无穷大来进行数值比较,事情就会变得非常简单。请注意,这不是 maxBound/minBound,因为值可以 <= maxbound,但所有值都将 <= maxBound。无穷大。
没有希望了吗?
I'm trying to implement a data structure where if I had the use of infinity for numerical comparison purposes, it would simply things greatly. Note this isn't maxBound/minBound, because a value can be <= maxbound, but all values would be < infinity.
No hope?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
嗯,那又如何呢!事实证明,如果您只输入
1/0
,它就会返回Infinity
!在 ghci 上:当然它会永远运行,永远不会找到大于无穷大的数字。 (但请参阅下面关于
[1..]
实际行为的 ephemient 评论)Well how about that! It turns out if you just type
1/0
it returnsInfinity
! On ghci:and then of course it runs forever, never finding a number bigger than infinity. (But see ephemient's comments below on the actual behavior of
[1..]
)也许您想要 Maybe 类型?
然后为 Num a => 编写一个 Num 实例无限 a,具有您需要的数字规则。
Maybe you want a Maybe type?
then write a Num instance for Num a => Infinite a, with the numeric rules you need.
尝试这样的事情。但是,要获得
Num
操作(例如+
或-
),您需要为定义
类型。就像我为Num
实例>无限aOrd
类所做的那样。Try something like this. However, to get
Num
operations (like+
or-
) you will need to defineNum
instance forInfinitable a
type. Just like I've done it forOrd
class.看看我的 RangedSets 库,它以非常通用的方式执行此操作。我定义了一个“边界”类型,以便“边界 a”类型的值始终高于或低于任何给定的“a”。边界可以是“AboveAll”、“BelowAll”、“Above x”和“Below x”。
Take a look at my RangedSets library, which does exactly this in a very general way. I defined a "Boundary" type so that a value of type "Boundary a" is always either above or below any given "a". Boundaries can be "AboveAll", "BelowAll", "Above x" and "Below x".
您可以使用
ieee754
软件包 它提供了 <代码>IEEE类型类。该类型类有一个infinity
成员。该类型类针对Float
、Double
、CFloat
和CDouble
实现。因此,您可以安装 ieee-754 软件包,然后通过以下方式获得正无穷大(或负无穷大):
对于 Float 、Double 等.它实现为[源]:
You can work with the
ieee754
package which offers anIEEE
typeclass. This typeclass has aninfinity
member. The typeclass is implemented forFloat
,Double
,CFloat
andCDouble
.You thus install the
ieee-754
package, and then obtain positive infinity (or negative infinity) with:For
Float
s,Double
s, etc. it is implemented as [src]:如果您的用例是您的边界条件有时需要检查,但有时不需要,您可以这样解决:
If your use case is that you have boundary conditions that sometimes need to be checked, but sometimes not, you can solve it like this:
有一种基于非标准分析想法的更有原则的方法。给定特征为零的全序环 R,您可以考虑具有自然词典全序的洛朗环 R[inf,1/inf]。例如,您有:
这样,洛朗环 R[inf,1/inf] 又是一个全序 Z 代数,即
Num
的实例,以及您可能想要的其他细节,包括 + /-无穷大、+/-无穷小、二阶无穷小等。但请注意,这不是阿基米德,归纳法将不再起作用,这是一种二阶算术。如需实现,请查看此示例 。正如代码中的注释所示,这种构造应该适用于其他代数,例如列表单子。您可以想到两个元素“无限接近”“二阶无限远”等的列表(这导致了玫瑰树的泛化。)There is a more principled approach based on an idea from non-standard analysis. Given a totally ordered ring R of characteristic zero, you can consider the Laurent ring R[inf,1/inf] with the natural lexicographic total ordering. For example, you have:
This way the Laurent ring R[inf,1/inf] is again a totally ordered Z-algebra, i.e. an instance of
Num
, with other niceties you possibly want, including +/-infinity, +/-infinitesimal, second-order infinitesimals, etc.. But note that it's not Archimedian and induction will no longer work, which is a sort of second-order arithmetic. For implementation take a look at this example. As in the comment in the code this construction should work for other algebras, such as the list monad. You can think of lists where two elements are "infinitely close" "second-order infinitely far away" etc. (which leads to a generalization of rose trees.)