Passed
Push — main ( 12d9ec...5a557a )
by Dylan
04:52
created

Polyrat   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 17
dl 0
loc 36
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A toString 0 6 1
A evaluate 0 11 1
A clone 0 10 2
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
  }
39
40
}
41
42
export default Polyrat
43