001: /*
002: * Copyright 2003-2005 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.util;
017:
018: import java.math.BigDecimal;
019:
020: import org.apache.commons.math.TestUtils;
021:
022: import junit.framework.Test;
023: import junit.framework.TestCase;
024: import junit.framework.TestSuite;
025:
026: /**
027: * Test cases for the MathUtils class.
028: *
029: * @version $Revision: 321515 $ $Date: 2005-10-15 17:09:16 -0700 (Sat, 15 Oct 2005) $
030: */
031: public final class MathUtilsTest extends TestCase {
032:
033: public MathUtilsTest(String name) {
034: super (name);
035: }
036:
037: public static Test suite() {
038: TestSuite suite = new TestSuite(MathUtilsTest.class);
039: suite.setName("MathUtils Tests");
040: return suite;
041: }
042:
043: public void testAddAndCheck() {
044: int big = Integer.MAX_VALUE;
045: int bigNeg = Integer.MIN_VALUE;
046: assertEquals(big, MathUtils.addAndCheck(big, 0));
047: try {
048: int res = MathUtils.addAndCheck(big, 1);
049: fail("Expecting ArithmeticException");
050: } catch (ArithmeticException ex) {
051: }
052: try {
053: int res = MathUtils.addAndCheck(bigNeg, -1);
054: fail("Expecting ArithmeticException");
055: } catch (ArithmeticException ex) {
056: }
057: }
058:
059: public void testMulAndCheck() {
060: int big = Integer.MAX_VALUE;
061: int bigNeg = Integer.MIN_VALUE;
062: assertEquals(big, MathUtils.mulAndCheck(big, 1));
063: try {
064: int res = MathUtils.mulAndCheck(big, 2);
065: fail("Expecting ArithmeticException");
066: } catch (ArithmeticException ex) {
067: }
068: try {
069: int res = MathUtils.mulAndCheck(bigNeg, 2);
070: fail("Expecting ArithmeticException");
071: } catch (ArithmeticException ex) {
072: }
073: }
074:
075: public void testSubAndCheck() {
076: int big = Integer.MAX_VALUE;
077: int bigNeg = Integer.MIN_VALUE;
078: assertEquals(big, MathUtils.subAndCheck(big, 0));
079: try {
080: int res = MathUtils.subAndCheck(big, -1);
081: fail("Expecting ArithmeticException");
082: } catch (ArithmeticException ex) {
083: }
084: try {
085: int res = MathUtils.subAndCheck(bigNeg, 1);
086: fail("Expecting ArithmeticException");
087: } catch (ArithmeticException ex) {
088: }
089: }
090:
091: public void testSubAndCheckErrorMessage() {
092: int big = Integer.MAX_VALUE;
093: int bigNeg = Integer.MIN_VALUE;
094: try {
095: int res = MathUtils.subAndCheck(big, -1);
096: fail("Expecting ArithmeticException");
097: } catch (ArithmeticException ex) {
098: assertEquals("overflow: subtract", ex.getMessage());
099: }
100: }
101:
102: public void testBinomialCoefficient() {
103: long[] bcoef5 = { 1, 5, 10, 10, 5, 1 };
104: long[] bcoef6 = { 1, 6, 15, 20, 15, 6, 1 };
105: for (int i = 0; i < 6; i++) {
106: assertEquals("5 choose " + i, bcoef5[i], MathUtils
107: .binomialCoefficient(5, i));
108: }
109: for (int i = 0; i < 7; i++) {
110: assertEquals("6 choose " + i, bcoef6[i], MathUtils
111: .binomialCoefficient(6, i));
112: }
113:
114: for (int n = 1; n < 10; n++) {
115: for (int k = 0; k <= n; k++) {
116: assertEquals(n + " choose " + k, binomialCoefficient(n,
117: k), MathUtils.binomialCoefficient(n, k));
118: assertEquals(n + " choose " + k,
119: (double) binomialCoefficient(n, k), MathUtils
120: .binomialCoefficientDouble(n, k),
121: Double.MIN_VALUE);
122: assertEquals(n + " choose " + k, Math
123: .log((double) binomialCoefficient(n, k)),
124: MathUtils.binomialCoefficientLog(n, k), 10E-12);
125: }
126: }
127:
128: /*
129: * Takes a long time for recursion to unwind, but succeeds
130: * and yields exact value = 2,333,606,220
131:
132: assertEquals(MathUtils.binomialCoefficient(34,17),
133: binomialCoefficient(34,17));
134: */
135: }
136:
137: /** Verify that b(0,0) = 1 */
138: public void test0Choose0() {
139: assertEquals(MathUtils.binomialCoefficientDouble(0, 0), 1d, 0);
140: assertEquals(MathUtils.binomialCoefficientLog(0, 0), 0d, 0);
141: assertEquals(MathUtils.binomialCoefficient(0, 0), 1);
142: }
143:
144: public void testBinomialCoefficientFail() {
145: try {
146: long x = MathUtils.binomialCoefficient(4, 5);
147: fail("expecting IllegalArgumentException");
148: } catch (IllegalArgumentException ex) {
149: ;
150: }
151:
152: try {
153: double x = MathUtils.binomialCoefficientDouble(4, 5);
154: fail("expecting IllegalArgumentException");
155: } catch (IllegalArgumentException ex) {
156: ;
157: }
158:
159: try {
160: double x = MathUtils.binomialCoefficientLog(4, 5);
161: fail("expecting IllegalArgumentException");
162: } catch (IllegalArgumentException ex) {
163: ;
164: }
165: try {
166: long x = MathUtils.binomialCoefficient(67, 34);
167: fail("expecting ArithmeticException");
168: } catch (ArithmeticException ex) {
169: ;
170: }
171: double x = MathUtils.binomialCoefficientDouble(1030, 515);
172: assertTrue("expecting infinite binomial coefficient", Double
173: .isInfinite(x));
174: }
175:
176: public void testFactorial() {
177: for (int i = 1; i < 10; i++) {
178: assertEquals(i + "! ", factorial(i), MathUtils.factorial(i));
179: assertEquals(i + "! ", (double) factorial(i), MathUtils
180: .factorialDouble(i), Double.MIN_VALUE);
181: assertEquals(i + "! ", Math.log((double) factorial(i)),
182: MathUtils.factorialLog(i), 10E-12);
183: }
184: assertEquals("0", 1, MathUtils.factorial(0));
185: assertEquals("0", 1.0d, MathUtils.factorialDouble(0), 1E-14);
186: assertEquals("0", 0.0d, MathUtils.factorialLog(0), 1E-14);
187: }
188:
189: public void testFactorialFail() {
190: try {
191: long x = MathUtils.factorial(-1);
192: fail("expecting IllegalArgumentException");
193: } catch (IllegalArgumentException ex) {
194: ;
195: }
196: try {
197: double x = MathUtils.factorialDouble(-1);
198: fail("expecting IllegalArgumentException");
199: } catch (IllegalArgumentException ex) {
200: ;
201: }
202: try {
203: double x = MathUtils.factorialLog(-1);
204: fail("expecting IllegalArgumentException");
205: } catch (IllegalArgumentException ex) {
206: ;
207: }
208: try {
209: double x = MathUtils.factorial(21);
210: fail("expecting ArithmeticException");
211: } catch (ArithmeticException ex) {
212: ;
213: }
214: assertTrue("expecting infinite factorial value", Double
215: .isInfinite(MathUtils.factorialDouble(171)));
216: }
217:
218: /**
219: * Exact recursive implementation to test against
220: */
221: private long binomialCoefficient(int n, int k) {
222: if ((n == k) || (k == 0)) {
223: return 1;
224: }
225: if ((k == 1) || (k == n - 1)) {
226: return n;
227: }
228: return binomialCoefficient(n - 1, k - 1)
229: + binomialCoefficient(n - 1, k);
230: }
231:
232: /**
233: * Finds the largest values of n for which binomialCoefficient and
234: * binomialCoefficientDouble will return values that fit in a long, double,
235: * resp. Remove comments around test below to get this in test-report
236: *
237: public void testLimits() {
238: findBinomialLimits();
239: }
240: */
241:
242: private void findBinomialLimits() {
243: /**
244: * will kick out 66 as the limit for long
245: */
246: boolean foundLimit = false;
247: int test = 10;
248: while (!foundLimit) {
249: try {
250: double x = MathUtils
251: .binomialCoefficient(test, test / 2);
252: } catch (ArithmeticException ex) {
253: foundLimit = true;
254: System.out
255: .println("largest n for binomialCoefficient = "
256: + (test - 1));
257: }
258: test++;
259: }
260:
261: /**
262: * will kick out 1029 as the limit for double
263: */
264: foundLimit = false;
265: test = 10;
266: while (!foundLimit) {
267: double x = MathUtils.binomialCoefficientDouble(test,
268: test / 2);
269: if (Double.isInfinite(x)) {
270: foundLimit = true;
271: System.out
272: .println("largest n for binomialCoefficientD = "
273: + (test - 1));
274: }
275: test++;
276: }
277: }
278:
279: /**
280: * Finds the largest values of n for which factiorial and
281: * factorialDouble will return values that fit in a long, double,
282: * resp. Remove comments around test below to get this in test-report
283:
284: public void testFactiorialLimits() {
285: findFactorialLimits();
286: }
287: */
288:
289: private void findFactorialLimits() {
290: /**
291: * will kick out 20 as the limit for long
292: */
293: boolean foundLimit = false;
294: int test = 10;
295: while (!foundLimit) {
296: try {
297: double x = MathUtils.factorial(test);
298: } catch (ArithmeticException ex) {
299: foundLimit = true;
300: System.out.println("largest n for factorial = "
301: + (test - 1));
302: }
303: test++;
304: }
305:
306: /**
307: * will kick out 170 as the limit for double
308: */
309: foundLimit = false;
310: test = 10;
311: while (!foundLimit) {
312: double x = MathUtils.factorialDouble(test);
313: if (Double.isInfinite(x)) {
314: foundLimit = true;
315: System.out.println("largest n for factorialDouble = "
316: + (test - 1));
317: }
318: test++;
319: }
320: }
321:
322: /**
323: * Exact direct multiplication implementation to test against
324: */
325: private long factorial(int n) {
326: long result = 1;
327: for (int i = 2; i <= n; i++) {
328: result *= i;
329: }
330: return result;
331: }
332:
333: public void testSignDouble() {
334: double delta = 0.0;
335: assertEquals(1.0, MathUtils.indicator(2.0), delta);
336: assertEquals(-1.0, MathUtils.indicator(-2.0), delta);
337: }
338:
339: public void testSignFloat() {
340: float delta = 0.0F;
341: assertEquals(1.0F, MathUtils.indicator(2.0F), delta);
342: assertEquals(-1.0F, MathUtils.indicator(-2.0F), delta);
343: }
344:
345: public void testSignByte() {
346: assertEquals((byte) 1, MathUtils.indicator((byte) 2));
347: assertEquals((byte) (-1), MathUtils.indicator((byte) (-2)));
348: }
349:
350: public void testSignShort() {
351: assertEquals((short) 1, MathUtils.indicator((short) 2));
352: assertEquals((short) (-1), MathUtils.indicator((short) (-2)));
353: }
354:
355: public void testSignInt() {
356: assertEquals((int) 1, MathUtils.indicator((int) (2)));
357: assertEquals((int) (-1), MathUtils.indicator((int) (-2)));
358: }
359:
360: public void testSignLong() {
361: assertEquals(1L, MathUtils.indicator(2L));
362: assertEquals(-1L, MathUtils.indicator(-2L));
363: }
364:
365: public void testIndicatorDouble() {
366: double delta = 0.0;
367: assertEquals(1.0, MathUtils.indicator(2.0), delta);
368: assertEquals(1.0, MathUtils.indicator(0.0), delta);
369: assertEquals(-1.0, MathUtils.indicator(-2.0), delta);
370: }
371:
372: public void testIndicatorFloat() {
373: float delta = 0.0F;
374: assertEquals(1.0F, MathUtils.indicator(2.0F), delta);
375: assertEquals(1.0F, MathUtils.indicator(0.0F), delta);
376: assertEquals(-1.0F, MathUtils.indicator(-2.0F), delta);
377: }
378:
379: public void testIndicatorByte() {
380: assertEquals((byte) 1, MathUtils.indicator((byte) 2));
381: assertEquals((byte) 1, MathUtils.indicator((byte) 0));
382: assertEquals((byte) (-1), MathUtils.indicator((byte) (-2)));
383: }
384:
385: public void testIndicatorShort() {
386: assertEquals((short) 1, MathUtils.indicator((short) 2));
387: assertEquals((short) 1, MathUtils.indicator((short) 0));
388: assertEquals((short) (-1), MathUtils.indicator((short) (-2)));
389: }
390:
391: public void testIndicatorInt() {
392: assertEquals((int) 1, MathUtils.indicator((int) (2)));
393: assertEquals((int) 1, MathUtils.indicator((int) (0)));
394: assertEquals((int) (-1), MathUtils.indicator((int) (-2)));
395: }
396:
397: public void testIndicatorLong() {
398: assertEquals(1L, MathUtils.indicator(2L));
399: assertEquals(1L, MathUtils.indicator(0L));
400: assertEquals(-1L, MathUtils.indicator(-2L));
401: }
402:
403: public void testCosh() {
404: double x = 3.0;
405: double expected = 10.06766;
406: assertEquals(expected, MathUtils.cosh(x), 1.0e-5);
407: }
408:
409: public void testSinh() {
410: double x = 3.0;
411: double expected = 10.01787;
412: assertEquals(expected, MathUtils.sinh(x), 1.0e-5);
413: }
414:
415: public void testCoshNaN() {
416: assertTrue(Double.isNaN(MathUtils.cosh(Double.NaN)));
417: }
418:
419: public void testSinhNaN() {
420: assertTrue(Double.isNaN(MathUtils.sinh(Double.NaN)));
421: }
422:
423: public void testEquals() {
424: double[] testArray = { Double.NaN, Double.POSITIVE_INFINITY,
425: Double.NEGATIVE_INFINITY, 1d, 0d };
426: for (int i = 0; i < testArray.length; i++) {
427: for (int j = 0; j < testArray.length; j++) {
428: if (i == j) {
429: assertTrue(MathUtils.equals(testArray[i],
430: testArray[j]));
431: assertTrue(MathUtils.equals(testArray[j],
432: testArray[i]));
433: } else {
434: assertTrue(!MathUtils.equals(testArray[i],
435: testArray[j]));
436: assertTrue(!MathUtils.equals(testArray[j],
437: testArray[i]));
438: }
439: }
440: }
441: }
442:
443: public void testHash() {
444: double[] testArray = { Double.NaN, Double.POSITIVE_INFINITY,
445: Double.NEGATIVE_INFINITY, 1d, 0d, 1E-14, (1 + 1E-14),
446: Double.MIN_VALUE, Double.MAX_VALUE };
447: for (int i = 0; i < testArray.length; i++) {
448: for (int j = 0; j < testArray.length; j++) {
449: if (i == j) {
450: assertEquals(MathUtils.hash(testArray[i]),
451: MathUtils.hash(testArray[j]));
452: assertEquals(MathUtils.hash(testArray[j]),
453: MathUtils.hash(testArray[i]));
454: } else {
455: assertTrue(MathUtils.hash(testArray[i]) != MathUtils
456: .hash(testArray[j]));
457: assertTrue(MathUtils.hash(testArray[j]) != MathUtils
458: .hash(testArray[i]));
459: }
460: }
461: }
462: }
463:
464: public void testGcd() {
465: int a = 30;
466: int b = 50;
467: int c = 77;
468:
469: assertEquals(0, MathUtils.gcd(0, 0));
470:
471: assertEquals(b, MathUtils.gcd(0, b));
472: assertEquals(a, MathUtils.gcd(a, 0));
473: assertEquals(b, MathUtils.gcd(0, -b));
474: assertEquals(a, MathUtils.gcd(-a, 0));
475:
476: assertEquals(10, MathUtils.gcd(a, b));
477: assertEquals(10, MathUtils.gcd(-a, b));
478: assertEquals(10, MathUtils.gcd(a, -b));
479: assertEquals(10, MathUtils.gcd(-a, -b));
480:
481: assertEquals(1, MathUtils.gcd(a, c));
482: assertEquals(1, MathUtils.gcd(-a, c));
483: assertEquals(1, MathUtils.gcd(a, -c));
484: assertEquals(1, MathUtils.gcd(-a, -c));
485: }
486:
487: public void testLcm() {
488: int a = 30;
489: int b = 50;
490: int c = 77;
491:
492: assertEquals(0, MathUtils.lcm(0, b));
493: assertEquals(0, MathUtils.lcm(a, 0));
494: assertEquals(b, MathUtils.lcm(1, b));
495: assertEquals(a, MathUtils.lcm(a, 1));
496: assertEquals(150, MathUtils.lcm(a, b));
497: assertEquals(150, MathUtils.lcm(-a, b));
498: assertEquals(150, MathUtils.lcm(a, -b));
499: assertEquals(2310, MathUtils.lcm(a, c));
500:
501: try {
502: MathUtils.lcm(Integer.MAX_VALUE, Integer.MAX_VALUE - 1);
503: fail("Expecting ArithmeticException");
504: } catch (ArithmeticException ex) {
505: // expected
506: }
507: }
508:
509: public void testRoundFloat() {
510: float x = 1.234567890f;
511: assertEquals(1.23f, MathUtils.round(x, 2), 0.0);
512: assertEquals(1.235f, MathUtils.round(x, 3), 0.0);
513: assertEquals(1.2346f, MathUtils.round(x, 4), 0.0);
514:
515: // BZ 35904
516: assertEquals(30.1f, MathUtils.round(30.095f, 2), 0.0f);
517: assertEquals(30.1f, MathUtils.round(30.095f, 1), 0.0f);
518: assertEquals(50.09f, MathUtils.round(50.085f, 2), 0.0f);
519: assertEquals(50.19f, MathUtils.round(50.185f, 2), 0.0f);
520: assertEquals(50.01f, MathUtils.round(50.005f, 2), 0.0f);
521: assertEquals(30.01f, MathUtils.round(30.005f, 2), 0.0f);
522: assertEquals(30.65f, MathUtils.round(30.645f, 2), 0.0f);
523:
524: assertEquals(1.24f, MathUtils.round(x, 2,
525: BigDecimal.ROUND_CEILING), 0.0);
526: assertEquals(1.235f, MathUtils.round(x, 3,
527: BigDecimal.ROUND_CEILING), 0.0);
528: assertEquals(1.2346f, MathUtils.round(x, 4,
529: BigDecimal.ROUND_CEILING), 0.0);
530: assertEquals(-1.23f, MathUtils.round(-x, 2,
531: BigDecimal.ROUND_CEILING), 0.0);
532: assertEquals(-1.234f, MathUtils.round(-x, 3,
533: BigDecimal.ROUND_CEILING), 0.0);
534: assertEquals(-1.2345f, MathUtils.round(-x, 4,
535: BigDecimal.ROUND_CEILING), 0.0);
536:
537: assertEquals(1.23f, MathUtils
538: .round(x, 2, BigDecimal.ROUND_DOWN), 0.0);
539: assertEquals(1.234f, MathUtils.round(x, 3,
540: BigDecimal.ROUND_DOWN), 0.0);
541: assertEquals(1.2345f, MathUtils.round(x, 4,
542: BigDecimal.ROUND_DOWN), 0.0);
543: assertEquals(-1.23f, MathUtils.round(-x, 2,
544: BigDecimal.ROUND_DOWN), 0.0);
545: assertEquals(-1.234f, MathUtils.round(-x, 3,
546: BigDecimal.ROUND_DOWN), 0.0);
547: assertEquals(-1.2345f, MathUtils.round(-x, 4,
548: BigDecimal.ROUND_DOWN), 0.0);
549:
550: assertEquals(1.23f, MathUtils.round(x, 2,
551: BigDecimal.ROUND_FLOOR), 0.0);
552: assertEquals(1.234f, MathUtils.round(x, 3,
553: BigDecimal.ROUND_FLOOR), 0.0);
554: assertEquals(1.2345f, MathUtils.round(x, 4,
555: BigDecimal.ROUND_FLOOR), 0.0);
556: assertEquals(-1.24f, MathUtils.round(-x, 2,
557: BigDecimal.ROUND_FLOOR), 0.0);
558: assertEquals(-1.235f, MathUtils.round(-x, 3,
559: BigDecimal.ROUND_FLOOR), 0.0);
560: assertEquals(-1.2346f, MathUtils.round(-x, 4,
561: BigDecimal.ROUND_FLOOR), 0.0);
562:
563: assertEquals(1.23f, MathUtils.round(x, 2,
564: BigDecimal.ROUND_HALF_DOWN), 0.0);
565: assertEquals(1.235f, MathUtils.round(x, 3,
566: BigDecimal.ROUND_HALF_DOWN), 0.0);
567: assertEquals(1.2346f, MathUtils.round(x, 4,
568: BigDecimal.ROUND_HALF_DOWN), 0.0);
569: assertEquals(-1.23f, MathUtils.round(-x, 2,
570: BigDecimal.ROUND_HALF_DOWN), 0.0);
571: assertEquals(-1.235f, MathUtils.round(-x, 3,
572: BigDecimal.ROUND_HALF_DOWN), 0.0);
573: assertEquals(-1.2346f, MathUtils.round(-x, 4,
574: BigDecimal.ROUND_HALF_DOWN), 0.0);
575: assertEquals(1.234f, MathUtils.round(1.2345f, 3,
576: BigDecimal.ROUND_HALF_DOWN), 0.0);
577: assertEquals(-1.234f, MathUtils.round(-1.2345f, 3,
578: BigDecimal.ROUND_HALF_DOWN), 0.0);
579:
580: assertEquals(1.23f, MathUtils.round(x, 2,
581: BigDecimal.ROUND_HALF_EVEN), 0.0);
582: assertEquals(1.235f, MathUtils.round(x, 3,
583: BigDecimal.ROUND_HALF_EVEN), 0.0);
584: assertEquals(1.2346f, MathUtils.round(x, 4,
585: BigDecimal.ROUND_HALF_EVEN), 0.0);
586: assertEquals(-1.23f, MathUtils.round(-x, 2,
587: BigDecimal.ROUND_HALF_EVEN), 0.0);
588: assertEquals(-1.235f, MathUtils.round(-x, 3,
589: BigDecimal.ROUND_HALF_EVEN), 0.0);
590: assertEquals(-1.2346f, MathUtils.round(-x, 4,
591: BigDecimal.ROUND_HALF_EVEN), 0.0);
592: assertEquals(1.234f, MathUtils.round(1.2345f, 3,
593: BigDecimal.ROUND_HALF_EVEN), 0.0);
594: assertEquals(-1.234f, MathUtils.round(-1.2345f, 3,
595: BigDecimal.ROUND_HALF_EVEN), 0.0);
596: assertEquals(1.236f, MathUtils.round(1.2355f, 3,
597: BigDecimal.ROUND_HALF_EVEN), 0.0);
598: assertEquals(-1.236f, MathUtils.round(-1.2355f, 3,
599: BigDecimal.ROUND_HALF_EVEN), 0.0);
600:
601: assertEquals(1.23f, MathUtils.round(x, 2,
602: BigDecimal.ROUND_HALF_UP), 0.0);
603: assertEquals(1.235f, MathUtils.round(x, 3,
604: BigDecimal.ROUND_HALF_UP), 0.0);
605: assertEquals(1.2346f, MathUtils.round(x, 4,
606: BigDecimal.ROUND_HALF_UP), 0.0);
607: assertEquals(-1.23f, MathUtils.round(-x, 2,
608: BigDecimal.ROUND_HALF_UP), 0.0);
609: assertEquals(-1.235f, MathUtils.round(-x, 3,
610: BigDecimal.ROUND_HALF_UP), 0.0);
611: assertEquals(-1.2346f, MathUtils.round(-x, 4,
612: BigDecimal.ROUND_HALF_UP), 0.0);
613: assertEquals(1.235f, MathUtils.round(1.2345f, 3,
614: BigDecimal.ROUND_HALF_UP), 0.0);
615: assertEquals(-1.235f, MathUtils.round(-1.2345f, 3,
616: BigDecimal.ROUND_HALF_UP), 0.0);
617:
618: assertEquals(-1.23f, MathUtils.round(-1.23f, 2,
619: BigDecimal.ROUND_UNNECESSARY), 0.0);
620: assertEquals(1.23f, MathUtils.round(1.23f, 2,
621: BigDecimal.ROUND_UNNECESSARY), 0.0);
622:
623: try {
624: MathUtils.round(1.234f, 2, BigDecimal.ROUND_UNNECESSARY);
625: fail();
626: } catch (ArithmeticException ex) {
627: // success
628: }
629:
630: assertEquals(1.24f, MathUtils.round(x, 2, BigDecimal.ROUND_UP),
631: 0.0);
632: assertEquals(1.235f,
633: MathUtils.round(x, 3, BigDecimal.ROUND_UP), 0.0);
634: assertEquals(1.2346f, MathUtils
635: .round(x, 4, BigDecimal.ROUND_UP), 0.0);
636: assertEquals(-1.24f, MathUtils
637: .round(-x, 2, BigDecimal.ROUND_UP), 0.0);
638: assertEquals(-1.235f, MathUtils.round(-x, 3,
639: BigDecimal.ROUND_UP), 0.0);
640: assertEquals(-1.2346f, MathUtils.round(-x, 4,
641: BigDecimal.ROUND_UP), 0.0);
642:
643: try {
644: MathUtils.round(1.234f, 2, 1923);
645: fail();
646: } catch (IllegalArgumentException ex) {
647: // success
648: }
649:
650: // special values
651: TestUtils.assertEquals(Float.NaN,
652: MathUtils.round(Float.NaN, 2), 0.0f);
653: assertEquals(0.0f, MathUtils.round(0.0f, 2), 0.0f);
654: assertEquals(Float.POSITIVE_INFINITY, MathUtils.round(
655: Float.POSITIVE_INFINITY, 2), 0.0f);
656: assertEquals(Float.NEGATIVE_INFINITY, MathUtils.round(
657: Float.NEGATIVE_INFINITY, 2), 0.0f);
658: }
659:
660: public void testRoundDouble() {
661: double x = 1.234567890;
662: assertEquals(1.23, MathUtils.round(x, 2), 0.0);
663: assertEquals(1.235, MathUtils.round(x, 3), 0.0);
664: assertEquals(1.2346, MathUtils.round(x, 4), 0.0);
665:
666: // BZ 35904
667: assertEquals(30.1d, MathUtils.round(30.095d, 2), 0.0d);
668: assertEquals(30.1d, MathUtils.round(30.095d, 1), 0.0d);
669: assertEquals(50.09d, MathUtils.round(50.085d, 2), 0.0d);
670: assertEquals(50.19d, MathUtils.round(50.185d, 2), 0.0d);
671: assertEquals(50.01d, MathUtils.round(50.005d, 2), 0.0d);
672: assertEquals(30.01d, MathUtils.round(30.005d, 2), 0.0d);
673: assertEquals(30.65d, MathUtils.round(30.645d, 2), 0.0d);
674:
675: assertEquals(1.24, MathUtils.round(x, 2,
676: BigDecimal.ROUND_CEILING), 0.0);
677: assertEquals(1.235, MathUtils.round(x, 3,
678: BigDecimal.ROUND_CEILING), 0.0);
679: assertEquals(1.2346, MathUtils.round(x, 4,
680: BigDecimal.ROUND_CEILING), 0.0);
681: assertEquals(-1.23, MathUtils.round(-x, 2,
682: BigDecimal.ROUND_CEILING), 0.0);
683: assertEquals(-1.234, MathUtils.round(-x, 3,
684: BigDecimal.ROUND_CEILING), 0.0);
685: assertEquals(-1.2345, MathUtils.round(-x, 4,
686: BigDecimal.ROUND_CEILING), 0.0);
687:
688: assertEquals(1.23,
689: MathUtils.round(x, 2, BigDecimal.ROUND_DOWN), 0.0);
690: assertEquals(1.234, MathUtils
691: .round(x, 3, BigDecimal.ROUND_DOWN), 0.0);
692: assertEquals(1.2345, MathUtils.round(x, 4,
693: BigDecimal.ROUND_DOWN), 0.0);
694: assertEquals(-1.23, MathUtils.round(-x, 2,
695: BigDecimal.ROUND_DOWN), 0.0);
696: assertEquals(-1.234, MathUtils.round(-x, 3,
697: BigDecimal.ROUND_DOWN), 0.0);
698: assertEquals(-1.2345, MathUtils.round(-x, 4,
699: BigDecimal.ROUND_DOWN), 0.0);
700:
701: assertEquals(1.23, MathUtils
702: .round(x, 2, BigDecimal.ROUND_FLOOR), 0.0);
703: assertEquals(1.234, MathUtils.round(x, 3,
704: BigDecimal.ROUND_FLOOR), 0.0);
705: assertEquals(1.2345, MathUtils.round(x, 4,
706: BigDecimal.ROUND_FLOOR), 0.0);
707: assertEquals(-1.24, MathUtils.round(-x, 2,
708: BigDecimal.ROUND_FLOOR), 0.0);
709: assertEquals(-1.235, MathUtils.round(-x, 3,
710: BigDecimal.ROUND_FLOOR), 0.0);
711: assertEquals(-1.2346, MathUtils.round(-x, 4,
712: BigDecimal.ROUND_FLOOR), 0.0);
713:
714: assertEquals(1.23, MathUtils.round(x, 2,
715: BigDecimal.ROUND_HALF_DOWN), 0.0);
716: assertEquals(1.235, MathUtils.round(x, 3,
717: BigDecimal.ROUND_HALF_DOWN), 0.0);
718: assertEquals(1.2346, MathUtils.round(x, 4,
719: BigDecimal.ROUND_HALF_DOWN), 0.0);
720: assertEquals(-1.23, MathUtils.round(-x, 2,
721: BigDecimal.ROUND_HALF_DOWN), 0.0);
722: assertEquals(-1.235, MathUtils.round(-x, 3,
723: BigDecimal.ROUND_HALF_DOWN), 0.0);
724: assertEquals(-1.2346, MathUtils.round(-x, 4,
725: BigDecimal.ROUND_HALF_DOWN), 0.0);
726: assertEquals(1.234, MathUtils.round(1.2345, 3,
727: BigDecimal.ROUND_HALF_DOWN), 0.0);
728: assertEquals(-1.234, MathUtils.round(-1.2345, 3,
729: BigDecimal.ROUND_HALF_DOWN), 0.0);
730:
731: assertEquals(1.23, MathUtils.round(x, 2,
732: BigDecimal.ROUND_HALF_EVEN), 0.0);
733: assertEquals(1.235, MathUtils.round(x, 3,
734: BigDecimal.ROUND_HALF_EVEN), 0.0);
735: assertEquals(1.2346, MathUtils.round(x, 4,
736: BigDecimal.ROUND_HALF_EVEN), 0.0);
737: assertEquals(-1.23, MathUtils.round(-x, 2,
738: BigDecimal.ROUND_HALF_EVEN), 0.0);
739: assertEquals(-1.235, MathUtils.round(-x, 3,
740: BigDecimal.ROUND_HALF_EVEN), 0.0);
741: assertEquals(-1.2346, MathUtils.round(-x, 4,
742: BigDecimal.ROUND_HALF_EVEN), 0.0);
743: assertEquals(1.234, MathUtils.round(1.2345, 3,
744: BigDecimal.ROUND_HALF_EVEN), 0.0);
745: assertEquals(-1.234, MathUtils.round(-1.2345, 3,
746: BigDecimal.ROUND_HALF_EVEN), 0.0);
747: assertEquals(1.236, MathUtils.round(1.2355, 3,
748: BigDecimal.ROUND_HALF_EVEN), 0.0);
749: assertEquals(-1.236, MathUtils.round(-1.2355, 3,
750: BigDecimal.ROUND_HALF_EVEN), 0.0);
751:
752: assertEquals(1.23, MathUtils.round(x, 2,
753: BigDecimal.ROUND_HALF_UP), 0.0);
754: assertEquals(1.235, MathUtils.round(x, 3,
755: BigDecimal.ROUND_HALF_UP), 0.0);
756: assertEquals(1.2346, MathUtils.round(x, 4,
757: BigDecimal.ROUND_HALF_UP), 0.0);
758: assertEquals(-1.23, MathUtils.round(-x, 2,
759: BigDecimal.ROUND_HALF_UP), 0.0);
760: assertEquals(-1.235, MathUtils.round(-x, 3,
761: BigDecimal.ROUND_HALF_UP), 0.0);
762: assertEquals(-1.2346, MathUtils.round(-x, 4,
763: BigDecimal.ROUND_HALF_UP), 0.0);
764: assertEquals(1.235, MathUtils.round(1.2345, 3,
765: BigDecimal.ROUND_HALF_UP), 0.0);
766: assertEquals(-1.235, MathUtils.round(-1.2345, 3,
767: BigDecimal.ROUND_HALF_UP), 0.0);
768:
769: assertEquals(-1.23, MathUtils.round(-1.23, 2,
770: BigDecimal.ROUND_UNNECESSARY), 0.0);
771: assertEquals(1.23, MathUtils.round(1.23, 2,
772: BigDecimal.ROUND_UNNECESSARY), 0.0);
773:
774: try {
775: MathUtils.round(1.234, 2, BigDecimal.ROUND_UNNECESSARY);
776: fail();
777: } catch (ArithmeticException ex) {
778: // success
779: }
780:
781: assertEquals(1.24, MathUtils.round(x, 2, BigDecimal.ROUND_UP),
782: 0.0);
783: assertEquals(1.235, MathUtils.round(x, 3, BigDecimal.ROUND_UP),
784: 0.0);
785: assertEquals(1.2346,
786: MathUtils.round(x, 4, BigDecimal.ROUND_UP), 0.0);
787: assertEquals(-1.24,
788: MathUtils.round(-x, 2, BigDecimal.ROUND_UP), 0.0);
789: assertEquals(-1.235, MathUtils
790: .round(-x, 3, BigDecimal.ROUND_UP), 0.0);
791: assertEquals(-1.2346, MathUtils.round(-x, 4,
792: BigDecimal.ROUND_UP), 0.0);
793:
794: try {
795: MathUtils.round(1.234, 2, 1923);
796: fail();
797: } catch (IllegalArgumentException ex) {
798: // success
799: }
800:
801: // special values
802: TestUtils.assertEquals(Double.NaN, MathUtils.round(Double.NaN,
803: 2), 0.0);
804: assertEquals(0.0, MathUtils.round(0.0, 2), 0.0);
805: assertEquals(Double.POSITIVE_INFINITY, MathUtils.round(
806: Double.POSITIVE_INFINITY, 2), 0.0);
807: assertEquals(Double.NEGATIVE_INFINITY, MathUtils.round(
808: Double.NEGATIVE_INFINITY, 2), 0.0);
809: }
810: }
|