1
|
|
|
import {rationalApproximation, pathToValue, continuedFraction} from './SternBrocotTree' |
2
|
|
|
|
3
|
|
|
test('Rational number for 13/7 is as expected', () => { |
4
|
|
|
expect(rationalApproximation(13/7).toString()).toBe('13/7') |
5
|
|
|
}) |
6
|
|
|
|
7
|
|
|
test('Path to 1 is empty', () => { |
8
|
|
|
const a = 1 |
9
|
|
|
const n = pathToValue(a).next() |
10
|
|
|
expect(n.value).toBeUndefined() |
11
|
|
|
expect(n.done).toBe(true) |
12
|
|
|
}) |
13
|
|
|
|
14
|
|
|
test('Path to 3/7 is as expected', () => { |
15
|
|
|
const a = 3/7 |
16
|
|
|
const ex = [false, false, true, true] |
17
|
|
|
const r = [] |
18
|
|
|
for (const n of pathToValue(a)) { |
19
|
|
|
r.push(n) |
20
|
|
|
} |
21
|
|
|
expect(r).toStrictEqual(ex) |
22
|
|
|
}) |
23
|
|
|
|
24
|
|
|
test('Continued fraction for Φ is all ones', () => { |
25
|
|
|
const Φ = (1 + Math.sqrt(5)) / 2 |
26
|
|
|
const ex = Array(38).fill(1) |
27
|
|
|
const r = [] |
28
|
|
|
for (const n of continuedFraction(Φ)) { |
29
|
|
|
r.push(n) |
30
|
|
|
} |
31
|
|
|
expect(r).toStrictEqual(ex) |
32
|
|
|
}) |
33
|
|
|
|
34
|
|
|
test('Continued fraction for 355/113 is 3,7', () => { |
35
|
|
|
const a = 355/113 |
36
|
|
|
const ex = [3, 7] |
37
|
|
|
const r = [] |
38
|
|
|
for (const n of continuedFraction(a)) { |
39
|
|
|
r.push(n) |
40
|
|
|
} |
41
|
|
|
expect(r).toStrictEqual(ex) |
42
|
|
|
}) |
43
|
|
|
|
44
|
|
|
test('Continued fraction for π is as expected', () => { |
45
|
|
|
const ex = [3, 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14] |
46
|
|
|
const r = [] |
47
|
|
|
for (const n of continuedFraction(Math.PI)) { |
48
|
|
|
r.push(n) |
49
|
|
|
} |
50
|
|
|
expect(r).toStrictEqual(ex) |
51
|
|
|
}) |
52
|
|
|
|
53
|
|
|
test('Continued fraction for e is as expected', () => { |
54
|
|
|
const ex = [2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 1, 1, 12, 1, 1] |
55
|
|
|
const r = [] |
56
|
|
|
for (const n of continuedFraction(Math.E)) { |
57
|
|
|
r.push(n) |
58
|
|
|
} |
59
|
|
|
expect(r).toStrictEqual(ex) |
60
|
|
|
}) |
61
|
|
|
|
62
|
|
|
test('Continued fraction for √2 is as expected', () => { |
63
|
|
|
const ex = [1].concat( Array(20).fill(2) ) |
64
|
|
|
const r = [] |
65
|
|
|
for (const n of continuedFraction(Math.SQRT2)) { |
66
|
|
|
r.push(n) |
67
|
|
|
} |
68
|
|
|
expect(r).toStrictEqual(ex) |
69
|
|
|
}) |
70
|
|
|
|