Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 16 | |
| 17 | /** |
| 18 | * Test arithmetic operations. |
| 19 | */ |
| 20 | public class FloatMath { |
| 21 | |
| 22 | static void convTest() { |
| 23 | System.out.println("FloatMath.convTest"); |
| 24 | |
| 25 | float f; |
| 26 | double d; |
| 27 | int i; |
| 28 | long l; |
| 29 | |
| 30 | /* float --> int */ |
| 31 | f = 1234.5678f; |
| 32 | i = (int) f; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 33 | Main.assertTrue(i == 1234); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 34 | |
| 35 | f = -1234.5678f; |
| 36 | i = (int) f; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 37 | Main.assertTrue(i == -1234); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 38 | |
| 39 | /* float --> long */ |
| 40 | f = 1238.5678f; |
| 41 | l = (long) f; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 42 | Main.assertTrue(l == 1238); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 43 | |
| 44 | f = -1238.5678f; |
| 45 | l = (long) f; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 46 | Main.assertTrue(l == -1238); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 47 | |
| 48 | /* float --> double */ |
| 49 | f = 1238.5678f; |
| 50 | d = (double) f; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 51 | Main.assertTrue(d > 1238.567 && d < 1238.568); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 52 | |
| 53 | /* double --> int */ |
| 54 | d = 1234.5678; |
| 55 | i = (int) d; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 56 | Main.assertTrue(i == 1234); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 57 | |
| 58 | d = -1234.5678; |
| 59 | i = (int) d; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 60 | Main.assertTrue(i == -1234); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 61 | |
| 62 | /* double --> long */ |
| 63 | d = 5678956789.0123; |
| 64 | l = (long) d; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 65 | Main.assertTrue(l == 5678956789L); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 66 | |
| 67 | d = -5678956789.0123; |
| 68 | l = (long) d; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 69 | Main.assertTrue(l == -5678956789L); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 70 | |
| 71 | /* double --> float */ |
| 72 | d = 1238.5678; |
| 73 | f = (float) d; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 74 | Main.assertTrue(f > 1238.567 && f < 1238.568); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 75 | |
| 76 | /* int --> long */ |
| 77 | i = 7654; |
| 78 | l = (long) i; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 79 | Main.assertTrue(l == 7654L); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 80 | |
| 81 | i = -7654; |
| 82 | l = (long) i; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 83 | Main.assertTrue(l == -7654L); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 84 | |
| 85 | /* int --> float */ |
| 86 | i = 1234; |
| 87 | f = (float) i; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 88 | Main.assertTrue(f > 1233.9f && f < 1234.1f); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 89 | |
| 90 | i = -1234; |
| 91 | f = (float) i; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 92 | Main.assertTrue(f < -1233.9f && f > -1234.1f); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 93 | |
| 94 | /* int --> double */ |
| 95 | i = 1238; |
| 96 | d = (double) i; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 97 | Main.assertTrue(d > 1237.9f && d < 1238.1f); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 98 | |
| 99 | i = -1238; |
| 100 | d = (double) i; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 101 | Main.assertTrue(d < -1237.9f && d > -1238.1f); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 102 | |
| 103 | /* long --> int (with truncation) */ |
| 104 | l = 5678956789L; |
| 105 | i = (int) l; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 106 | Main.assertTrue(i == 1383989493); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 107 | |
| 108 | l = -5678956789L; |
| 109 | i = (int) l; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 110 | Main.assertTrue(i == -1383989493); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 111 | |
| 112 | /* long --> float */ |
| 113 | l = 5678956789L; |
| 114 | f = (float) l; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 115 | Main.assertTrue(f > 5.6789564E9 && f < 5.6789566E9); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 116 | |
| 117 | l = -5678956789L; |
| 118 | f = (float) l; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 119 | Main.assertTrue(f < -5.6789564E9 && f > -5.6789566E9); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 120 | |
| 121 | /* long --> double */ |
| 122 | l = 6678956789L; |
| 123 | d = (double) l; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 124 | Main.assertTrue(d > 6.6789567E9 && d < 6.6789568E9); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 125 | |
| 126 | l = -6678956789L; |
| 127 | d = (double) l; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 128 | Main.assertTrue(d < -6.6789567E9 && d > -6.6789568E9); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* |
| 132 | * We pass in the arguments and return the results so the compiler |
| 133 | * doesn't do the math for us. |
| 134 | */ |
| 135 | static float[] floatOperTest(float x, float y) { |
| 136 | System.out.println("FloatMath.floatOperTest"); |
| 137 | |
| 138 | float[] results = new float[9]; |
| 139 | |
| 140 | /* this seems to generate "op-float" instructions */ |
| 141 | results[0] = x + y; |
| 142 | results[1] = x - y; |
| 143 | results[2] = x * y; |
| 144 | results[3] = x / y; |
| 145 | results[4] = x % -y; |
| 146 | |
| 147 | /* this seems to generate "op-float/2addr" instructions */ |
| 148 | results[8] = x + (((((x + y) - y) * y) / y) % y); |
| 149 | |
| 150 | return results; |
| 151 | } |
| 152 | static void floatOperCheck(float[] results) { |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 153 | Main.assertTrue(results[0] > 69996.99f && results[0] < 69997.01f); |
| 154 | Main.assertTrue(results[1] > 70002.99f && results[1] < 70003.01f); |
| 155 | Main.assertTrue(results[2] > -210000.01f && results[2] < -209999.99f); |
| 156 | Main.assertTrue(results[3] > -23333.34f && results[3] < -23333.32f); |
| 157 | Main.assertTrue(results[4] > 0.999f && results[4] < 1.001f); |
| 158 | Main.assertTrue(results[8] > 70000.99f && results[8] < 70001.01f); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* |
| 162 | * We pass in the arguments and return the results so the compiler |
| 163 | * doesn't do the math for us. |
| 164 | */ |
| 165 | static double[] doubleOperTest(double x, double y) { |
| 166 | System.out.println("FloatMath.doubleOperTest"); |
| 167 | |
| 168 | double[] results = new double[9]; |
| 169 | |
| 170 | /* this seems to generate "op-double" instructions */ |
| 171 | results[0] = x + y; |
| 172 | results[1] = x - y; |
| 173 | results[2] = x * y; |
| 174 | results[3] = x / y; |
| 175 | results[4] = x % -y; |
| 176 | |
| 177 | /* this seems to generate "op-double/2addr" instructions */ |
| 178 | results[8] = x + (((((x + y) - y) * y) / y) % y); |
| 179 | |
| 180 | return results; |
| 181 | } |
| 182 | static void doubleOperCheck(double[] results) { |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 183 | Main.assertTrue(results[0] > 69996.99 && results[0] < 69997.01); |
| 184 | Main.assertTrue(results[1] > 70002.99 && results[1] < 70003.01); |
| 185 | Main.assertTrue(results[2] > -210000.01 && results[2] < -209999.99); |
| 186 | Main.assertTrue(results[3] > -23333.34 && results[3] < -23333.32); |
| 187 | Main.assertTrue(results[4] > 0.999 && results[4] < 1.001); |
| 188 | Main.assertTrue(results[8] > 70000.99 && results[8] < 70001.01); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Try to cause some unary operations. |
| 193 | */ |
| 194 | static float unopTest(float f) { |
| 195 | f = -f; |
| 196 | return f; |
| 197 | } |
| 198 | |
| 199 | static int[] convI(long l, float f, double d, float zero) { |
| 200 | int[] results = new int[6]; |
| 201 | results[0] = (int) l; |
| 202 | results[1] = (int) f; |
| 203 | results[2] = (int) d; |
| 204 | results[3] = (int) (1.0f / zero); // +inf |
| 205 | results[4] = (int) (-1.0f / zero); // -inf |
| 206 | results[5] = (int) ((1.0f / zero) / (1.0f / zero)); // NaN |
| 207 | return results; |
| 208 | } |
| 209 | static void checkConvI(int[] results) { |
| 210 | System.out.println("FloatMath.checkConvI"); |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 211 | Main.assertTrue(results[0] == 0x44332211); |
| 212 | Main.assertTrue(results[1] == 123); |
| 213 | Main.assertTrue(results[2] == -3); |
| 214 | Main.assertTrue(results[3] == 0x7fffffff); |
| 215 | Main.assertTrue(results[4] == 0x80000000); |
| 216 | Main.assertTrue(results[5] == 0); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static long[] convL(int i, float f, double d, double zero) { |
| 220 | long[] results = new long[6]; |
| 221 | results[0] = (long) i; |
| 222 | results[1] = (long) f; |
| 223 | results[2] = (long) d; |
| 224 | results[3] = (long) (1.0 / zero); // +inf |
| 225 | results[4] = (long) (-1.0 / zero); // -inf |
| 226 | results[5] = (long) ((1.0 / zero) / (1.0 / zero)); // NaN |
| 227 | return results; |
| 228 | } |
| 229 | static void checkConvL(long[] results) { |
| 230 | System.out.println("FloatMath.checkConvL"); |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 231 | Main.assertTrue(results[0] == 0xFFFFFFFF88776655L); |
| 232 | Main.assertTrue(results[1] == 123); |
| 233 | Main.assertTrue(results[2] == -3); |
| 234 | Main.assertTrue(results[3] == 0x7fffffffffffffffL); |
| 235 | Main.assertTrue(results[4] == 0x8000000000000000L); |
| 236 | Main.assertTrue(results[5] == 0); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static float[] convF(int i, long l, double d) { |
| 240 | float[] results = new float[3]; |
| 241 | results[0] = (float) i; |
| 242 | results[1] = (float) l; |
| 243 | results[2] = (float) d; |
| 244 | return results; |
| 245 | } |
| 246 | static void checkConvF(float[] results) { |
| 247 | System.out.println("FloatMath.checkConvF"); |
Yi Kong | a94596d | 2015-11-18 13:03:37 +0000 | [diff] [blame] | 248 | Main.assertTrue(results[0] == -2.0054409E9f); |
| 249 | Main.assertTrue(results[1] == -8.613303E18f); |
| 250 | Main.assertTrue(results[2] == -3.1415927f); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | static double[] convD(int i, long l, float f) { |
| 254 | double[] results = new double[3]; |
| 255 | results[0] = (double) i; |
| 256 | results[1] = (double) l; |
| 257 | results[2] = (double) f; |
| 258 | return results; |
| 259 | } |
| 260 | static void checkConvD(double[] results) { |
| 261 | System.out.println("FloatMath.checkConvD"); |
Yi Kong | a94596d | 2015-11-18 13:03:37 +0000 | [diff] [blame] | 262 | Main.assertTrue(results[0] == -2.005440939E9); |
| 263 | Main.assertTrue(results[1] == -8.6133032459203287E18); |
| 264 | Main.assertTrue(results[2] == 123.45600128173828); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | static void checkConsts() { |
| 268 | System.out.println("FloatMath.checkConsts"); |
| 269 | |
| 270 | float f = 10.0f; // const/special |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 271 | Main.assertTrue(f > 9.9 && f < 10.1); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 272 | |
| 273 | double d = 10.0; // const-wide/special |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 274 | Main.assertTrue(d > 9.9 && d < 10.1); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /* |
| 278 | * Determine if two floating point numbers are approximately equal. |
| 279 | * |
| 280 | * (Assumes that floating point is generally working, so we can't use |
| 281 | * this for the first set of tests.) |
| 282 | */ |
| 283 | static boolean approxEqual(float a, float b, float maxDelta) { |
| 284 | if (a > b) |
| 285 | return (a - b) < maxDelta; |
| 286 | else |
| 287 | return (b - a) < maxDelta; |
| 288 | } |
| 289 | static boolean approxEqual(double a, double b, double maxDelta) { |
| 290 | if (a > b) |
| 291 | return (a - b) < maxDelta; |
| 292 | else |
| 293 | return (b - a) < maxDelta; |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * Test some java.lang.Math functions. |
| 298 | * |
| 299 | * The method arguments are positive values. |
| 300 | */ |
| 301 | static void jlmTests(float ff, double dd) { |
| 302 | System.out.println("FloatMath.jlmTests"); |
| 303 | |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 304 | Main.assertTrue(approxEqual(Math.abs(ff), ff, 0.001f)); |
| 305 | Main.assertTrue(approxEqual(Math.abs(-ff), ff, 0.001f)); |
| 306 | Main.assertTrue(approxEqual(Math.min(ff, -5.0f), -5.0f, 0.001f)); |
| 307 | Main.assertTrue(approxEqual(Math.max(ff, -5.0f), ff, 0.001f)); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 308 | |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 309 | Main.assertTrue(approxEqual(Math.abs(dd), dd, 0.001)); |
| 310 | Main.assertTrue(approxEqual(Math.abs(-dd), dd, 0.001)); |
| 311 | Main.assertTrue(approxEqual(Math.min(dd, -5.0), -5.0, 0.001)); |
| 312 | Main.assertTrue(approxEqual(Math.max(dd, -5.0), dd, 0.001)); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 313 | |
| 314 | double sq = Math.sqrt(dd); |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 315 | Main.assertTrue(approxEqual(sq*sq, dd, 0.001)); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 316 | |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 317 | Main.assertTrue(approxEqual(0.5403023058681398, Math.cos(1.0), 0.00000001)); |
| 318 | Main.assertTrue(approxEqual(0.8414709848078965, Math.sin(1.0), 0.00000001)); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | public static void run() { |
| 322 | convTest(); |
| 323 | |
| 324 | float[] floatResults; |
| 325 | double[] doubleResults; |
| 326 | int[] intResults; |
| 327 | long[] longResults; |
| 328 | |
| 329 | floatResults = floatOperTest(70000.0f, -3.0f); |
| 330 | floatOperCheck(floatResults); |
| 331 | doubleResults = doubleOperTest(70000.0, -3.0); |
| 332 | doubleOperCheck(doubleResults); |
| 333 | |
| 334 | intResults = convI(0x8877665544332211L, 123.456f, -3.1415926535, 0.0f); |
| 335 | checkConvI(intResults); |
| 336 | longResults = convL(0x88776655, 123.456f, -3.1415926535, 0.0); |
| 337 | checkConvL(longResults); |
| 338 | floatResults = convF(0x88776655, 0x8877665544332211L, -3.1415926535); |
| 339 | checkConvF(floatResults); |
| 340 | doubleResults = convD(0x88776655, 0x8877665544332211L, 123.456f); |
| 341 | checkConvD(doubleResults); |
| 342 | |
| 343 | unopTest(123.456f); |
| 344 | |
| 345 | checkConsts(); |
| 346 | |
| 347 | jlmTests(3.14159f, 123456.78987654321); |
| 348 | } |
| 349 | } |