The IEEE 754 format has one bit reserved for the sign and the remaining bits representing the magnitude. This means that it is "symmetrical" around origo (as opposed to the Integer values, which have one more negative value). Thus the minimum value is simply the same as the maximum value, with the sign-bit flipped, so yes, -Double.MAX_VALUE is the lowest actual number you can represent with a double.
I suppose the Double.MAX_VALUE should be seen as maximum magnitude, in which case it actually makes sense to simply write -Double.MAX_VALUE. It also explains why Double.MIN_VALUE is the least positive value (since that represents the least possible magnitude).
But sure, I agree that the naming is a bit misleading. Being used to the meaning Integer.MIN_VALUE, I too was a bit surprised when I read that Double.MIN_VALUE was the smallest absolute value that could be represented. Perhaps they thought it was superfluous to have a constant representing the least possible value as it is simply a - away from MAX_VALUE :-)
(Note, there is also Double.NEGATIVE_INFINITY but I'm disregarding from this, as it is to be seen as a "special case" and does not in fact represent any actual number.)
Here is a good text on the subject.
Answer from aioobe on Stack OverflowThe IEEE 754 format has one bit reserved for the sign and the remaining bits representing the magnitude. This means that it is "symmetrical" around origo (as opposed to the Integer values, which have one more negative value). Thus the minimum value is simply the same as the maximum value, with the sign-bit flipped, so yes, -Double.MAX_VALUE is the lowest actual number you can represent with a double.
I suppose the Double.MAX_VALUE should be seen as maximum magnitude, in which case it actually makes sense to simply write -Double.MAX_VALUE. It also explains why Double.MIN_VALUE is the least positive value (since that represents the least possible magnitude).
But sure, I agree that the naming is a bit misleading. Being used to the meaning Integer.MIN_VALUE, I too was a bit surprised when I read that Double.MIN_VALUE was the smallest absolute value that could be represented. Perhaps they thought it was superfluous to have a constant representing the least possible value as it is simply a - away from MAX_VALUE :-)
(Note, there is also Double.NEGATIVE_INFINITY but I'm disregarding from this, as it is to be seen as a "special case" and does not in fact represent any actual number.)
Here is a good text on the subject.
These constants have nothing to do with sign. This makes more sense if you consider a double as a composite of three parts: Sign, Exponent and Mantissa. Double.MIN_VALUE is actually the smallest value Mantissa can assume when the Exponent is at minimun value before a flush to zero occurs. Likewise MAX_VALUE can be understood as the largest value Mantissa can assume when the Exponent is at maximum value before a flush to infinity occurs.
A more descriptive name for these two could be Largest Absolute (add non-zero for verbositiy) and Smallest Absolute value (add non-infinity for verbositiy).
Check out the IEEE 754 (1985) standard for details. There is a revised (2008) version, but that only introduces more formats which aren't even supported by java (strictly speaking java even lacks support for some mandatory features of IEEE 754 1985, like many other high level languages).
Try changing this:
double max = Double.MAX_VALUE;
double min = Double.MIN_VALUE;
to this:
double max = -Double.MAX_VALUE;
double min = Double.MAX_VALUE;
After all, if you start off thinking you've already seen MAX_VALUE as the highest, you're never going to see anything greater than that, are you?
EDIT: Note the use of -Double.MAX_VALUE here, instead of Double.MIN_VALUE. MIN_VALUE is the smallest positive number, whereas I assume you really want "the most strongly negative finite value".
You want the first value you read to replace both max and min. An alternative would be to use Double instead, to represent the "missing" value with null to start with:
Double max = null;
Double min = null;
if (max == null || mag > max)
{
max = mag;
}
// Ditto for min
As a style issue, I'd also only compute the average after you've read the whole of the input - you're repeatedly overwriting the value for no reason. The only reason not to do this is to avoid having to think about the case where count is 0 (i.e. an empty file) but I'd personally handle that separately anyway - when you have no values, there simply isn't an average.
dont use Double.MIN_VALUE its not what you expect it to be
Double.MIN_VALUE its not a negative number Double.MIN_VALUE is a very small nummber its 4.9e-324; // 0x0.0000000000001P-1022
Comparing with it will break as soon as you use negative numbers
A small utility class i wrote to help with it:
public class MinMaxUtil<T extends Comparable<T>>
{
private T minimum;
private T maximum;
public MinMaxUtil()
{
reset();
}
public void check( T value)
{
if ( minimum == null || value.compareTo(minimum) < 0 )
minimum = value;
if ( maximum == null || value.compareTo(maximum) > 0 )
maximum = value;
}
public void check( T [] values )
{
for (T value : values)
{
check(value);
}
}
public void check( Collection<T> values )
{
for (T value : values)
{
check(value);
}
}
public T getMinimum()
{
return minimum;
}
public T getMaximum()
{
return maximum;
}
public void reset()
{
minimum = null;
maximum = null;
}
@Override
public String toString()
{
return "MinMaxUtil{"+
"minimum="+minimum+
", maximum="+maximum+
'}';
}
}