Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparator with double type

I have written the following code:

public class NewClass2 implements Comparator<Point> {     public int compare(Point p1, Point p2)     {         return (int)(p1.getY() - p2.getY());     } } 

If I let's say have two double numbers, 3.2 - 3.1, the difference should be 0.1. When I cast the number to an int, however, the difference ends up as 0, which is not correct.

I therefore need compare() to return a double, not an int. The problem is, my getX field is a double. How can I solve this problem?

like image 554
user472221 Avatar asked Nov 22 '10 03:11

user472221


People also ask

How do you compare two double values?

The compare() method of Double Class is a built-in method in Java that compares the two specified double values. The sign of the integer value returned is the same as that of the integer that would be returned by the function call.

Can you compare doubles with ==?

Using the == Operator As a result, we can't have an exact representation of most double values in our computers. They must be rounded to be saved. In that case, comparing both values with the == operator would produce a wrong result.

Can we compare int and double in Java?

It is valid, but you may get interesting results in edge cases if you don't specify a precision on the double... With the caveat from @PinnyM, I should point out that converting int to double is lossless, but promoting long to double is lossy.

What does double compare return?

CompareTo(Double) Compares this instance to a specified double-precision floating-point number and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the specified double-precision floating-point number.


2 Answers

I suggest you use the builtin method Double.compare(). If you need a range for double values to be equal you can use chcek for that first.

return Double.compare(p1.getY(), p2.gety()); 

or

if(Math.abs(p1.getY()-p2.getY()) < ERR) return 0;     return Double.compare(p1.getY(), p2.gety()); 

The problem with using < and > is that NaN will return false in both cases resulting in a possibly inconsistent handling. e.g. NaN is defined as not being equal to anything, even itself however in @suihock's and @Martinho's solutions, if either value is NaN the method will return 0 everytime, implying that NaN is equal to everything.

like image 117
Peter Lawrey Avatar answered Oct 09 '22 13:10

Peter Lawrey


You don't need to return double.

The Comparator interface is used to establish an ordering for the elements being compared. Having fields that use double is irrelevant to this ordering.

Your code is fine.

Sorry, I was wrong, reading the question again, this is what you need:

public class NewClass2 implements Comparator<Point> {     public int compare(Point p1, Point p2) {         if (p1.getY() < p2.getY()) return -1;         if (p1.getY() > p2.getY()) return 1;         return 0;     }     } 
like image 23
dteoh Avatar answered Oct 09 '22 12:10

dteoh