I'm implementing Comparator for a class and while overriding the compare , i have a question.
double data1 = GetValueOf(Data1);
double data2 = GetValueOf(Data2);
int compareResult = Double.compare(data1, data2);
What happens if data1 and/or data2 are NaN?
Is this still a valid comparison to perform?
UPDATE I don't understand why this question is being down rated. Just to explain myself, I'm facing a " Comparison method violates general contract " problem.
I'm doing this...
if(Double.isNaN(data1)) data1 = Double.Positive_Infinity;
if(Double.isNaN(data2)) data2 = Double.Positive_Infinity;
before the Double.compare but still getting this error.
So, I need to understand if there's some basic thing I'm not understanding.
An unordered result is result is returned if you are comparing NAN with itself. The Java document says:
An operation that overflows produces a signed infinity, an operation that underflows produces a denormalized value or a signed zero, and an operation that has no mathematically definite result produces NaN. All numeric operations with NaN as an operand produce NaN as a result. As has already been described, NaN is unordered, so a numeric comparison operation involving one or two NaNs returns false and any != comparison involving NaN returns true, including x!=x when x is NaN.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.