|
| 1 | +package org.tron.common.math; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.Objects; |
| 7 | + |
| 8 | +/** |
| 9 | + * This class is deprecated and should not be used in new code, |
| 10 | + * for cross-platform consistency, please use {@link StrictMathWrapper} instead, |
| 11 | + * especially for floating-point calculations. |
| 12 | + */ |
| 13 | +@Deprecated |
| 14 | +public class MathWrapper { |
| 15 | + |
| 16 | + private static final Map<PowData, Double> powData = Collections.synchronizedMap(new HashMap<>()); |
| 17 | + private static final String POW = "3f40624dd2f1a9fc"; // 1/2000 = 0.0005 |
| 18 | + |
| 19 | + public static double pow(double a, double b) { |
| 20 | + double strictResult = StrictMath.pow(a, b); |
| 21 | + return powData.getOrDefault(new PowData(a, b), strictResult); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * This static block is used to initialize the data map. |
| 26 | + */ |
| 27 | + static { |
| 28 | + addPowData("3ff0192278704be3", POW, "3ff000033518c576"); // 4137160 |
| 29 | + addPowData("3ff000002fc6a33f", POW, "3ff0000000061d86"); // 4065476 |
| 30 | + addPowData("3ff00314b1e73ecf", POW, "3ff0000064ea3ef8"); // 4071538 |
| 31 | + addPowData("3ff0068cd52978ae", POW, "3ff00000d676966c"); // 4109544 |
| 32 | + addPowData("3ff0032fda05447d", POW, "3ff0000068636fe0"); // 4123826 |
| 33 | + addPowData("3ff00051c09cc796", POW, "3ff000000a76c20e"); // 4166806 |
| 34 | + addPowData("3ff00bef8115b65d", POW, "3ff0000186893de0"); // 4225778 |
| 35 | + addPowData("3ff009b0b2616930", POW, "3ff000013d27849e"); // 4251796 |
| 36 | + addPowData("3ff00364ba163146", POW, "3ff000006f26a9dc"); // 4257157 |
| 37 | + addPowData("3ff019be4095d6ae", POW, "3ff0000348e9f02a"); // 4260583 |
| 38 | + addPowData("3ff0123e52985644", POW, "3ff0000254797fd0"); // 4367125 |
| 39 | + addPowData("3ff0126d052860e2", POW, "3ff000025a6cde26"); // 4402197 |
| 40 | + addPowData("3ff0001632cccf1b", POW, "3ff0000002d76406"); // 4405788 |
| 41 | + addPowData("3ff0000965922b01", POW, "3ff000000133e966"); // 4490332 |
| 42 | + addPowData("3ff00005c7692d61", POW, "3ff0000000bd5d34"); // 4499056 |
| 43 | + addPowData("3ff015cba20ec276", POW, "3ff00002c84cef0e"); // 4518035 |
| 44 | + addPowData("3ff00002f453d343", POW, "3ff000000060cf4e"); // 4533215 |
| 45 | + addPowData("3ff006ea73f88946", POW, "3ff00000e26d4ea2"); // 4647814 |
| 46 | + addPowData("3ff00a3632db72be", POW, "3ff000014e3382a6"); // 4766695 |
| 47 | + addPowData("3ff000c0e8df0274", POW, "3ff0000018b0aeb2"); // 4771494 |
| 48 | + addPowData("3ff00015c8f06afe", POW, "3ff0000002c9d73e"); // 4793587 |
| 49 | + addPowData("3ff00068def18101", POW, "3ff000000d6c3cac"); // 4801947 |
| 50 | + addPowData("3ff01349f3ac164b", POW, "3ff000027693328a"); // 4916843 |
| 51 | + addPowData("3ff00e86a7859088", POW, "3ff00001db256a52"); // 4924111 |
| 52 | + addPowData("3ff00000c2a51ab7", POW, "3ff000000018ea20"); // 5098864 |
| 53 | + addPowData("3ff020fb74e9f170", POW, "3ff00004346fbfa2"); // 5133963 |
| 54 | + addPowData("3ff00001ce277ce7", POW, "3ff00000003b27dc"); // 5139389 |
| 55 | + addPowData("3ff005468a327822", POW, "3ff00000acc20750"); // 5151258 |
| 56 | + addPowData("3ff00006666f30ff", POW, "3ff0000000d1b80e"); // 5185021 |
| 57 | + addPowData("3ff000045a0b2035", POW, "3ff00000008e98e6"); // 5295829 |
| 58 | + addPowData("3ff00e00380e10d7", POW, "3ff00001c9ff83c8"); // 5380897 |
| 59 | + addPowData("3ff00c15de2b0d5e", POW, "3ff000018b6eaab6"); // 5400886 |
| 60 | + addPowData("3ff00042afe6956a", POW, "3ff0000008892244"); // 5864127 |
| 61 | + addPowData("3ff0005b7357c2d4", POW, "3ff000000bb48572"); // 6167339 |
| 62 | + addPowData("3ff00033d5ab51c8", POW, "3ff0000006a279c8"); // 6240974 |
| 63 | + addPowData("3ff0000046d74585", POW, "3ff0000000091150"); // 6279093 |
| 64 | + addPowData("3ff0010403f34767", POW, "3ff0000021472146"); // 6428736 |
| 65 | + addPowData("3ff00496fe59bc98", POW, "3ff000009650a4ca"); // 6432355,6493373 |
| 66 | + addPowData("3ff0012e43815868", POW, "3ff0000026af266e"); // 6555029 |
| 67 | + addPowData("3ff00021f6080e3c", POW, "3ff000000458d16a"); // 7092933 |
| 68 | + addPowData("3ff000489c0f28bd", POW, "3ff00000094b3072"); // 7112412 |
| 69 | + addPowData("3ff00009d3df2e9c", POW, "3ff00000014207b4"); // 7675535 |
| 70 | + addPowData("3ff000def05fa9c8", POW, "3ff000001c887cdc"); // 7860324 |
| 71 | + addPowData("3ff0013bca543227", POW, "3ff00000286a42d2"); // 8292427 |
| 72 | + addPowData("3ff0021a2f14a0ee", POW, "3ff0000044deb040"); // 8517311 |
| 73 | + addPowData("3ff0002cc166be3c", POW, "3ff0000005ba841e"); // 8763101 |
| 74 | + addPowData("3ff0000cc84e613f", POW, "3ff0000001a2da46"); // 9269124 |
| 75 | + addPowData("3ff000057b83c83f", POW, "3ff0000000b3a640"); // 9631452 |
| 76 | + // add pow data |
| 77 | + } |
| 78 | + |
| 79 | + private static void addPowData(String a, String b, String ret) { |
| 80 | + powData.put(new PowData(hexToDouble(a), hexToDouble(b)), hexToDouble(ret)); |
| 81 | + } |
| 82 | + |
| 83 | + private static double hexToDouble(String input) { |
| 84 | + // Convert the hex string to a long |
| 85 | + long hexAsLong = Long.parseLong(input, 16); |
| 86 | + // and then convert the long to a double |
| 87 | + return Double.longBitsToDouble(hexAsLong); |
| 88 | + } |
| 89 | + |
| 90 | + private static class PowData { |
| 91 | + final double a; |
| 92 | + final double b; |
| 93 | + |
| 94 | + public PowData(double a, double b) { |
| 95 | + this.a = a; |
| 96 | + this.b = b; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public boolean equals(Object o) { |
| 101 | + if (this == o) { |
| 102 | + return true; |
| 103 | + } |
| 104 | + if (o == null || getClass() != o.getClass()) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + PowData powData = (PowData) o; |
| 108 | + return Double.compare(powData.a, a) == 0 && Double.compare(powData.b, b) == 0; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public int hashCode() { |
| 113 | + return Objects.hash(a, b); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * *** methods are same as {@link java.lang.Math} methods, guaranteed by the call start *** |
| 119 | + */ |
| 120 | + |
| 121 | + /** |
| 122 | + * finally calls {@link java.lang.Math#addExact(long, long)} |
| 123 | + */ |
| 124 | + |
| 125 | + public static long addExact(long x, long y) { |
| 126 | + return StrictMath.addExact(x, y); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * finally calls {@link java.lang.Math#addExact(int, int)} |
| 131 | + */ |
| 132 | + |
| 133 | + public static int addExact(int x, int y) { |
| 134 | + return StrictMath.addExact(x, y); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * finally calls {@link java.lang.Math#subtractExact(long, long)} |
| 139 | + */ |
| 140 | + |
| 141 | + public static long subtractExact(long x, long y) { |
| 142 | + return StrictMath.subtractExact(x, y); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * finally calls {@link java.lang.Math#floorMod(long, long)} |
| 147 | + */ |
| 148 | + public static long multiplyExact(long x, long y) { |
| 149 | + return StrictMath.multiplyExact(x, y); |
| 150 | + } |
| 151 | + |
| 152 | + public static long multiplyExact(long x, int y) { |
| 153 | + return multiplyExact(x, (long) y); |
| 154 | + } |
| 155 | + |
| 156 | + public static int multiplyExact(int x, int y) { |
| 157 | + return StrictMath.multiplyExact(x, y); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * finally calls {@link java.lang.Math#floorDiv(long, long)} |
| 162 | + */ |
| 163 | + public static long floorDiv(long x, long y) { |
| 164 | + return StrictMath.floorDiv(x, y); |
| 165 | + } |
| 166 | + |
| 167 | + public static long floorDiv(long x, int y) { |
| 168 | + return floorDiv(x, (long) y); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * finally calls {@link java.lang.Math#min(int, int)} |
| 173 | + */ |
| 174 | + public static int min(int a, int b) { |
| 175 | + return StrictMath.min(a, b); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * finally calls {@link java.lang.Math#min(long, long)} |
| 180 | + */ |
| 181 | + public static long min(long a, long b) { |
| 182 | + return StrictMath.min(a, b); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * finally calls {@link java.lang.Math#max(int, int)} |
| 187 | + */ |
| 188 | + public static int max(int a, int b) { |
| 189 | + return StrictMath.max(a, b); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * finally calls {@link java.lang.Math#max(long, long)} |
| 194 | + */ |
| 195 | + public static long max(long a, long b) { |
| 196 | + return StrictMath.max(a, b); |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * finally calls {@link java.lang.Math#round(float)} |
| 201 | + */ |
| 202 | + public static int round(float a) { |
| 203 | + return StrictMath.round(a); |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * finally calls {@link java.lang.Math#round(double)} |
| 208 | + */ |
| 209 | + public static long round(double a) { |
| 210 | + return StrictMath.round(a); |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * finally calls {@link java.lang.Math#signum(double)} |
| 215 | + */ |
| 216 | + public static double signum(double d) { |
| 217 | + return StrictMath.signum(d); |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * finally calls {@link java.lang.Math#signum(float)} |
| 222 | + */ |
| 223 | + public static long abs(long a) { |
| 224 | + return StrictMath.abs(a); |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * *** methods are same as {@link java.lang.Math} methods, guaranteed by the call end *** |
| 229 | + */ |
| 230 | + |
| 231 | + /** |
| 232 | + * *** methods are same as {@link java.lang.Math} methods by mathematical algorithms*** |
| 233 | + * / |
| 234 | +
|
| 235 | +
|
| 236 | + /** |
| 237 | + * mathematical integer: ceil(i) = floor(i) = i |
| 238 | + * @return the smallest (closest to negative infinity) double value that is greater |
| 239 | + * than or equal to the argument and is equal to a mathematical integer. |
| 240 | + */ |
| 241 | + public static double ceil(double a) { |
| 242 | + return StrictMath.ceil(a); |
| 243 | + } |
| 244 | + |
| 245 | + /** |
| 246 | + * *** methods are no matters *** |
| 247 | + */ |
| 248 | + public static double random() { |
| 249 | + return StrictMath.random(); |
| 250 | + } |
| 251 | + |
| 252 | +} |
0 commit comments