Velocity Reviews > Java > Float.MIN_VALUE < Integer.MIN_VALUE

# Float.MIN_VALUE < Integer.MIN_VALUE

Praveen
Guest
Posts: n/a

 08-20-2003
Hi..All,

A quick question.

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

Regards,

P

Dario
Guest
Posts: n/a

 08-20-2003
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

Jacob
Guest
Posts: n/a

 08-20-2003
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;

Guest
Posts: n/a

 08-20-2003
> double minValue = +Double.MAX_VALUE;
> double maxValue = -Double.MAX_VALUE;

Should they be the other way around ?

Praveen
Guest
Posts: n/a

 08-21-2003
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 ?
>
>
>
>
>
>
>

Jacob
Guest
Posts: n/a

 08-21-2003
> > 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;
}

Carsten Friedrich
Guest
Posts: n/a

 08-22-2003
>
> 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

Dale King
Guest
Posts: n/a

 08-22-2003
"Praveen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.