Conditions | 4 |
Total Lines | 21 |
Code Lines | 15 |
Lines | 0 |
Ratio | 0 % |
Tests | 13 |
CRAP Score | 4 |
Changes | 0 |
1 | 3 | import {MAX_LOOPS} from './config' |
|
4 | |||
5 | /** |
||
6 | * Traverse the tree, returning the rational number that best approximates the floating point number. |
||
7 | */ |
||
8 | 3 | export function rationalApproximation(n: number): Rat { |
|
9 | 3 | const r = new Rat(ONE) |
|
10 | 3 | const m = [ONE, ZERO, ZERO, ONE] |
|
11 | 3 | for (let i=0; i<MAX_LOOPS; i++) { |
|
12 | 36 | if (r.approximates(n)) break |
|
13 | 33 | if (+r > n) { |
|
14 | 18 | m[0] += m[1] |
|
15 | 18 | m[2] += m[3] |
|
16 | } |
||
17 | else { |
||
18 | 15 | m[1] += m[0] |
|
19 | 15 | m[3] += m[2] |
|
20 | } |
||
21 | 33 | r.n = m[0] + m[1] |
|
22 | 33 | r.d = m[2] + m[3] |
|
23 | } |
||
24 | 3 | return r |
|
25 | } |
||
67 |