blob: 0658a65aaae43b0013ce2bf4fccacf820c92693d [file] [log] [blame]
jeffhao5d1ac922011-09-29 17:41:15 -07001// Copyright 2006 The Android Open Source Project
2
3/**
4 * Test arithmetic operations.
5 */
6public class FloatMath {
7
8 static void convTest() {
9 System.out.println("FloatMath.convTest");
10
11 float f;
12 double d;
13 int i;
14 long l;
15
16 /* float --> int */
17 f = 1234.5678f;
18 i = (int) f;
jeffhao795d78f2011-09-30 18:34:35 -070019 Main.assertTrue(i == 1234);
jeffhao5d1ac922011-09-29 17:41:15 -070020
21 f = -1234.5678f;
22 i = (int) f;
jeffhao795d78f2011-09-30 18:34:35 -070023 Main.assertTrue(i == -1234);
jeffhao5d1ac922011-09-29 17:41:15 -070024
25 /* float --> long */
26 f = 1238.5678f;
27 l = (long) f;
jeffhao795d78f2011-09-30 18:34:35 -070028 Main.assertTrue(l == 1238);
jeffhao5d1ac922011-09-29 17:41:15 -070029
30 f = -1238.5678f;
31 l = (long) f;
jeffhao795d78f2011-09-30 18:34:35 -070032 Main.assertTrue(l == -1238);
jeffhao5d1ac922011-09-29 17:41:15 -070033
34 /* float --> double */
35 f = 1238.5678f;
36 d = (double) f;
jeffhao795d78f2011-09-30 18:34:35 -070037 Main.assertTrue(d > 1238.567 && d < 1238.568);
jeffhao5d1ac922011-09-29 17:41:15 -070038
39 /* double --> int */
40 d = 1234.5678;
41 i = (int) d;
jeffhao795d78f2011-09-30 18:34:35 -070042 Main.assertTrue(i == 1234);
jeffhao5d1ac922011-09-29 17:41:15 -070043
44 d = -1234.5678;
45 i = (int) d;
jeffhao795d78f2011-09-30 18:34:35 -070046 Main.assertTrue(i == -1234);
jeffhao5d1ac922011-09-29 17:41:15 -070047
48 /* double --> long */
49 d = 5678956789.0123;
50 l = (long) d;
jeffhao795d78f2011-09-30 18:34:35 -070051 Main.assertTrue(l == 5678956789L);
jeffhao5d1ac922011-09-29 17:41:15 -070052
53 d = -5678956789.0123;
54 l = (long) d;
jeffhao795d78f2011-09-30 18:34:35 -070055 Main.assertTrue(l == -5678956789L);
jeffhao5d1ac922011-09-29 17:41:15 -070056
57 /* double --> float */
58 d = 1238.5678;
59 f = (float) d;
jeffhao795d78f2011-09-30 18:34:35 -070060 Main.assertTrue(f > 1238.567 && f < 1238.568);
jeffhao5d1ac922011-09-29 17:41:15 -070061
62 /* int --> long */
63 i = 7654;
64 l = (long) i;
jeffhao795d78f2011-09-30 18:34:35 -070065 Main.assertTrue(l == 7654L);
jeffhao5d1ac922011-09-29 17:41:15 -070066
67 i = -7654;
68 l = (long) i;
jeffhao795d78f2011-09-30 18:34:35 -070069 Main.assertTrue(l == -7654L);
jeffhao5d1ac922011-09-29 17:41:15 -070070
71 /* int --> float */
72 i = 1234;
73 f = (float) i;
jeffhao795d78f2011-09-30 18:34:35 -070074 Main.assertTrue(f > 1233.9f && f < 1234.1f);
jeffhao5d1ac922011-09-29 17:41:15 -070075
76 i = -1234;
77 f = (float) i;
jeffhao795d78f2011-09-30 18:34:35 -070078 Main.assertTrue(f < -1233.9f && f > -1234.1f);
jeffhao5d1ac922011-09-29 17:41:15 -070079
80 /* int --> double */
81 i = 1238;
82 d = (double) i;
jeffhao795d78f2011-09-30 18:34:35 -070083 Main.assertTrue(d > 1237.9f && d < 1238.1f);
jeffhao5d1ac922011-09-29 17:41:15 -070084
85 i = -1238;
86 d = (double) i;
jeffhao795d78f2011-09-30 18:34:35 -070087 Main.assertTrue(d < -1237.9f && d > -1238.1f);
jeffhao5d1ac922011-09-29 17:41:15 -070088
89 /* long --> int (with truncation) */
90 l = 5678956789L;
91 i = (int) l;
jeffhao795d78f2011-09-30 18:34:35 -070092 Main.assertTrue(i == 1383989493);
jeffhao5d1ac922011-09-29 17:41:15 -070093
94 l = -5678956789L;
95 i = (int) l;
jeffhao795d78f2011-09-30 18:34:35 -070096 Main.assertTrue(i == -1383989493);
jeffhao5d1ac922011-09-29 17:41:15 -070097
98 /* long --> float */
99 l = 5678956789L;
100 f = (float) l;
jeffhao795d78f2011-09-30 18:34:35 -0700101 Main.assertTrue(f > 5.6789564E9 && f < 5.6789566E9);
jeffhao5d1ac922011-09-29 17:41:15 -0700102
103 l = -5678956789L;
104 f = (float) l;
jeffhao795d78f2011-09-30 18:34:35 -0700105 Main.assertTrue(f < -5.6789564E9 && f > -5.6789566E9);
jeffhao5d1ac922011-09-29 17:41:15 -0700106
107 /* long --> double */
108 l = 6678956789L;
109 d = (double) l;
jeffhao795d78f2011-09-30 18:34:35 -0700110 Main.assertTrue(d > 6.6789567E9 && d < 6.6789568E9);
jeffhao5d1ac922011-09-29 17:41:15 -0700111
112 l = -6678956789L;
113 d = (double) l;
jeffhao795d78f2011-09-30 18:34:35 -0700114 Main.assertTrue(d < -6.6789567E9 && d > -6.6789568E9);
jeffhao5d1ac922011-09-29 17:41:15 -0700115 }
116
117 /*
118 * We pass in the arguments and return the results so the compiler
119 * doesn't do the math for us.
120 */
121 static float[] floatOperTest(float x, float y) {
122 System.out.println("FloatMath.floatOperTest");
123
124 float[] results = new float[9];
125
126 /* this seems to generate "op-float" instructions */
127 results[0] = x + y;
128 results[1] = x - y;
129 results[2] = x * y;
130 results[3] = x / y;
131 results[4] = x % -y;
132
133 /* this seems to generate "op-float/2addr" instructions */
134 results[8] = x + (((((x + y) - y) * y) / y) % y);
135
136 return results;
137 }
138 static void floatOperCheck(float[] results) {
jeffhao795d78f2011-09-30 18:34:35 -0700139 Main.assertTrue(results[0] > 69996.99f && results[0] < 69997.01f);
140 Main.assertTrue(results[1] > 70002.99f && results[1] < 70003.01f);
141 Main.assertTrue(results[2] > -210000.01f && results[2] < -209999.99f);
142 Main.assertTrue(results[3] > -23333.34f && results[3] < -23333.32f);
143 Main.assertTrue(results[4] > 0.999f && results[4] < 1.001f);
144 Main.assertTrue(results[8] > 70000.99f && results[8] < 70001.01f);
jeffhao5d1ac922011-09-29 17:41:15 -0700145 }
146
147 /*
148 * We pass in the arguments and return the results so the compiler
149 * doesn't do the math for us.
150 */
151 static double[] doubleOperTest(double x, double y) {
152 System.out.println("FloatMath.doubleOperTest");
153
154 double[] results = new double[9];
155
156 /* this seems to generate "op-double" instructions */
157 results[0] = x + y;
158 results[1] = x - y;
159 results[2] = x * y;
160 results[3] = x / y;
161 results[4] = x % -y;
162
163 /* this seems to generate "op-double/2addr" instructions */
164 results[8] = x + (((((x + y) - y) * y) / y) % y);
165
166 return results;
167 }
168 static void doubleOperCheck(double[] results) {
jeffhao795d78f2011-09-30 18:34:35 -0700169 Main.assertTrue(results[0] > 69996.99 && results[0] < 69997.01);
170 Main.assertTrue(results[1] > 70002.99 && results[1] < 70003.01);
171 Main.assertTrue(results[2] > -210000.01 && results[2] < -209999.99);
172 Main.assertTrue(results[3] > -23333.34 && results[3] < -23333.32);
173 Main.assertTrue(results[4] > 0.999 && results[4] < 1.001);
174 Main.assertTrue(results[8] > 70000.99 && results[8] < 70001.01);
jeffhao5d1ac922011-09-29 17:41:15 -0700175 }
176
177 /*
178 * Try to cause some unary operations.
179 */
180 static float unopTest(float f) {
181 f = -f;
182 return f;
183 }
184
185 static int[] convI(long l, float f, double d, float zero) {
186 int[] results = new int[6];
187 results[0] = (int) l;
188 results[1] = (int) f;
189 results[2] = (int) d;
190 results[3] = (int) (1.0f / zero); // +inf
191 results[4] = (int) (-1.0f / zero); // -inf
192 results[5] = (int) ((1.0f / zero) / (1.0f / zero)); // NaN
193 return results;
194 }
195 static void checkConvI(int[] results) {
196 System.out.println("FloatMath.checkConvI");
jeffhao795d78f2011-09-30 18:34:35 -0700197 Main.assertTrue(results[0] == 0x44332211);
198 Main.assertTrue(results[1] == 123);
199 Main.assertTrue(results[2] == -3);
200 Main.assertTrue(results[3] == 0x7fffffff);
201 Main.assertTrue(results[4] == 0x80000000);
202 Main.assertTrue(results[5] == 0);
jeffhao5d1ac922011-09-29 17:41:15 -0700203 }
204
205 static long[] convL(int i, float f, double d, double zero) {
206 long[] results = new long[6];
207 results[0] = (long) i;
208 results[1] = (long) f;
209 results[2] = (long) d;
210 results[3] = (long) (1.0 / zero); // +inf
211 results[4] = (long) (-1.0 / zero); // -inf
212 results[5] = (long) ((1.0 / zero) / (1.0 / zero)); // NaN
213 return results;
214 }
215 static void checkConvL(long[] results) {
216 System.out.println("FloatMath.checkConvL");
jeffhao795d78f2011-09-30 18:34:35 -0700217 Main.assertTrue(results[0] == 0xFFFFFFFF88776655L);
218 Main.assertTrue(results[1] == 123);
219 Main.assertTrue(results[2] == -3);
220 Main.assertTrue(results[3] == 0x7fffffffffffffffL);
221 Main.assertTrue(results[4] == 0x8000000000000000L);
222 Main.assertTrue(results[5] == 0);
jeffhao5d1ac922011-09-29 17:41:15 -0700223 }
224
225 static float[] convF(int i, long l, double d) {
226 float[] results = new float[3];
227 results[0] = (float) i;
228 results[1] = (float) l;
229 results[2] = (float) d;
230 return results;
231 }
232 static void checkConvF(float[] results) {
233 System.out.println("FloatMath.checkConvF");
jeffhao795d78f2011-09-30 18:34:35 -0700234 // TODO: Main.assertTrue values
jeffhao5d1ac922011-09-29 17:41:15 -0700235 for (int i = 0; i < results.length; i++)
236 System.out.println(" " + i + ": " + results[i]);
237 System.out.println("-2.0054409E9, -8.6133031E18, -3.1415927");
238 }
239
240 static double[] convD(int i, long l, float f) {
241 double[] results = new double[3];
242 results[0] = (double) i;
243 results[1] = (double) l;
244 results[2] = (double) f;
245 return results;
246 }
247 static void checkConvD(double[] results) {
248 System.out.println("FloatMath.checkConvD");
jeffhao795d78f2011-09-30 18:34:35 -0700249 // TODO: Main.assertTrue values
jeffhao5d1ac922011-09-29 17:41:15 -0700250 for (int i = 0; i < results.length; i++)
251 System.out.println(" " + i + ": " + results[i]);
252 System.out.println("-2.005440939E9, -8.6133032459203287E18, 123.4560012817382");
253 }
254
255 static void checkConsts() {
256 System.out.println("FloatMath.checkConsts");
257
258 float f = 10.0f; // const/special
jeffhao795d78f2011-09-30 18:34:35 -0700259 Main.assertTrue(f > 9.9 && f < 10.1);
jeffhao5d1ac922011-09-29 17:41:15 -0700260
261 double d = 10.0; // const-wide/special
jeffhao795d78f2011-09-30 18:34:35 -0700262 Main.assertTrue(d > 9.9 && d < 10.1);
jeffhao5d1ac922011-09-29 17:41:15 -0700263 }
264
265 /*
266 * Determine if two floating point numbers are approximately equal.
267 *
268 * (Assumes that floating point is generally working, so we can't use
269 * this for the first set of tests.)
270 */
271 static boolean approxEqual(float a, float b, float maxDelta) {
272 if (a > b)
273 return (a - b) < maxDelta;
274 else
275 return (b - a) < maxDelta;
276 }
277 static boolean approxEqual(double a, double b, double maxDelta) {
278 if (a > b)
279 return (a - b) < maxDelta;
280 else
281 return (b - a) < maxDelta;
282 }
283
284 /*
285 * Test some java.lang.Math functions.
286 *
287 * The method arguments are positive values.
288 */
289 static void jlmTests(float ff, double dd) {
290 System.out.println("FloatMath.jlmTests");
291
jeffhao795d78f2011-09-30 18:34:35 -0700292 Main.assertTrue(approxEqual(Math.abs(ff), ff, 0.001f));
293 Main.assertTrue(approxEqual(Math.abs(-ff), ff, 0.001f));
294 Main.assertTrue(approxEqual(Math.min(ff, -5.0f), -5.0f, 0.001f));
295 Main.assertTrue(approxEqual(Math.max(ff, -5.0f), ff, 0.001f));
jeffhao5d1ac922011-09-29 17:41:15 -0700296
jeffhao795d78f2011-09-30 18:34:35 -0700297 Main.assertTrue(approxEqual(Math.abs(dd), dd, 0.001));
298 Main.assertTrue(approxEqual(Math.abs(-dd), dd, 0.001));
299 Main.assertTrue(approxEqual(Math.min(dd, -5.0), -5.0, 0.001));
300 Main.assertTrue(approxEqual(Math.max(dd, -5.0), dd, 0.001));
jeffhao5d1ac922011-09-29 17:41:15 -0700301
302 double sq = Math.sqrt(dd);
jeffhao795d78f2011-09-30 18:34:35 -0700303 Main.assertTrue(approxEqual(sq*sq, dd, 0.001));
jeffhao5d1ac922011-09-29 17:41:15 -0700304
jeffhao795d78f2011-09-30 18:34:35 -0700305 Main.assertTrue(approxEqual(0.5403023058681398, Math.cos(1.0), 0.00000001));
306 Main.assertTrue(approxEqual(0.8414709848078965, Math.sin(1.0), 0.00000001));
jeffhao5d1ac922011-09-29 17:41:15 -0700307 }
308
309 public static void run() {
310 convTest();
311
312 float[] floatResults;
313 double[] doubleResults;
314 int[] intResults;
315 long[] longResults;
316
317 floatResults = floatOperTest(70000.0f, -3.0f);
318 floatOperCheck(floatResults);
319 doubleResults = doubleOperTest(70000.0, -3.0);
320 doubleOperCheck(doubleResults);
321
322 intResults = convI(0x8877665544332211L, 123.456f, -3.1415926535, 0.0f);
323 checkConvI(intResults);
324 longResults = convL(0x88776655, 123.456f, -3.1415926535, 0.0);
325 checkConvL(longResults);
326 floatResults = convF(0x88776655, 0x8877665544332211L, -3.1415926535);
327 checkConvF(floatResults);
328 doubleResults = convD(0x88776655, 0x8877665544332211L, 123.456f);
329 checkConvD(doubleResults);
330
331 unopTest(123.456f);
332
333 checkConsts();
334
335 jlmTests(3.14159f, 123456.78987654321);
336 }
337}