1 9 package javolution.lang; 10 11 import java.util.Random ; 12 13 23 public final class MathLib { 24 25 28 private MathLib() { 29 } 30 31 39 public static int random(int min, int max) { 40 int next = RANDOM.nextInt(); 41 if ((next >= min) && (next <= max)) 42 return next; 43 next += Integer.MIN_VALUE; 44 if ((next >= min) && (next <= max)) 45 return next; 46 final int interval = 1 + max - min; if (interval <= 0) 50 throw new Error ("Interval error"); return MathLib.abs(next % interval) + min; 52 } 53 54 private static final Random RANDOM = new Random (); 55 56 64 public static long random(long min, long max) { 65 long next = RANDOM.nextLong(); 66 if ((next >= min) && (next <= max)) 67 return next; 68 next += Long.MIN_VALUE; 69 if ((next >= min) && (next <= max)) 70 return next; 71 final long interval = 1L + max - min; if (interval <= 0) 75 throw new Error ("Interval error"); return MathLib.abs(next % interval) + min; 77 } 78 79 91 92 106 107 118 public static int bitLength(int i) { 119 if (i < 0) 120 i = -++i; 121 return (i < 1 << 16) ? (i < 1 << 8) ? BIT_LENGTH[i] 122 : BIT_LENGTH[i >>> 8] + 8 123 : (i < 1 << 24) ? BIT_LENGTH[i >>> 16] + 16 124 : BIT_LENGTH[i >>> 24] + 24; 125 } 126 127 private static final byte[] BIT_LENGTH = { 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 128 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 129 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 130 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 131 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 132 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 133 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 134 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 135 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 136 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 137 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 138 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 139 8, 8, 8 }; 140 141 152 public static int bitLength(long l) { 153 int i = (int) (l >> 32); 154 if (i > 0) 155 return (i < 1 << 16) ? (i < 1 << 8) ? BIT_LENGTH[i] + 32 156 : BIT_LENGTH[i >>> 8] + 40 157 : (i < 1 << 24) ? BIT_LENGTH[i >>> 16] + 48 158 : BIT_LENGTH[i >>> 24] + 56; 159 if (i < 0) 160 return bitLength(-++l); 161 i = (int) l; 162 return (i < 0) ? 32 : (i < 1 << 16) ? (i < 1 << 8) ? BIT_LENGTH[i] 163 : BIT_LENGTH[i >>> 8] + 8 164 : (i < 1 << 24) ? BIT_LENGTH[i >>> 16] + 16 165 : BIT_LENGTH[i >>> 24] + 24; 166 } 167 168 179 public static int digitLength(int i) { 180 if (i < 0) 181 i = -++i; 182 return (i >= 100000) ? (i >= 10000000) ? (i >= 1000000000) ? 10 183 : (i >= 100000000) ? 9 : 8 : (i >= 1000000) ? 7 : 6 184 : (i >= 100) ? (i >= 10000) ? 5 : (i >= 1000) ? 4 : 3 185 : (i >= 10) ? 2 : (i >= 1) ? 1 : 0; 186 } 187 188 199 public static int digitLength(long l) { 200 if (l < 0) 201 l = -++l; 202 if (l <= Integer.MAX_VALUE) { 203 int i = (int) l; 204 return (i >= 100000) ? (i >= 10000000) ? (i >= 1000000000) ? 10 205 : (i >= 100000000) ? 9 : 8 : (i >= 1000000) ? 7 : 6 206 : (i >= 100) ? (i >= 10000) ? 5 : (i >= 1000) ? 4 : 3 207 : (i >= 10) ? 2 : (i >= 1) ? 1 : 0; 208 } 209 return (l >= 100000000000000L) ? (l >= 10000000000000000L) ? (l >= 1000000000000000000L) ? 19 211 : (l >= 100000000000000000L) ? 18 : 17 212 : (l >= 1000000000000000L) ? 16 : 15 213 : (l >= 100000000000L) ? (l >= 10000000000000L) ? 14 214 : (l >= 1000000000000L) ? 13 : 12 215 : (l >= 10000000000L) ? 11 : 10; 216 217 } 218 219 255 256 377 378 409 410 528 529 548 549 569 570 626 627 637 638 648 649 659 660 676 677 689 690 702 703 713 714 724 725 735 736 750 751 762 763 776 777 801 802 812 813 823 824 834 835 847 848 859 860 871 872 882 885 891 892 902 903 914 915 926 927 933 public static int abs(int i) { 934 return (i < 0) ? -i : i; 935 } 936 937 943 public static long abs(long l) { 944 return (l < 0) ? -l : l; 945 } 946 947 957 958 968 969 976 public static int max(int x, int y) { 977 return (x >= y) ? x : y; 978 } 979 980 987 public static long max(long x, long y) { 988 return (x >= y) ? x : y; 989 } 990 991 1002 1003 1014 1015 1022 public static int min(int x, int y) { 1023 return (x < y) ? x : y; 1024 } 1025 1026 1033 public static long min(long x, long y) { 1034 return (x < y) ? x : y; 1035 } 1036 1037 1048 1049 1060 1061 1063 1074 1075 1171 1172 1174 1184 1185 1303 1304 1306 1315 1316 1454 1455} | Popular Tags |