tags:

views:

102

answers:

3

I have a list of double values that I don't know the range of and I want to find the maximum value. However, the Math.max function is giving a curious result for this sample code:

double a = -100.0;
double maxA = Double.MIN_VALUE;
maxA = Math.max(maxA, a);
System.out.println(maxA);

And the output is:

4.9E-324

So for some reason, Double.MIN_VALUE is being considered the max when compared to -100.0.

Why?

+7  A: 

MIN_VALUE is:

A constant holding the smallest positive nonzero value of type double, 2^(-1074).

Not the most negative possible value.

sje397
Ah yes, thanks! I didn't even notice it was the smallest positive nonzero value. I just assumed it would actually be the smallest negative value.
Reason Enough
The problem with this definition is that it is inconsistent with the integer types, where MIN_VALUE is indeed the smallest possible value.
starblue
+1  A: 

Its pretty obvious -100 is less than 4.9E-324

-100 , -99 ..... -1 , 0 , 4.9E-324 , 1 , 2 , 3......
org.life.java
Only if you know scientific notation.
mizipzor
well if you don't, now is a super time to learn
jk
A: 

MIN_VALUE A constant holding the smallest positive nonzero value of type double.

Note "positive" value.

You are comparing it with a negative value is 1 > -1.

Jaydee