blob: 96befe9cdcc98fa5e973f4e59e255514bc8ca133 [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");
Yi Konga94596d2015-11-18 13:03:37 +0000248 Main.assertTrue(results[0] == -2.0054409E9f);
249 Main.assertTrue(results[1] == -8.613303E18f);
250 Main.assertTrue(results[2] == -3.1415927f);
jeffhao5d1ac922011-09-29 17:41:15 -0700251 }
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 Konga94596d2015-11-18 13:03:37 +0000262 Main.assertTrue(results[0] == -2.005440939E9);
263 Main.assertTrue(results[1] == -8.6133032459203287E18);
264 Main.assertTrue(results[2] == 123.45600128173828);
jeffhao5d1ac922011-09-29 17:41:15 -0700265 }
266
267 static void checkConsts() {
268 System.out.println("FloatMath.checkConsts");
269
270 float f = 10.0f; // const/special
jeffhao795d78f2011-09-30 18:34:35 -0700271 Main.assertTrue(f > 9.9 && f < 10.1);
jeffhao5d1ac922011-09-29 17:41:15 -0700272
273 double d = 10.0; // const-wide/special
jeffhao795d78f2011-09-30 18:34:35 -0700274 Main.assertTrue(d > 9.9 && d < 10.1);
jeffhao5d1ac922011-09-29 17:41:15 -0700275 }
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
jeffhao795d78f2011-09-30 18:34:35 -0700304 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));
jeffhao5d1ac922011-09-29 17:41:15 -0700308
jeffhao795d78f2011-09-30 18:34:35 -0700309 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));
jeffhao5d1ac922011-09-29 17:41:15 -0700313
314 double sq = Math.sqrt(dd);
jeffhao795d78f2011-09-30 18:34:35 -0700315 Main.assertTrue(approxEqual(sq*sq, dd, 0.001));
jeffhao5d1ac922011-09-29 17:41:15 -0700316
jeffhao795d78f2011-09-30 18:34:35 -0700317 Main.assertTrue(approxEqual(0.5403023058681398, Math.cos(1.0), 0.00000001));
318 Main.assertTrue(approxEqual(0.8414709848078965, Math.sin(1.0), 0.00000001));
jeffhao5d1ac922011-09-29 17:41:15 -0700319 }
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}