-
Enhancement
-
Resolution: Fixed
-
P4
-
7
-
b74
-
x86
-
windows_xp
A DESCRIPTION OF THE REQUEST :
Add following 'static compare' methods:
// Integer.java
public static int compare(int a, int b) {
return (a < b)? -1: (a == b)? 0: 1;
}
// Long.java
public static int compare(long a, long b) {
return (a < b)? -1: (a == b)? 0: 1;
}
// Double.java
public static int compare(long a, double b) {
// precise comparison, i.e. 40000000000000001L should be greater then 40000000000000000.0 (with == operator they are equal)
// ...
}
public static int compare(double a, long b) {
// ... same as above
}
// Float.java
public static int compare(float f, int i) {
return Double.compare(f, (double) i); // or Double.compare(f, (long) i) or maybe something more efficient
}
public static int compare(int i, float f) {
return Double.compare((double) i, f); // or Double.compare((long) i, f) or maybe something more efficient
}
'static compare' methods could also be added to Byte, Short, Character and Boolean just for consistency.
JUSTIFICATION :
Absence of Integer.compare(int, int) and Long.compare(long, long) often leads to quite common bug pattern where substraction is used (substraction is wrong in general due to overflow).
Absence of Double.compare(long, double) often leads to using plain comparison operators (i.e. <, >, = etc), which is not wholly satisfactory due to precision loss.
Note: In order to be consistent with Double.compare(double, double) and Float.compare(float, float):
1. NaN should be greater then any integer number;
2. -0.0 should be less then integer zero.
CUSTOMER SUBMITTED WORKAROUND :
Workaround for Integer.compare(int, int) and Long.compare(long, long)
1. Using boxing
I.e.
((Integer) a).compareTo(b)
instead of
Integer.compare(a, b)
But it looks like less efficient way then proposed
2. Duplicate corrent comparison code everywhere it's needed
(a < b)? -1: (a == b)? 0: 1
Workaround for Double.compare(long, double)
1. Write own precise comparison code
Add following 'static compare' methods:
// Integer.java
public static int compare(int a, int b) {
return (a < b)? -1: (a == b)? 0: 1;
}
// Long.java
public static int compare(long a, long b) {
return (a < b)? -1: (a == b)? 0: 1;
}
// Double.java
public static int compare(long a, double b) {
// precise comparison, i.e. 40000000000000001L should be greater then 40000000000000000.0 (with == operator they are equal)
// ...
}
public static int compare(double a, long b) {
// ... same as above
}
// Float.java
public static int compare(float f, int i) {
return Double.compare(f, (double) i); // or Double.compare(f, (long) i) or maybe something more efficient
}
public static int compare(int i, float f) {
return Double.compare((double) i, f); // or Double.compare((long) i, f) or maybe something more efficient
}
'static compare' methods could also be added to Byte, Short, Character and Boolean just for consistency.
JUSTIFICATION :
Absence of Integer.compare(int, int) and Long.compare(long, long) often leads to quite common bug pattern where substraction is used (substraction is wrong in general due to overflow).
Absence of Double.compare(long, double) often leads to using plain comparison operators (i.e. <, >, = etc), which is not wholly satisfactory due to precision loss.
Note: In order to be consistent with Double.compare(double, double) and Float.compare(float, float):
1. NaN should be greater then any integer number;
2. -0.0 should be less then integer zero.
CUSTOMER SUBMITTED WORKAROUND :
Workaround for Integer.compare(int, int) and Long.compare(long, long)
1. Using boxing
I.e.
((Integer) a).compareTo(b)
instead of
Integer.compare(a, b)
But it looks like less efficient way then proposed
2. Duplicate corrent comparison code everywhere it's needed
(a < b)? -1: (a == b)? 0: 1
Workaround for Double.compare(long, double)
1. Write own precise comparison code
- relates to
-
JDK-6797535 Add shared two argument static equals method to the platform
- Resolved
-
JDK-6583551 compare numbers of different Number classes
- Closed