Float.MIN_VALUE < Integer.MIN_VALUE

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

1. PraveenGuest

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

2. DarioGuest

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

3. JacobGuest

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

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

Should they be the other way around ?

5. PraveenGuest

Thanks alot for the response!!!

Another question.

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

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

>
>
> Should they be the other way around ?
>
>
>
>
>
>
>

Praveen, Aug 21, 2003
6. JacobGuest

> > 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
7. Carsten FriedrichGuest

>
> 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
8. Dale KingGuest

"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