1 3 package jodd.util; 4 5 8 public class MathUtil { 9 10 20 21 public static long randomLong(long min, long max) { 22 return min + (long)(Math.random() * (max - min)); 23 } 24 25 26 36 public static int randomInt(int min, int max) { 37 return min + (int)(Math.random() * (max - min)); 38 } 39 40 41 43 51 public static int compare(double lhs, double rhs) { 52 if (lhs < rhs) { 53 return -1; 54 } 55 if (lhs > rhs) { 56 return +1; 57 } 58 long lhsBits = Double.doubleToLongBits(lhs); 59 long rhsBits = Double.doubleToLongBits(rhs); 60 if (lhsBits == rhsBits) { 61 return 0; 62 } 63 if (lhsBits < rhsBits) { 64 return -1; 65 } else { 66 return +1; 67 } 68 } 69 70 78 public static int compare(float lhs, float rhs) { 79 if (lhs < rhs) { 80 return -1; 81 } 82 if (lhs > rhs) { 83 return +1; 84 } 85 int lhsBits = Float.floatToIntBits(lhs); 86 int rhsBits = Float.floatToIntBits(rhs); 87 if (lhsBits == rhsBits) { 88 return 0; 89 } 90 if (lhsBits < rhsBits) { 91 return -1; 92 } else { 93 return +1; 94 } 95 } 96 97 } 98 | Popular Tags |