Float.MIN_VALUE < Integer.MIN_VALUE

Discussion in 'Java' started by Praveen, Aug 20, 2003.

  1. Praveen

    Praveen Guest

    Hi..All,

    A quick question.

    why is Float.MIN_VALUE < Integer.MIN_VALUE return false? Eventhough both
    are 32 bits.

    Regards,

    P
     
    Praveen, Aug 20, 2003
    #1
    1. Advertising

  2. Praveen

    Dario Guest

    Praveen wrote:

    > why is Float.MIN_VALUE < Integer.MIN_VALUE return false?


    Because the following Java program:

    class A52 {
    public static void main(String[]args) {
    System.out.println("Float.MIN_VALUE = "+Float.MIN_VALUE);
    System.out.println("Integer.MIN_VALUE = "+Integer.MIN_VALUE);
    }
    }

    outputs the following result:

    Float.MIN_VALUE = 1.4E-45
    Integer.MIN_VALUE = -2147483648

    A positive number is not less than a negative one!

    - Dario
     
    Dario, Aug 20, 2003
    #2
    1. Advertising

  3. Praveen

    Jacob Guest

    Praveen wrote:
    > Hi..All,
    >
    > A quick question.
    >
    > why is Float.MIN_VALUE < Integer.MIN_VALUE return false? Eventhough both
    > are 32 bits.


    The terms are actually quite misleading as
    they describe two different properties.
    Integer.MIN_VALUE is a huge negative number,
    while Float.MIN_VALUE is the smallest possible
    positive one.

    And it is a common source of error. In search
    algorithms where looking for smallest and largest
    number in a collection, I often initialize as
    follows:

    double minValue = Double.MAX_VALUE;
    double maxValue = Double.MIN_VALUE;

    The correct approach is of course:

    double minValue = +Double.MAX_VALUE;
    double maxValue = -Double.MAX_VALUE;
     
    Jacob, Aug 20, 2003
    #3
  4. Praveen

    samspade Guest

    > double minValue = +Double.MAX_VALUE;
    > double maxValue = -Double.MAX_VALUE;



    Should they be the other way around ?
     
    samspade, Aug 20, 2003
    #4
  5. Praveen

    Praveen Guest

    Thanks alot for the response!!!

    Another question.

    Why is the Float.MIN_VALUE not negative?
    where as Integer.MIN_VALUE is negative.

    samspade wrote:

    > > double minValue = +Double.MAX_VALUE;
    > > double maxValue = -Double.MAX_VALUE;

    >
    >
    > Should they be the other way around ?
    >
    >
    >
    >
    >
    >
    >
     
    Praveen, Aug 21, 2003
    #5
  6. Praveen

    Jacob Guest

    samspade wrote:
    > > double minValue = +Double.MAX_VALUE;
    > > double maxValue = -Double.MAX_VALUE;

    >
    >
    > Should they be the other way around ?



    Not in the context mentioned.

    If you look for the highest number in a
    collection a common pattern is:

    double maxValue = -Double.MAX_VALUE;
    while (!done) {
    double value = getNextValue();
    if (value > maxValue) maxValue = value;
    }
     
    Jacob, Aug 21, 2003
    #6
  7. >
    > The correct approach is of course:
    >
    > double minValue = +Double.MAX_VALUE;
    > double maxValue = -Double.MAX_VALUE;
    >


    I'm not sure if it is guaranteed that -Double.MAX_VALUE is still a valid
    double. I think the better approach is actually:

    double maxValue = Double.NEGATIVE_INFINITY;
    double minValue = Double.POSITIVE_INFINITY;

    or even:

    double minValue = Double.NaN;
    double maxValue = Double.NaN;

    Carsten
     
    Carsten Friedrich, Aug 22, 2003
    #7
  8. Praveen

    Dale King Guest

    "Praveen" <> wrote in message
    news:...
    > Thanks alot for the response!!!
    >
    > Another question.
    >
    > Why is the Float.MIN_VALUE not negative?
    > where as Integer.MIN_VALUE is negative.


    Because floating point is symmetric around zero. They essentially are a
    sign-magnitude representation. For every positive value there is also a
    negative value. Having Float.MIN_VALUE be the most negative value would kind
    of be a waste because that would be the same as -Float.MAX_VALUE. But it is
    important to know what is the closest you can get to zero without being zero
    is.

    The integers are not symmetric about zero. They use two's complement. There
    is 1 more negative value than positive value. Integer.MIN_VALUE = -
    Integer.MAX_VALUE - 1. The number closest to zero is of course 1, so there
    is no need to define a constant for it.
     
    Dale King, Aug 22, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy
    Replies:
    7
    Views:
    6,268
    Roedy Green
    May 10, 2004
  2. Kevin
    Replies:
    8
    Views:
    2,210
    Kevin
    Jul 28, 2005
  3. bd
    Replies:
    0
    Views:
    634
  4. 10godina

    Float.MIN_VALUE

    10godina, Feb 12, 2008, in forum: Java
    Replies:
    17
    Views:
    737
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,558
    James Kanze
    Oct 8, 2009
Loading...

Share This Page