tags:

views:

67

answers:

3

I need infinite value to set for some edges in graph that do not exist. i cannot use negative values. I perhaps need to set largest possible value of int as infinite, but need to do that in platform independent way.

What is the best value to use for infinite value

A: 

There is Float.POSITIVE_INFINITY and Float.NEGATIVE_INFINITY. The same for Double.

Witek
A: 

If you're writing in Java, you could use null -- just make sure to check for it first to avoid NPEs.

Bugmaster
That assumes that you are using `Integer`, not `int`.
Stephen C
+4  A: 

The largest possible value for an int is Integer.MAX_VALUE. Because of Java's consistent ways, this is a constant (0x7fffffff). Watch for inadvertent integer overflows.

If you are set on using ints, Integer.MIN_VALUE may be preferable. With this value excluded, the number of mathematically positive int values is equals to the number of negative values. An interesting property of this value is that, like 0, Integer.MIN_VALUE = -Integer.MIN_VALUE.

Tom Hawtin - tackline
"Because of Java's consistent ways" ... specifically, it is because the JLS and JVM specs REQUIRE 'int' to be 32 bit signed (2's complement) on every platform. (By comparison, C and C++ do not tightly specify the representations for the primitive types.)
Stephen C