View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.util;
23  
24  import java.math.BigDecimal;
25  import java.math.BigInteger;
26  import java.math.MathContext;
27  
28  import org.hipparchus.UnitTestUtils;
29  import org.hipparchus.exception.MathRuntimeException;
30  import org.junit.Assert;
31  import org.junit.Test;
32  
33  public class BigRealTest {
34  
35      @Test
36      public void testConstructor() {
37          Assert.assertEquals(1.625,
38                              new BigReal(new BigDecimal("1.625")).doubleValue(),
39                              1.0e-15);
40          Assert.assertEquals(-5.0,
41                              new BigReal(new BigInteger("-5")).doubleValue(),
42                              1.0e-15);
43          Assert.assertEquals(-5.0, new BigReal(new BigInteger("-5"),
44                                                MathContext.DECIMAL64)
45              .doubleValue(), 1.0e-15);
46          Assert
47              .assertEquals(0.125,
48                            new BigReal(new BigInteger("125"), 3).doubleValue(),
49                            1.0e-15);
50          Assert.assertEquals(0.125, new BigReal(new BigInteger("125"), 3,
51                                                 MathContext.DECIMAL64)
52              .doubleValue(), 1.0e-15);
53          Assert.assertEquals(1.625, new BigReal(new char[] {
54              '1', '.', '6', '2', '5'
55          }).doubleValue(), 1.0e-15);
56          Assert.assertEquals(1.625, new BigReal(new char[] {
57              'A', 'A', '1', '.', '6', '2', '5', '9'
58          }, 2, 5).doubleValue(), 1.0e-15);
59          Assert.assertEquals(1.625, new BigReal(new char[] {
60              'A', 'A', '1', '.', '6', '2', '5', '9'
61          }, 2, 5, MathContext.DECIMAL64).doubleValue(), 1.0e-15);
62          Assert.assertEquals(1.625, new BigReal(new char[] {
63              '1', '.', '6', '2', '5'
64          }, MathContext.DECIMAL64).doubleValue(), 1.0e-15);
65          Assert.assertEquals(1.625, new BigReal(1.625).doubleValue(), 1.0e-15);
66          Assert.assertEquals(1.625, new BigReal(1.625, MathContext.DECIMAL64)
67              .doubleValue(), 1.0e-15);
68          Assert.assertEquals(-5.0, new BigReal(-5).doubleValue(), 1.0e-15);
69          Assert.assertEquals(-5.0, new BigReal(-5, MathContext.DECIMAL64)
70              .doubleValue(), 1.0e-15);
71          Assert.assertEquals(-5.0, new BigReal(-5l).doubleValue(), 1.0e-15);
72          Assert.assertEquals(-5.0, new BigReal(-5l, MathContext.DECIMAL64)
73              .doubleValue(), 1.0e-15);
74          Assert.assertEquals(1.625, new BigReal("1.625").doubleValue(), 1.0e-15);
75          Assert.assertEquals(1.625, new BigReal("1.625", MathContext.DECIMAL64)
76              .doubleValue(), 1.0e-15);
77      }
78  
79      @Test
80      public void testCompareTo() {
81          BigReal first = new BigReal(1.0 / 2.0);
82          BigReal second = new BigReal(1.0 / 3.0);
83          BigReal third = new BigReal(1.0 / 2.0);
84  
85          Assert.assertEquals(0, first.compareTo(first));
86          Assert.assertEquals(0, first.compareTo(third));
87          Assert.assertEquals(1, first.compareTo(second));
88          Assert.assertEquals(-1, second.compareTo(first));
89  
90      }
91  
92      @Test
93      public void testAdd() {
94          BigReal a = new BigReal("1.2345678");
95          BigReal b = new BigReal("8.7654321");
96          Assert.assertEquals(9.9999999, a.add(b).doubleValue(), 1.0e-15);
97      }
98  
99      @Test
100     public void testSubtract() {
101         BigReal a = new BigReal("1.2345678");
102         BigReal b = new BigReal("8.7654321");
103         Assert.assertEquals(-7.5308643, a.subtract(b).doubleValue(), 1.0e-15);
104     }
105 
106     @Test
107     public void testNegate() {
108         BigReal a = new BigReal("1.2345678");
109         BigReal zero = new BigReal("0.0000000");
110         Assert.assertEquals(a.negate().add(a), zero);
111         Assert.assertEquals(a.add(a.negate()), zero);
112         Assert.assertEquals(zero, zero.negate());
113     }
114 
115     @Test
116     public void testDivide() {
117         BigReal a = new BigReal("1.0000000000");
118         BigReal b = new BigReal("0.0009765625");
119         Assert.assertEquals(1024.0, a.divide(b).doubleValue(), 1.0e-15);
120     }
121 
122     @Test(expected = MathRuntimeException.class)
123     public void testDivisionByZero() {
124         final BigReal a = BigReal.ONE;
125         final BigReal b = BigReal.ZERO;
126         a.divide(b);
127     }
128 
129     @Test
130     public void testReciprocal() {
131         BigReal a = new BigReal("1.2345678");
132         double eps = FastMath.pow(10., -a.getScale());
133         BigReal one = new BigReal("1.0000000");
134         BigReal b = a.reciprocal();
135         BigReal r = one.subtract(a.multiply(b));
136         Assert.assertTrue(FastMath.abs(r.doubleValue()) <= eps);
137         r = one.subtract(b.multiply(a));
138         Assert.assertTrue(FastMath.abs(r.doubleValue()) <= eps);
139     }
140 
141     @Test(expected = MathRuntimeException.class)
142     public void testReciprocalOfZero() {
143         BigReal.ZERO.reciprocal();
144     }
145 
146     @Test
147     public void testMultiply() {
148         BigReal a = new BigReal("1024.0");
149         BigReal b = new BigReal("0.0009765625");
150         Assert.assertEquals(1.0, a.multiply(b).doubleValue(), 1.0e-15);
151         int n = 1024;
152         Assert.assertEquals(1.0, b.multiply(n).doubleValue(), 1.0e-15);
153     }
154 
155     @Test
156     public void testDoubleValue() {
157         Assert.assertEquals(0.5, new BigReal(0.5).doubleValue(), 1.0e-15);
158     }
159 
160     @Test
161     public void testBigDecimalValue() {
162         BigDecimal pi = new BigDecimal(
163                                        "3.1415926535897932384626433832795028841971693993751");
164         Assert.assertEquals(pi, new BigReal(pi).bigDecimalValue());
165         Assert.assertEquals(new BigDecimal(0.5),
166                             new BigReal(1.0 / 2.0).bigDecimalValue());
167     }
168 
169     @SuppressWarnings("unlikely-arg-type")
170     @Test
171     public void testEqualsAndHashCode() {
172         BigReal zero = new BigReal(0.0);
173         BigReal nullReal = null;
174         Assert.assertTrue(zero.equals(zero));
175         Assert.assertFalse(zero.equals(nullReal));
176         Assert.assertFalse(zero.equals(Double.valueOf(0)));
177         BigReal zero2 = new BigReal(0.0);
178         Assert.assertTrue(zero.equals(zero2));
179         Assert.assertEquals(zero.hashCode(), zero2.hashCode());
180         BigReal one = new BigReal(1.0);
181         Assert.assertFalse((one.equals(zero) || zero.equals(one)));
182         Assert.assertTrue(one.equals(BigReal.ONE));
183     }
184 
185     @Test
186     public void testSerial() {
187         BigReal[] Reals = {
188             new BigReal(3.0), BigReal.ONE, BigReal.ZERO, new BigReal(17),
189             new BigReal(FastMath.PI), new BigReal(-2.5)
190         };
191         for (BigReal Real : Reals) {
192             Assert.assertEquals(Real, UnitTestUtils.serializeAndRecover(Real));
193         }
194     }
195 }