How to implement infinity in Java?

395,366

Solution 1

double supports Infinity

double inf = Double.POSITIVE_INFINITY;
System.out.println(inf + 5);
System.out.println(inf - inf); // same as Double.NaN
System.out.println(inf * -1); // same as Double.NEGATIVE_INFINITY

prints

Infinity
NaN
-Infinity

note: Infinity - Infinity is Not A Number.

Solution 2

I'm supposing you're using integer math for a reason. If so, you can get a result that's functionally nearly the same as POSITIVE_INFINITY by using the MAX_VALUE field of the Integer class:

Integer myInf = Integer.MAX_VALUE;

(And for NEGATIVE_INFINITY you could use MIN_VALUE.) There will of course be some functional differences, e.g., when comparing myInf to a value that happens to be MAX_VALUE: clearly this number isn't less than myInf. Also, as noted in the comments below, incrementing positive infinity will wrap you back around to negative numbers (and decrementing negative infinity will wrap you back to positive).

There's also a library that actually has fields POSITIVE_INFINITY and NEGATIVE_INFINITY, but they are really just new names for MAX_VALUE and MIN_VALUE.

Solution 3

To use Infinity, you can use Double which supports Infinity: -

    System.out.println(Double.POSITIVE_INFINITY);
    System.out.println(Double.POSITIVE_INFINITY * -1);
    System.out.println(Double.NEGATIVE_INFINITY);

    System.out.println(Double.POSITIVE_INFINITY - Double.NEGATIVE_INFINITY);
    System.out.println(Double.POSITIVE_INFINITY - Double.POSITIVE_INFINITY);

OUTPUT: -

Infinity
-Infinity
-Infinity

Infinity 
NaN

Solution 4

The Double and Float types have the POSITIVE_INFINITY constant.

Solution 5

Integer Infinity :

  Integer maxNumber = Integer.MAX_VALUE

Double Infinity

  Double maxNumber = Double.MAX_VALUE;
  Double positiveInf = Double.POSITIVE_INFINITY;
  Double negativeInf = Double.NEGATIVE_INFINITY

Float infinity

   Float positiveInf = Float.POSITIVE_INFINITY;
   Float negativeInf = Float.NEGATIVE_INFINITY
   Float maxNumber = Float.MAX_VALUE;
Share:
395,366

Related videos on Youtube

user1753100
Author by

user1753100

Updated on April 08, 2022

Comments

  • user1753100
    user1753100 about 2 years

    Does Java have anything to represent infinity for every numerical data type? How is it implemented such that I can do mathematical operations with it?

    E.g.

    int myInf = infinity; //However it is done
    myInf + 5; //returns infinity
    myInf*(-1); //returns negative infinity
    

    I have tried using very large numbers, but I want a proper, easy solution.

    • Dave Richardson
      Dave Richardson over 11 years
      there are an infinite number of infinities, which one would you like to model?
    • brimborium
      brimborium over 11 years
      Why should ∞-∞==0 be true? And also: Why do you need such a thing?
  • Vishy
    Vishy over 11 years
    I avoid using float whenever possible as its precision is pretty poor. ;)
  • Tudor
    Tudor over 11 years
    @user1753100: By default no, but some libraries, like this one: jscience.org implement it apparently.
  • Patrick Brinich-Langlois
    Patrick Brinich-Langlois about 11 years
    It seems arbitrary to restrict infinite values to Doubles and Floats. Their maximum values are closer to infinity than the maximum value of Integers, but not much closer.
  • David Morris
    David Morris over 10 years
    @PatrickBrinich-Langlois floating-point types (such as double and float) are typically capable of directly expressing infinity (i.e., there is a bit pattern that specifically means 'infinity', distinct from the maximum value of the type). Double and Float have MAX_VALUE, in common with Integer.
  • Joey Carson
    Joey Carson over 10 years
    Implementing algorithms like Dijkstra make me question whether or not POSITIVE_INFINITY < POSITIVE_INFINITY.
  • Erwin Smout
    Erwin Smout almost 9 years
    How much is Integer.MAX_VALUE + 5 ?
  • Erick G. Hagstrom
    Erick G. Hagstrom almost 9 years
    Integer.MAX_VALUE + 5 wraps around into the negative integers. Integer.MAX_VALUE + 5 = Integer.MIN_VALUE + 4 = -2147483644.
  • Erick G. Hagstrom
    Erick G. Hagstrom almost 9 years
    Not for all of the numeric wrapper types. Just for Double and Float.
  • carlsb3rg
    carlsb3rg about 8 years
    "Their maximum values are closer to infinity than the maximum value of Integers, but not much closer.". Any finite number is infinity away from infinity ;)
  • blubberdiblub
    blubberdiblub over 7 years
    It may be worthwhile to use an extra enum field in order to represent the additional states, so you can have negative Infinity as well, which is often desirable and makes -(yourvalue) work properly. And that would also allow you to support the NaN (not a number) concept. Apart from that, adding special values on top of integral types can be a good idea, particularly when the application needs semantics that are violated by floating point numbers.
  • ahitt6345
    ahitt6345 almost 7 years
    Whats the difference between using Integer.MAX_VALUE as infinity rather than Double.POSITIVE_INFINITY you said that they are 'functionally nearly the same', so what is the difference?
  • mgthomas99
    mgthomas99 almost 7 years
    @ahitt6345 Integer.MAX_VALUE is still finite, its just a hack for mimicking infinity. Furthermore, Integer.MAX_VALUE is only 32-bits whereas Double.POSITIVE_INFINITY is 64-bits.
  • JohnK
    JohnK almost 7 years
    Thanks, @GeorgeThomas. Yep, using any finite value to represent infinity is a hack. But until there's a way to represent infinity in Integers, what's the alternative in Integers?
  • refaelio
    refaelio about 5 years
    Integer.MAX_VALUE is a valid number which can be used in your input. op asked for infinity, which is NOT a number, but a mathematical symbol.
  • JohnK
    JohnK about 5 years
    @yarden.refaeli, hence the reason I said "functionally nearly the same" and also explained when it wouldn't work.
  • Lan Vukušič
    Lan Vukušič over 3 years
    Yea that is sometimes useful, but in cases where you update this value (especially increase it) you have to be extra cautious since it can cause an overflow and leave you with a very small value instead of a really big one!
  • Craig.C
    Craig.C about 3 years
    I think Integer.MAX_VALUE an achievable value, so it can be matched by a representable number of digits. This does not always matter as a larger figure is not possible as +1 would overflow to Integer.MIN_VALUE. Never the less, it's not quite the same as Infinity?
  • Craig.C
    Craig.C about 3 years
    However, you can use MAX_VALUE in many situations like a merge sort were it can be used as a "sentinel value". If for example, you wanted to return the smallest of a pair and you happened to compare the sentinel with the value 2147483647 occurring in your list. It doesn't matter which you return.
  • Craig.C
    Craig.C about 3 years
    The only problem being if you have the right combination of MAX_VALUEs in your input you might iterate beyond the index of your array. At which point you might as well drop the sentinel and just check the length of the list.