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 org.hipparchus.UnitTestUtils;
25  import org.hipparchus.exception.MathRuntimeException;
26  import org.junit.jupiter.api.Test;
27  
28  import java.math.BigDecimal;
29  import java.math.BigInteger;
30  import java.math.MathContext;
31  
32  import static org.junit.jupiter.api.Assertions.assertEquals;
33  import static org.junit.jupiter.api.Assertions.assertFalse;
34  import static org.junit.jupiter.api.Assertions.assertNotEquals;
35  import static org.junit.jupiter.api.Assertions.assertThrows;
36  import static org.junit.jupiter.api.Assertions.assertTrue;
37  
38  class BigRealTest {
39  
40      @Test
41      void testConstructor() {
42          assertEquals(1.625,
43                              new BigReal(new BigDecimal("1.625")).doubleValue(),
44                              1.0e-15);
45          assertEquals(-5.0,
46                              new BigReal(new BigInteger("-5")).doubleValue(),
47                              1.0e-15);
48          assertEquals(-5.0, new BigReal(new BigInteger("-5"),
49                                                MathContext.DECIMAL64)
50              .doubleValue(), 1.0e-15);
51          assertEquals(0.125,
52                            new BigReal(new BigInteger("125"), 3).doubleValue(),
53                            1.0e-15);
54          assertEquals(0.125, new BigReal(new BigInteger("125"), 3,
55                                                 MathContext.DECIMAL64)
56              .doubleValue(), 1.0e-15);
57          assertEquals(1.625, new BigReal(new char[] {
58              '1', '.', '6', '2', '5'
59          }).doubleValue(), 1.0e-15);
60          assertEquals(1.625, new BigReal(new char[] {
61              'A', 'A', '1', '.', '6', '2', '5', '9'
62          }, 2, 5).doubleValue(), 1.0e-15);
63          assertEquals(1.625, new BigReal(new char[] {
64              'A', 'A', '1', '.', '6', '2', '5', '9'
65          }, 2, 5, MathContext.DECIMAL64).doubleValue(), 1.0e-15);
66          assertEquals(1.625, new BigReal(new char[] {
67              '1', '.', '6', '2', '5'
68          }, MathContext.DECIMAL64).doubleValue(), 1.0e-15);
69          assertEquals(1.625, new BigReal(1.625).doubleValue(), 1.0e-15);
70          assertEquals(1.625, new BigReal(1.625, MathContext.DECIMAL64)
71              .doubleValue(), 1.0e-15);
72          assertEquals(-5.0, new BigReal(-5).doubleValue(), 1.0e-15);
73          assertEquals(-5.0, new BigReal(-5, MathContext.DECIMAL64)
74              .doubleValue(), 1.0e-15);
75          assertEquals(-5.0, new BigReal(-5l).doubleValue(), 1.0e-15);
76          assertEquals(-5.0, new BigReal(-5l, MathContext.DECIMAL64)
77              .doubleValue(), 1.0e-15);
78          assertEquals(1.625, new BigReal("1.625").doubleValue(), 1.0e-15);
79          assertEquals(1.625, new BigReal("1.625", MathContext.DECIMAL64)
80              .doubleValue(), 1.0e-15);
81      }
82  
83      @Test
84      void testCompareTo() {
85          BigReal first = new BigReal(1.0 / 2.0);
86          BigReal second = new BigReal(1.0 / 3.0);
87          BigReal third = new BigReal(1.0 / 2.0);
88  
89          assertEquals(0, first.compareTo(first));
90          assertEquals(0, first.compareTo(third));
91          assertEquals(1, first.compareTo(second));
92          assertEquals(-1, second.compareTo(first));
93  
94      }
95  
96      @Test
97      void testAdd() {
98          BigReal a = new BigReal("1.2345678");
99          BigReal b = new BigReal("8.7654321");
100         assertEquals(9.9999999, a.add(b).doubleValue(), 1.0e-15);
101     }
102 
103     @Test
104     void testSubtract() {
105         BigReal a = new BigReal("1.2345678");
106         BigReal b = new BigReal("8.7654321");
107         assertEquals(-7.5308643, a.subtract(b).doubleValue(), 1.0e-15);
108     }
109 
110     @Test
111     void testNegate() {
112         BigReal a = new BigReal("1.2345678");
113         BigReal zero = new BigReal("0.0000000");
114         assertEquals(a.negate().add(a), zero);
115         assertEquals(a.add(a.negate()), zero);
116         assertEquals(zero, zero.negate());
117     }
118 
119     @Test
120     void testDivide() {
121         BigReal a = new BigReal("1.0000000000");
122         BigReal b = new BigReal("0.0009765625");
123         assertEquals(1024.0, a.divide(b).doubleValue(), 1.0e-15);
124     }
125 
126     @Test
127     void testDivisionByZero() {
128         assertThrows(MathRuntimeException.class, () -> {
129             final BigReal a = BigReal.ONE;
130             final BigReal b = BigReal.ZERO;
131             a.divide(b);
132         });
133     }
134 
135     @Test
136     void testReciprocal() {
137         BigReal a = new BigReal("1.2345678");
138         double eps = FastMath.pow(10., -a.getScale());
139         BigReal one = new BigReal("1.0000000");
140         BigReal b = a.reciprocal();
141         BigReal r = one.subtract(a.multiply(b));
142         assertTrue(FastMath.abs(r.doubleValue()) <= eps);
143         r = one.subtract(b.multiply(a));
144         assertTrue(FastMath.abs(r.doubleValue()) <= eps);
145     }
146 
147     @Test
148     void testReciprocalOfZero() {
149         assertThrows(MathRuntimeException.class, () -> {
150             BigReal.ZERO.reciprocal();
151         });
152     }
153 
154     @Test
155     void testMultiply() {
156         BigReal a = new BigReal("1024.0");
157         BigReal b = new BigReal("0.0009765625");
158         assertEquals(1.0, a.multiply(b).doubleValue(), 1.0e-15);
159         int n = 1024;
160         assertEquals(1.0, b.multiply(n).doubleValue(), 1.0e-15);
161     }
162 
163     @Test
164     void testDoubleValue() {
165         assertEquals(0.5, new BigReal(0.5).doubleValue(), 1.0e-15);
166     }
167 
168     @Test
169     void testBigDecimalValue() {
170         BigDecimal pi = new BigDecimal(
171                                        "3.1415926535897932384626433832795028841971693993751");
172         assertEquals(pi, new BigReal(pi).bigDecimalValue());
173         assertEquals(new BigDecimal(0.5),
174                             new BigReal(1.0 / 2.0).bigDecimalValue());
175     }
176 
177     @SuppressWarnings("unlikely-arg-type")
178     @Test
179     void testEqualsAndHashCode() {
180         BigReal zero = new BigReal(0.0);
181         BigReal nullReal = null;
182         assertEquals(zero, zero);
183         assertNotEquals(zero, nullReal);
184         assertNotEquals(zero, Double.valueOf(0));
185         BigReal zero2 = new BigReal(0.0);
186         assertEquals(zero, zero2);
187         assertEquals(zero.hashCode(), zero2.hashCode());
188         BigReal one = new BigReal(1.0);
189         assertFalse((one.equals(zero) || zero.equals(one)));
190         assertEquals(BigReal.ONE, one);
191     }
192 
193     @Test
194     void testSerial() {
195         BigReal[] Reals = {
196             new BigReal(3.0), BigReal.ONE, BigReal.ZERO, new BigReal(17),
197             new BigReal(FastMath.PI), new BigReal(-2.5)
198         };
199         for (BigReal Real : Reals) {
200             assertEquals(Real, UnitTestUtils.serializeAndRecover(Real));
201         }
202     }
203 }