Float.MIN_VALUE < Integer.MIN_VALUE

P

Praveen

Hi..All,

A quick question.

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

Regards,

P
 
D

Dario

Praveen said:
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
 
J

Jacob

Praveen said:
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;
 
P

Praveen

Thanks alot for the response!!!

Another question.

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

Jacob

samspade said:
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;
}
 
C

Carsten Friedrich

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
 
D

Dale King

Praveen said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top