blob: a0bc9f46dd7c5b6420af8532cbb115fcca2b1aba [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
jeffhao5d1ac922011-09-29 17:41:15 -070016
17/**
18 * Test arithmetic operations.
19 */
20public 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;
jeffhao795d78f2011-09-30 18:34:35 -070033 Main.assertTrue(i == 1234);
jeffhao5d1ac922011-09-29 17:41:15 -070034
35 f = -1234.5678f;
36 i = (int) f;
jeffhao795d78f2011-09-30 18:34:35 -070037 Main.assertTrue(i == -1234);
jeffhao5d1ac922011-09-29 17:41:15 -070038
39 /* float --> long */
40 f = 1238.5678f;
41 l = (long) f;
jeffhao795d78f2011-09-30 18:34:35 -070042 Main.assertTrue(l == 1238);
jeffhao5d1ac922011-09-29 17:41:15 -070043
44 f = -1238.5678f;
45 l = (long) f;
jeffhao795d78f2011-09-30 18:34:35 -070046 Main.assertTrue(l == -1238);
jeffhao5d1ac922011-09-29 17:41:15 -070047
48 /* float --> double */
49 f = 1238.5678f;
50 d = (double) f;
jeffhao795d78f2011-09-30 18:34:35 -070051 Main.assertTrue(d > 1238.567 && d < 1238.568);
jeffhao5d1ac922011-09-29 17:41:15 -070052
53 /* double --> int */
54 d = 1234.5678;
55 i = (int) d;
jeffhao795d78f2011-09-30 18:34:35 -070056 Main.assertTrue(i == 1234);
jeffhao5d1ac922011-09-29 17:41:15 -070057
58 d = -1234.5678;
59 i = (int) d;
jeffhao795d78f2011-09-30 18:34:35 -070060 Main.assertTrue(i == -1234);
jeffhao5d1ac922011-09-29 17:41:15 -070061
62 /* double --> long */
63 d = 5678956789.0123;
64 l = (long) d;
jeffhao795d78f2011-09-30 18:34:35 -070065 Main.assertTrue(l == 5678956789L);
jeffhao5d1ac922011-09-29 17:41:15 -070066
67 d = -5678956789.0123;
68 l = (long) d;
jeffhao795d78f2011-09-30 18:34:35 -070069 Main.assertTrue(l == -5678956789L);
jeffhao5d1ac922011-09-29 17:41:15 -070070
71 /* double --> float */
72 d = 1238.5678;
73 f = (float) d;
jeffhao795d78f2011-09-30 18:34:35 -070074 Main.assertTrue(f > 1238.567 && f < 1238.568);
jeffhao5d1ac922011-09-29 17:41:15 -070075
76 /* int --> long */
77 i = 7654;
78 l = (long) i;
jeffhao795d78f2011-09-30 18:34:35 -070079 Main.assertTrue(l == 7654L);
jeffhao5d1ac922011-09-29 17:41:15 -070080
81 i = -7654;
82 l = (long) i;
jeffhao795d78f2011-09-30 18:34:35 -070083 Main.assertTrue(l == -7654L);
jeffhao5d1ac922011-09-29 17:41:15 -070084
85 /* int --> float */
86 i = 1234;
87 f = (float) i;
jeffhao795d78f2011-09-30 18:34:35 -070088 Main.assertTrue(f > 1233.9f && f < 1234.1f);
jeffhao5d1ac922011-09-29 17:41:15 -070089
90 i = -1234;
91 f = (float) i;
jeffhao795d78f2011-09-30 18:34:35 -070092 Main.assertTrue(f < -1233.9f && f > -1234.1f);
jeffhao5d1ac922011-09-29 17:41:15 -070093
94 /* int --> double */
95 i = 1238;
96 d = (double) i;
jeffhao795d78f2011-09-30 18:34:35 -070097 Main.assertTrue(d > 1237.9f && d < 1238.1f);
jeffhao5d1ac922011-09-29 17:41:15 -070098
99 i = -1238;
100 d = (double) i;
jeffhao795d78f2011-09-30 18:34:35 -0700101 Main.assertTrue(d < -1237.9f && d > -1238.1f);
jeffhao5d1ac922011-09-29 17:41:15 -0700102
103 /* long --> int (with truncation) */
104 l = 5678956789L;
105 i = (int) l;
jeffhao795d78f2011-09-30 18:34:35 -0700106 Main.assertTrue(i == 1383989493);
jeffhao5d1ac922011-09-29 17:41:15 -0700107
108 l = -5678956789L;
109 i = (int) l;
jeffhao795d78f2011-09-30 18:34:35 -0700110 Main.assertTrue(i == -1383989493);
jeffhao5d1ac922011-09-29 17:41:15 -0700111
112 /* long --> float */
113 l = 5678956789L;
114 f = (float) l;
jeffhao795d78f2011-09-30 18:34:35 -0700115 Main.assertTrue(f > 5.6789564E9 && f < 5.6789566E9);
jeffhao5d1ac922011-09-29 17:41:15 -0700116
117 l = -5678956789L;
118 f = (float) l;
jeffhao795d78f2011-09-30 18:34:35 -0700119 Main.assertTrue(f < -5.6789564E9 && f > -5.6789566E9);
jeffhao5d1ac922011-09-29 17:41:15 -0700120
121 /* long --> double */
122 l = 6678956789L;
123 d = (double) l;
jeffhao795d78f2011-09-30 18:34:35 -0700124 Main.assertTrue(d > 6.6789567E9 && d < 6.6789568E9);
jeffhao5d1ac922011-09-29 17:41:15 -0700125
126 l = -6678956789L;
127 d = (double) l;
jeffhao795d78f2011-09-30 18:34:35 -0700128 Main.assertTrue(d < -6.6789567E9 && d > -6.6789568E9);
jeffhao5d1ac922011-09-29 17:41:15 -0700129 }
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) {
jeffhao795d78f2011-09-30 18:34:35 -0700153 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);
jeffhao5d1ac922011-09-29 17:41:15 -0700159 }
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) {
jeffhao795d78f2011-09-30 18:34:35 -0700183 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);
jeffhao5d1ac922011-09-29 17:41:15 -0700189 }
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");
jeffhao795d78f2011-09-30 18:34:35 -0700211 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);
jeffhao5d1ac922011-09-29 17:41:15 -0700217 }
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");
jeffhao795d78f2011-09-30 18:34:35 -0700231 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);
jeffhao5d1ac922011-09-29 17:41:15 -0700237 }
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");
jeffhao795d78f2011-09-30 18:34:35 -0700248 // TODO: Main.assertTrue values
jeffhao5d1ac922011-09-29 17:41:15 -0700249 for (int i = 0; i < results.length; i++)
250 System.out.println(" " + i + ": " + results[i]);
251 System.out.println("-2.0054409E9, -8.6133031E18, -3.1415927");
252 }
253
254 static double[] convD(int i, long l, float f) {
255 double[] results = new double[3];
256 results[0] = (double) i;
257 results[1] = (double) l;
258 results[2] = (double) f;
259 return results;
260 }
261 static void checkConvD(double[] results) {
262 System.out.println("FloatMath.checkConvD");
jeffhao795d78f2011-09-30 18:34:35 -0700263 // TODO: Main.assertTrue values
jeffhao5d1ac922011-09-29 17:41:15 -0700264 for (int i = 0; i < results.length; i++)
265 System.out.println(" " + i + ": " + results[i]);
266 System.out.println("-2.005440939E9, -8.6133032459203287E18, 123.4560012817382");
267 }
268
269 static void checkConsts() {
270 System.out.println("FloatMath.checkConsts");
271
272 float f = 10.0f; // const/special
jeffhao795d78f2011-09-30 18:34:35 -0700273 Main.assertTrue(f > 9.9 && f < 10.1);
jeffhao5d1ac922011-09-29 17:41:15 -0700274
275 double d = 10.0; // const-wide/special
jeffhao795d78f2011-09-30 18:34:35 -0700276 Main.assertTrue(d > 9.9 && d < 10.1);
jeffhao5d1ac922011-09-29 17:41:15 -0700277 }
278
279 /*
280 * Determine if two floating point numbers are approximately equal.
281 *
282 * (Assumes that floating point is generally working, so we can't use
283 * this for the first set of tests.)
284 */
285 static boolean approxEqual(float a, float b, float maxDelta) {
286 if (a > b)
287 return (a - b) < maxDelta;
288 else
289 return (b - a) < maxDelta;
290 }
291 static boolean approxEqual(double a, double b, double maxDelta) {
292 if (a > b)
293 return (a - b) < maxDelta;
294 else
295 return (b - a) < maxDelta;
296 }
297
298 /*
299 * Test some java.lang.Math functions.
300 *
301 * The method arguments are positive values.
302 */
303 static void jlmTests(float ff, double dd) {
304 System.out.println("FloatMath.jlmTests");
305
jeffhao795d78f2011-09-30 18:34:35 -0700306 Main.assertTrue(approxEqual(Math.abs(ff), ff, 0.001f));
307 Main.assertTrue(approxEqual(Math.abs(-ff), ff, 0.001f));
308 Main.assertTrue(approxEqual(Math.min(ff, -5.0f), -5.0f, 0.001f));
309 Main.assertTrue(approxEqual(Math.max(ff, -5.0f), ff, 0.001f));
jeffhao5d1ac922011-09-29 17:41:15 -0700310
jeffhao795d78f2011-09-30 18:34:35 -0700311 Main.assertTrue(approxEqual(Math.abs(dd), dd, 0.001));
312 Main.assertTrue(approxEqual(Math.abs(-dd), dd, 0.001));
313 Main.assertTrue(approxEqual(Math.min(dd, -5.0), -5.0, 0.001));
314 Main.assertTrue(approxEqual(Math.max(dd, -5.0), dd, 0.001));
jeffhao5d1ac922011-09-29 17:41:15 -0700315
316 double sq = Math.sqrt(dd);
jeffhao795d78f2011-09-30 18:34:35 -0700317 Main.assertTrue(approxEqual(sq*sq, dd, 0.001));
jeffhao5d1ac922011-09-29 17:41:15 -0700318
jeffhao795d78f2011-09-30 18:34:35 -0700319 Main.assertTrue(approxEqual(0.5403023058681398, Math.cos(1.0), 0.00000001));
320 Main.assertTrue(approxEqual(0.8414709848078965, Math.sin(1.0), 0.00000001));
jeffhao5d1ac922011-09-29 17:41:15 -0700321 }
322
323 public static void run() {
324 convTest();
325
326 float[] floatResults;
327 double[] doubleResults;
328 int[] intResults;
329 long[] longResults;
330
331 floatResults = floatOperTest(70000.0f, -3.0f);
332 floatOperCheck(floatResults);
333 doubleResults = doubleOperTest(70000.0, -3.0);
334 doubleOperCheck(doubleResults);
335
336 intResults = convI(0x8877665544332211L, 123.456f, -3.1415926535, 0.0f);
337 checkConvI(intResults);
338 longResults = convL(0x88776655, 123.456f, -3.1415926535, 0.0);
339 checkConvL(longResults);
340 floatResults = convF(0x88776655, 0x8877665544332211L, -3.1415926535);
341 checkConvF(floatResults);
342 doubleResults = convD(0x88776655, 0x8877665544332211L, 123.456f);
343 checkConvD(doubleResults);
344
345 unopTest(123.456f);
346
347 checkConsts();
348
349 jlmTests(3.14159f, 123456.78987654321);
350 }
351}