Total Complexity | 4 |
Total Lines | 36 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | import {Rat} from './Rat' |
||
2 | |||
3 | /** |
||
4 | * @class Rational polynumber |
||
5 | * @name Polyrat |
||
6 | */ |
||
7 | 2 | export class Polyrat { |
|
8 | 5 | p: Rat[] = [] |
|
9 | |||
10 | /** |
||
11 | * Evaluate the value at x. |
||
12 | */ |
||
13 | // https://github.com/acerix/rational.js/blob/master/src/polyrat.js#L121 |
||
14 | evaluate(x: bigint): number { |
||
15 | 1 | let v = 0 |
|
16 | 1 | this.p.forEach((r, i) => { |
|
17 | 1 | v += (+r * Number(x))**i |
|
18 | }) |
||
19 | 1 | return v |
|
20 | } |
||
21 | |||
22 | /** |
||
23 | * The text representation. |
||
24 | */ |
||
25 | toString(): string { |
||
26 | 2 | return this.p.join(', ') |
|
27 | } |
||
28 | |||
29 | /** |
||
30 | * Clone this. |
||
31 | */ |
||
32 | clone(): Polyrat { |
||
33 | 1 | const p = new Polyrat() |
|
34 | 1 | for (const r of this.p) { |
|
35 | 1 | p.p.push(r.clone()) |
|
36 | } |
||
37 | 1 | return p |
|
38 | } |
||
43 |