因此,在 Ruby 中,有一个指定无穷大的技巧:
1.0/0
=> Infinity
我相信在 Python 中你可以做这样的事情,
float('inf')
尽管这些只是示例,但我确信大多数语言在某些方面都具有无穷大。 您何时会在现实世界中实际使用此构造? 为什么在范围内使用它比仅使用布尔表达式更好? 例如
(0..1.0/0).include?(number) == (number >= 0) # True for all values of number
=> true
,总而言之,我正在寻找的是使用 Infinity 的现实原因。
编辑:我正在寻找现实世界的代码。 可以说,当你“可以”使用它时,当人们实际使用它时,这一切都很好。
So in Ruby there is a trick to specify infinity:
1.0/0
=> Infinity
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
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.
发布评论
评论(24)
Dijkstra 算法通常指定无穷大作为图中的初始边权重。 这不一定是“无穷大”,只是一些任意常数,但在 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.
在搜索最小值时,它可以用作初始值。
例如:
我更喜欢将
min
最初设置为somelist
的第一个值当然,在 Python 中,您应该在大多数情况下使用 min() 内置函数案例。
Off the top of the head, it can be useful as an initial value when searching for a minimum value.
For example:
Which I prefer to setting
min
initially to the first value ofsomelist
Of course, in Python, you should just use the min() built-in function in most cases.
似乎隐含着“为什么这个功能存在?” 在你的问题中。 原因是 Ruby 和 Python 只是提供对 IEEE 指定的浮点形式指定的全部值的访问。
这个页面似乎描述得很好:
http://steve.hollasch.net/cgindex/coding/ieeefloat.html
因此,您还可以拥有 NaN(非数字)值和 -0.0,但您可能不会立即拥有这些值的实际用途。
There seems to be an implied "Why does this functionality even exist?" in your question. And the reason is that Ruby and Python are just giving access to the full range of values that one can specify in floating point form as specified by IEEE.
This page seems to describe it well:
http://steve.hollasch.net/cgindex/coding/ieeefloat.html
As a result, you can also have NaN (Not-a-number) values and -0.0, while you may not immediately have real-world uses for those either.
在某些物理计算中,您可以将相同数量级的不规则性(即无限数)归一化,将它们两者抵消并得出近似结果。
当您处理极限时,诸如 (无穷大/无穷大) -> 之类的计算 可以达到接近有限的数量。 对于语言来说,能够覆盖常规被零除错误是很有用的。
In some physics calculations you can normalize irregularities (ie, infinite numbers) of the same order with each other, canceling them both and allowing a approximate result to come through.
When you deal with limits, calculations like (infinity / infinity) -> approaching a finite a number could be achieved. It's useful for the language to have the ability to overwrite the regular divide-by-zero error.
在实现数学算法时需要使用
Infinity
和-Infinity
。在 Ruby 中,
Infinity
和-Infinity
具有很好的比较属性,因此-Infinity
-Infinity
-Infinity
-Infinity
x
<Infinity
对于任何实数x
。 例如,Math.log(0)
返回-Infinity
,将0
的属性扩展为x > 。 y
意味着 Math.log(x) > Math.log(y)。 另外,如果 x > ,Infinity * x
就是Infinity
。 0,-Infinity
如果 x如果 x 为 0,则为“NaN”(不是数字;即未定义)。
例如,我在某些 对数似然比。 我显式引用
-Infinity
来定义一个值,即使k
是0
或n
ANDx< /code> 是
0
或1
。Use
Infinity
and-Infinity
when implementing a mathematical algorithm calls for it.In Ruby,
Infinity
and-Infinity
have nice comparative properties so that-Infinity
<x
<Infinity
for any real numberx
. For example,Math.log(0)
returns-Infinity
, extending to0
the property thatx > y
implies thatMath.log(x) > Math.log(y)
. Also,Infinity * x
isInfinity
if x > 0,-Infinity
if x < 0, and 'NaN' (not a number; that is, undefined) if x is 0.For example, I use the following bit of code in part of the calculation of some log likelihood ratios. I explicitly reference
-Infinity
to define a value even ifk
is0
orn
ANDx
is0
or1
.Alpha-beta 剪枝
Alpha-beta pruning
我用它来指定物理模拟中静态物体的质量和惯性。 静态物体基本上不受重力和其他模拟力的影响。
I use it to specify the mass and inertia of a static object in physics simulations. Static objects are essentially unaffected by gravity and other simulation forces.
在 Ruby 中,无穷大可用于实现惰性列表。 假设我想要 N 个从 200 开始的数字,每次逐渐增大 100 个单位:
更多信息如下:http://banisterfiend.wordpress.com/2009/10/02/wtf-infinite-ranges-in-ruby/
In Ruby infinity can be used to implement lazy lists. Say i want N numbers starting at 200 which get successively larger by 100 units each time:
More info here: http://banisterfiend.wordpress.com/2009/10/02/wtf-infinite-ranges-in-ruby/
我将它用于您想要定义偏好/允许范围的情况。
例如,在 37signals 应用程序中,您对项目数量有限制,
那么您可以进行如下检查
I've used it for cases where you want to define ranges of preferences / allowed.
For example in 37signals apps you have like a limit to project number
then you can do checks like
我用它来表示相机焦距,令我惊讶的是,在 Python 中:
我想知道这是为什么。
I used it for representing camera focus distance and to my surprise in Python:
I wonder why is that.
我在 minimax 算法中使用了它。 当我生成新动作时,如果最小玩家在该节点上获胜,则该节点的值为 -∞。 相反,如果最大玩家获胜,则该节点的值是+∞。
另外,如果您要生成节点/游戏状态,然后尝试几种启发式方法,您可以将所有节点值设置为 -∞/+∞,这是有意义的,然后当您运行启发式方法时,可以轻松设置节点值:
I've used it in the minimax algorithm. When I'm generating new moves, if the min player wins on that node then the value of the node is -∞. Conversely, if the max player wins then the value of that node is +∞.
Also, if you're generating nodes/game states and then trying out several heuristics you can set all the node values to -∞/+∞ which ever makes sense and then when you're running a heuristic its easy to set the node value:
我在类似于 Rails 的
has_one
和has_many
的 DSL 中使用了它:这使得在 DSL 中表达 Kleene star 和 plus 等概念变得很容易。
I've used it in a DSL similar to Rails'
has_one
andhas_many
:This makes it easy to express concepts like Kleene star and plus in your DSL.
当我有一个 Range 对象,其中一端或两端需要打开时,我会使用它
I use it when I have a Range object where one or both ends need to be open
我在处理范围比较时使用了正无穷大和负无穷大的符号值,以消除否则需要特殊处理的极端情况:
给定两个范围 A=[a,b) 和 C=[c,d) 它们是否相交,是一个大于另一个,或者一个包含另一个?
如果您的正无穷大值和负无穷大值分别大于和小于所有其他值,则无需对开放式范围进行任何特殊处理。 由于浮点数和双精度数已经实现了这些值,因此您不妨使用它们,而不是尝试查找平台上的最大/最小值。 对于整数,使用“无穷大”会更困难,因为硬件不支持它。
I've used symbolic values for positive and negative infinity in dealing with range comparisons to eliminate corner cases that would otherwise require special handling:
Given two ranges A=[a,b) and C=[c,d) do they intersect, is one greater than the other, or does one contain the other?
If you have values for positive and negative infinity that respectively compare greater than and less than all other values, you don't need to do any special handling for open-ended ranges. Since floats and doubles already implement these values, you might as well use them instead of trying to find the largest/smallest values on your platform. With integers, it's more difficult to use "infinity" since it's not supported by hardware.
我遇到这个问题是因为我正在寻找一个“无限”值来设置最大值(如果给定值不存在),以尝试创建二叉树。 (因为我是根据一系列值进行选择,而不仅仅是单个值,所以我很快意识到即使是哈希值在我的情况下也不起作用。)
由于我希望所有涉及的数字都是正数,所以最小值很容易:0。不过,由于我不知道最大值会是什么,所以我希望上限是某种形式的无穷大。 这样,我就不必弄清楚我应该将事物与什么“最大值”进行比较。
由于这是我在工作中正在进行的一个项目,因此从技术上来说它是一个“现实世界问题”。 它可能有点罕见,但就像许多抽象一样,当你需要它时它很方便!
另外,对于那些说这个(和其他例子)是人为的人,我想指出所有抽象都有些人为; 这并不意味着当你设计它们时它们是有用的。
I ran across this because I'm looking for an "infinite" value to set for a maximum, if a given value doesn't exist, in an attempt to create a binary tree. (Because I'm selecting based on a range of values, and not just a single value, I quickly realized that even a hash won't work in my situation.)
Since I expect all numbers involved to be positive, the minimum is easy: 0. Since I don't know what to expect for a maximum, though, I would like the upper bound to be Infinity of some sort. This way, I won't have to figure out what "maximum" I should compare things to.
Since this is a project I'm working on at work, it's technically a "Real world problem". It may be kindof rare, but like a lot of abstractions, it's convenient when you need it!
Also, to those who say that this (and other examples) are contrived, I would point out that all abstractions are somewhat contrived; that doesn't mean they are useful when you contrive them.
当在使用三角函数(尤其是正切)的问题域中工作时,无穷大是可能出现的答案。 三角函数最终广泛用于图形应用程序、游戏和地理空间应用程序,以及显而易见的数学应用程序。
When working in a problem domain where trig is used (especially tangent) infinity is an answer that can come up. Trig ends up being used heavily in graphics applications, games, and geospatial applications, plus the obvious math applications.
我确信还有其他方法可以做到这一点,但您可以使用 Infinity 来检查字符串到浮点转换中的合理输入。 至少在 Java 中,Float.isNaN() 静态方法将为无穷大的数字返回 false,表明它们是有效数字,即使您的程序可能希望将它们分类为无效数字。 检查 Float.POSITIVE_INFINITY 和 Float.NEGATIVE_INFINITY 常量可以解决该问题。 例如:
示例输出:
字符串表示形式:-999999999999999999999999999999999999999999999
isNaN 的结果: false
这个数字太小了。
字符串表示形式:12345
isNaN 的结果: false
这个数字是正确的。
字符串表示:999999999999999999999999999999999999999999999
isNaN 的结果: false
这个数字太大了。
I'm sure there are other ways to do this, but you could use Infinity to check for reasonable inputs in a String-to-Float conversion. In Java, at least, the Float.isNaN() static method will return false for numbers with infinite magnitude, indicating they are valid numbers, even though your program might want to classify them as invalid. Checking against the Float.POSITIVE_INFINITY and Float.NEGATIVE_INFINITY constants solves that problem. For example:
Sample Output:
String representation: -999999999999999999999999999999999999999999999
Result of isNaN: false
That number is too small.
String representation: 12345
Result of isNaN: false
That number is jussssst right.
String representation: 999999999999999999999999999999999999999999999
Result of isNaN: false
That number is too big.
它在图形学中应用相当广泛。 例如,3D 图像中不属于实际对象的任何像素都被标记为无限远。 以便稍后可以用背景图像替换它。
It is used quite extensively in graphics. For example, any pixel in a 3D image that is not part of an actual object is marked as infinitely far away. So that it can later be replaced with a background image.
我正在使用一个网络库,您可以在其中指定重新连接尝试的最大次数。 因为我希望我的永远重新连接:
my_connection = ConnectionLibrary(max_connection_attempts = float('inf'))
在我看来,它比典型的“设置为 -1 以永远重试”风格更清晰,因为它的字面意思是“重试,直到连接尝试次数大于无穷大”。
I'm using a network library where you can specify the maximum number of reconnection attempts. Since I want mine to reconnect forever:
my_connection = ConnectionLibrary(max_connection_attempts = float('inf'))
In my opinion, it's more clear than the typical "set to -1 to retry forever" style, since it's literally saying "retry until the number of connection attempts is greater than infinity".
一些程序员使用 Infinity 或 NaN 来表示变量从未在程序中初始化或分配。
Some programmers use Infinity or
NaN
s to show a variable has never been initialized or assigned in the program.如果您想要输入中的最大数字,但他们可能会使用非常大的负数。 如果我输入 -13543124321.431,它仍然是最大的数字,因为它比 -inf 大。
If you want the largest number from an input but they might use very large negatives. If I enter -13543124321.431 it still works out as the largest number since it's bigger than -inf.
您可以使用:
或:
You can to use:
or:
对于排序,
我看到它用作排序值,表示“始终将这些项目排序到底部”。
For sorting
I've seen it used as a sort value, to say "always sort these items to the bottom".
指定不存在的最大值
如果您正在处理数字,则
nil
表示未知数量,在这种情况下应优先于0
。 同样,Infinity
表示无界数量,在这种情况下应优先于(任意大的数字)
。我认为它可以使代码更清晰。 例如,我正在使用
Float::INFINITY
在 Ruby gem 中就是这样:用户可以指定消息的最大字符串长度,也可以指定:all
。 在这种情况下,我将最大长度表示为 Float::INFINITY ,以便稍后当我检查“此消息是否比最大长度长?”时 答案总是错误的,不需要特殊情况。To specify a non-existent maximum
If you're dealing with numbers,
nil
represents an unknown quantity, and should be preferred to0
for that case. Similarly,Infinity
represents an unbounded quantity, and should be preferred to(arbitrarily_large_number)
in that case.I think it can make the code cleaner. For example, I'm using
Float::INFINITY
in a Ruby gem for exactly that: the user can specify a maximum string length for a message, or they can specify:all
. In that case, I represent the maximum length asFloat::INFINITY
, so that later when I check "is this message longer than the maximum length?" the answer will always be false, without needing a special case.