Is there any constant defined for infinity in java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • marvinp
    New Member
    • Feb 2007
    • 8

    Is there any constant defined for infinity in java

    I am writing a class which contain a variable INFINITY. Is there any constant defined for this in java so that i can assign that value?

    In .NET, there is System.Threadin g.Timeout.Infin ite for infinity. I am looking for the same in java also.
  • hirak1984
    Contributor
    • Jan 2007
    • 316

    #2
    No idea , eager to know if there is any!!!

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      What do you need infinity for?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        For the numeric wrapper types.

        e.g Double.POSITVE_ INFINITY

        Comment

        • marvinp
          New Member
          • Feb 2007
          • 8

          #5
          Originally posted by Ganon11
          What do you need infinity for?
          I have a function WaitOnMultipleE vents( long lMilliseconds ) in the class. So the parameter value is set to m_lMilliseconds member variable which means how much time it has to wait.

          when lMilliseconds is less than 0, i want to set the m_lMilliseconds to infinity. So what value should i assign for it.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by marvinp
            I have a function WaitOnMultipleE vents( long lMilliseconds ) in the class. So the parameter value is set to m_lMilliseconds member variable which means how much time it has to wait.

            when lMilliseconds is less than 0, i want to set the m_lMilliseconds to infinity. So what value should i assign for it.
            lMilliseconds is a long. The largest value it can take is the largest value for the long data type which is 2^(63) - 1

            Comment

            • marvinp
              New Member
              • Feb 2007
              • 8

              #7
              Originally posted by r035198x
              lMilliseconds is a long. The largest value it can take is the largest value for the long data type which is 2^(63) - 1

              i mean, i want to assign m_lmilliSeconds a value which means it is infinity. Like, i can assign -1 to represent it is infinity. But instead of that juz wanted to know if any constant value is already defined in java for this like what is there in .NET ( System.Threadin g.Timeout.Infin ity ).

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by marvinp
                i mean, i want to assign m_lmilliSeconds a value which means it is infinity. Like, i can assign -1 to represent it is infinity. But instead of that juz wanted to know if any constant value is already defined in java for this like what is there in .NET ( System.Threadin g.Timeout.Infin ity ).
                You can only assign a value that is withing the range of the type of the variable. You cannot store a number greater than 2^(63) -1 in a variable of type long. Remember that infinity itself is not a number. You can only find a very large number to represent it. That is what .NET or any other languages do. In Java there is an infinities for each of the numeric types. If you need a number larger than the numeric types you can use either BigInteger or BigDecimal.

                Comment

                • RedSon
                  Recognized Expert Expert
                  • Jan 2007
                  • 4980

                  #9
                  Originally posted by marvinp
                  I have a function WaitOnMultipleE vents( long lMilliseconds ) in the class. So the parameter value is set to m_lMilliseconds member variable which means how much time it has to wait.

                  when lMilliseconds is less than 0, i want to set the m_lMilliseconds to infinity. So what value should i assign for it.

                  Did you write the method WaitOnMultipleE vents? That sounds like a standard named events method in win32 programming. For that method you usually need two things, a collection of the events to wait on and the maximum amount of time you want it to wait for. If you make the maximum amount of time to wait zero then usually the method interprets that as wait forever, since it doesnt make much sense to wait for 0 milliseconds or it would never wait, which is why it would be pointless to call the method anyway.

                  I say "usually" because this is the java forum and I have never heard of a java class the emulates the named event processing found in win32. If you have written it yourself then just change it to mean that zero is equal to infinity and there you have it.

                  Comment

                  • clincks
                    New Member
                    • May 2010
                    • 1

                    #10
                    reply to post #8: Not fully true...

                    Check this code:
                    f
                    Code:
                    or(double j=1; ;j++) {
                    	double temp = Double.MAX_VALUE - j;
                    	if(temp != Double.MAX_VALUE) {
                    		break;
                    	}
                    }
                    When do you think you will leave the loop?

                    Double is trying to implement the mathematical infinite representation.

                    My 2 cents.

                    Comment

                    • DLittleModifier
                      New Member
                      • Nov 2011
                      • 1

                      #11
                      I also want to know how to use infinity. Reason why I want to use it is, is because there is a formula I made on paper that won't work unless the difference between two variables is an integer. So sort of how I want to use it

                      int soandso=(value for infinity);
                      float x=in.nextFloat( );
                      float n=in.nextFloat
                      int abx=0;
                      int abn=0;
                      if (x<0){
                      abx=(0-x);
                      }
                      if (x>=0){
                      abx=x;
                      }
                      if (n<0){
                      abn=(0-n);
                      }
                      if (n>=0){
                      abn=n;
                      }
                      int dif=abx-abn;
                      while (dif!=soandso){
                      n=in.nextFloat( );
                      if (x<0){
                      abx=(0-x);
                      }
                      if (x>=0){
                      abx=x;
                      }
                      if (n<0){
                      abn=(0-n);
                      }
                      if (n>=0){
                      abn=n;
                      }
                      dif=abx-abn;
                      }

                      this would make it so the user would have to continue reentering a value for n until the difference between abx and abn is an interger reason why it needs to absolute value is because there is such thing as something like .64-(-.36)=1

                      Comment

                      • Rajani K
                        New Member
                        • Nov 2011
                        • 1

                        #12
                        no. their is no constant/keyword like Infinity in java. Still you get a result Infinity when you work with floating point literals

                        "Infinity" is produced if a floating point operation creates such a large floating-point number that it cannot be represented normally. "Infinity" is a special value that represent the concept of positive infinity.

                        1.0 / 0.0 -> Infinity

                        Comment

                        • kuldeeprsks
                          New Member
                          • Dec 2011
                          • 22

                          #13
                          Instead of infinity try using the max value of the datatype.

                          Comment

                          • edk2
                            New Member
                            • Sep 2012
                            • 1

                            #14
                            According to Javadocs, as far as wait() is concerned, 0 == infinity...



                            "The specified amount of real time has elapsed, more or less. If timeout is zero, however, then real time is not taken into consideration and the thread simply waits until notified."

                            Comment

                            Working...