1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.hipparchus.util;
18
19 import org.hipparchus.CalculusFieldElement;
20 import org.hipparchus.exception.MathIllegalStateException;
21 import org.junit.Assert;
22 import org.junit.Test;
23
24
25
26
27 public class FieldContinuedFractionTest {
28
29 @Test
30 public void testGoldenRatio() {
31 FieldContinuedFraction cf = new FieldContinuedFraction() {
32
33 @Override
34 public <T extends CalculusFieldElement<T>> T getA(final int n, final T x) {
35 return x.getField().getOne();
36 }
37
38 @Override
39 public <T extends CalculusFieldElement<T>> T getB(final int n, final T x) {
40 return x.getField().getOne();
41 }
42
43 };
44
45 Binary64 gr = cf.evaluate(new Binary64(0.0), 10e-9);
46 Assert.assertEquals(1.61803399, gr.getReal(), 10e-9);
47 }
48
49 @Test(expected = MathIllegalStateException.class)
50 public void testNonConvergentContinuedFraction() {
51 FieldContinuedFraction cf = new FieldContinuedFraction() {
52
53 @Override
54 public <T extends CalculusFieldElement<T>> T getA(final int n, final T x) {
55 return x.getField().getOne();
56 }
57
58 @Override
59 public <T extends CalculusFieldElement<T>> T getB(final int n, final T x) {
60 return x.getField().getOne();
61 }
62
63 };
64
65 cf.evaluate(new Binary64(0.0), 10e-9, 10);
66 }
67
68 @Test(expected = MathIllegalStateException.class)
69 public void testInfinityDivergence() {
70 FieldContinuedFraction cf = new FieldContinuedFraction() {
71
72 @Override
73 public <T extends CalculusFieldElement<T>> T getA(final int n, final T x) {
74 return x.getField().getOne().divide(n);
75 }
76
77 @Override
78 public <T extends CalculusFieldElement<T>> T getB(final int n, final T x) {
79 return x.getField().getOne();
80 }
81
82 };
83
84 cf.evaluate(new Binary64(1));
85 }
86 }