Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Math.max seems to be returning the wrong answer

Tags:

java

math

max

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?

like image 417
Reason Enough Avatar asked Sep 15 '10 12:09

Reason Enough


1 Answers

MIN_VALUE is:

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

Not the most negative possible value.

like image 143
sje397 Avatar answered Oct 17 '22 22:10

sje397