在您的示例中,可以使用 Integer.MIN_VALUE 代替“负无穷大”。然而, 如果算法要求您使用Integer.MIN_VALUE作为实际值,那么您不能也将其用作“无值”标记。 Integer.MIN_VALUE 不能作为“负无穷大”代理的另一种情况是当您使用它进行算术时。 (Integer.MIN_VALUE + x == Integer.MIN_VALUE 仅适用于 x == 0。)
I was wondering though if there is a more generic way/trick to represent this when implementing the algorithm in a concrete programming language
In general no. There is no way to represent "minus infinity" as an integer that will always work. The smallest possible integer value (in Java Integer.MIN_VALUE) is often used. But you need to look at the specific algorithm to understand if this solution works.
Hence there is no general / generic solution.
In your example Integer.MIN_VALUE can be used in place of "minus infinity". However, if an algorithm requires you to use Integer.MIN_VALUE as an actual value, then you can't also use it as a "no value" marker. Another case where Integer.MIN_VALUE doesn't work as a proxy for "minus infinity" is when you use it to do arithmetic. (Integer.MIN_VALUE + x == Integer.MIN_VALUE only holds for x == 0.)
最后,总是有一个 boolean firstTime = true; 选项,您可以在第一次时针对 if 条件|| 进行设置,并立即设置将其设置为false。
You can use Integer.MIN_VALUE which is the smallest 32-bit representable value (-2^31).
Alternatively, if this could actually be a valid value in your problem, you could extend to 64 bits and just use Long.MIN_VALUE.
And finally, there's always the option of boolean firstTime = true; which you || against your if condition the first time around, and immediately set it to false.
Integer.MIN_VALUE and Integer.MAX_VALUE bad solutions because in some cases they can become values in your domain. More carefully using Double.NEGATIVE_INFINITY.
For example Double.NEGATIVE_INFINITY + 2 = Double.NEGATIVE_INFINITY
发布评论
评论(5)
一般来说没有。没有办法将“负无穷大”表示为始终有效的整数。通常使用尽可能小的整数值(在 Java
Integer.MIN_VALUE
中)。但你需要查看具体的算法来了解这个解决方案是否有效。因此,没有通用/通用的解决方案。
在您的示例中,可以使用
Integer.MIN_VALUE
代替“负无穷大”。然而,如果算法要求您使用
Integer.MIN_VALUE
作为实际值,那么您不能也将其用作“无值”标记。Integer.MIN_VALUE
不能作为“负无穷大”代理的另一种情况是当您使用它进行算术时。 (Integer.MIN_VALUE + x == Integer.MIN_VALUE
仅适用于x == 0
。)In general no. There is no way to represent "minus infinity" as an integer that will always work. The smallest possible integer value (in Java
Integer.MIN_VALUE
) is often used. But you need to look at the specific algorithm to understand if this solution works.Hence there is no general / generic solution.
In your example
Integer.MIN_VALUE
can be used in place of "minus infinity". However,if an algorithm requires you to use
Integer.MIN_VALUE
as an actual value, then you can't also use it as a "no value" marker. Another case whereInteger.MIN_VALUE
doesn't work as a proxy for "minus infinity" is when you use it to do arithmetic. (Integer.MIN_VALUE + x == Integer.MIN_VALUE
only holds forx == 0
.)您可以使用
Integer.MIN_VALUE
这是最小的 32 位可表示值 (-2^31)。
或者,如果这实际上是您问题中的有效值,您可以扩展到 64 位并仅使用
Long.MIN_VALUE
。最后,总是有一个
boolean firstTime = true;
选项,您可以在第一次时针对if
条件||
进行设置,并立即设置将其设置为false
。You can use
Integer.MIN_VALUE
which is the smallest 32-bit representable value (-2^31).Alternatively, if this could actually be a valid value in your problem, you could extend to 64 bits and just use
Long.MIN_VALUE
.And finally, there's always the option of
boolean firstTime = true;
which you||
against yourif
condition the first time around, and immediately set it tofalse
.这给出了 current_sum int 可以具有的最小值,-(2^31)。
This gives current_sum the minimum value an int can have, -(2^31).
通常,您用适合您的数据类型的最大值来表示无穷大。
例如,如果您有 +inifity,则可以使用
Integer.MAX_VALUE
。与 -infinity 相同,您可以使用
Integer.MIN_VALUE
。其他数据类型也是如此。
Normally you are representing infinity with the maximum that can fit in your datatype.
For example if you have +inifity, you can use
Integer.MAX_VALUE
.Same with -infinity, you can use
Integer.MIN_VALUE
.It's the same with other datatypes.
Integer.MIN_VALUE
和Integer.MAX_VALUE
不好的解决方案,因为在某些情况下它们可能会成为您域中的值。更仔细地使用Double.NEGATIVE_INFINITY
。例如
Double.NEGATIVE_INFINITY + 2 = Double.NEGATIVE_INFINITY
Integer.MIN_VALUE
andInteger.MAX_VALUE
bad solutions because in some cases they can become values in your domain. More carefully usingDouble.NEGATIVE_INFINITY
.For example
Double.NEGATIVE_INFINITY + 2 = Double.NEGATIVE_INFINITY