编程语言在什么情况下真正利用了Infinity值? [英] In what contexts do programming languages make real use of an Infinity value?

查看:68
本文介绍了编程语言在什么情况下真正利用了Infinity值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此在Ruby中,有一个技巧可以指定无穷大:

So in Ruby there is a trick to specify infinity:

1.0/0
=> Infinity

我相信在Python中您可以做类似的事情

I believe in Python you can do something like this

float('inf')

这些只是示例,我敢肯定,大多数语言在某种程度上都具有无限性.您何时真正在现实世界中使用此构造?为什么在范围内使用它比仅使用布尔表达式更好?例如

These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use this construct in the real world? Why would using it in a range be better than just using a boolean expression? For instance

(0..1.0/0).include?(number) == (number >= 0) # True for all values of number
=> true

总而言之,我正在寻找的是使用Infinity的现实原因.

To summarize, what I'm looking for is a real world reason to use Infinity.

编辑:我正在寻找真实的代码.很好地说这是什么时候您可以"使用它,何时人们实际上使用它.

EDIT: I'm looking for real world code. It's all well and good to say this is when you "could" use it, when have people actually used it.

推荐答案

Dijkstra算法通常将无穷大指定为图形中的初始边缘权重.不必是"infinity",只是一些任意常量,但是在Java中,我通常使用Double.Infinity.我认为可以类似地使用ruby.

Dijkstra's Algorithm typically assigns infinity as the initial edge weights in a graph. This doesn't have to be "infinity", just some arbitrarily constant but in java I typically use Double.Infinity. I assume ruby could be used similarly.

这篇关于编程语言在什么情况下真正利用了Infinity值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆